It's all about the answers!

Ask a question

[closed] How to do API versioning?


Geetha Ganesan (111) | asked Apr 20 '15, 1:49 a.m.
closed Jun 26 '21, 7:41 a.m. by Ralph Schoon (63.1k33645)
Hi,

I have to do API versioning. I am using REST APIs and Restlet framework. I am confused how to implement the versioning in Source code.

If my API looks like this : http://localhost:8080/example.com/v2/hello
Then In my source code how to map the version to appropriate functionality?
For Example :
If(version == 1) {//Do Version 1 Source Code}
else If(version == 2) {//Do Version 2 Source Code}
else If(version == 3) {//Do Version 3 Source Code} ………..
If I did like this, Then why we are going for Versioning. We can implement Manually Right? And also If I have to add some 100 versions, then the code looks uglier and becomes lengthier.
I get confused how to map the version in Source Code. Please Clarify my doubt.

Regards,
Geetha.

The question has been closed for the following reason: "Question is off-topic or not relevant" by rschoon Jun 26 '21, 7:41 a.m.

3 answers



permanent link
Surya Tripathi (65017) | answered Apr 24 '15, 1:57 p.m.
I think a source control system is not going to address your problem of how to version apis. Source control system will manage changes to your code and store versions of files in database. 

If you are making breaking changes to your api, you are breaking contract and that is why you would want to provide a new uri to access to that resource. As far how to manage different versions of the apis, I would think, you can baseline your code everything you ship a new version of the api.  

permanent link
sam detweiler (12.5k6195201) | answered Apr 25 '15, 10:11 a.m.
my old training says, backward compatibility requires that  you don't GET to make breaking changes. you MUST produce the same output with the same input.

you can ADD an new api/method, but cannot remove/change the old one..

and versioning an api really means, return some value that the client can use to detemine which level of function you support.

based on the prior rule, version 2 MUST support everything in version 1 EXACTLY.
and version 3 MUST support everything in version 2, etc..

so your code doesn't need any of the IF checks..  you add more function where you need it, maybe map old methods onto new code (provided old methods continue to return exact same results).

you add one total method, that returns your 'version' number.
and each time you add an api, you increase the version number returned.

regression testcases are important to insure that nothing changed from the prior releases

permanent link
Ralph Schoon (63.1k33645) | answered Apr 26 '15, 10:50 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
You might want to look into http://www-03.ibm.com/software/products/en/api-management it is also provided on BlueMix as a service.