I am writing a Rest API and am wondering how best to handle supporting the different versions. By this I don't mean how to define a URI as V2 or V3, but rather how to structure the code given that it'd need to:
- Support multiple versions at the same time, eg. V1 & V2 & V3 URIs must be live at the same time. I would retire V1 when say V4 comes in in order to constrain the amount supported at any one time.
- Avoid as much code duplication as possible
- Make it easy to add non-breaking changes to a version, without it impacting on other versions
It would seem that there are few approaches that could be taken:
Use Git to control the versions, with a branch for the different versions (and old versions essentially having no new development work done on it). This would mean no code duplication as only the latest version is in the code, but previous versions would need to work with the new version of the DB until they are retired.
Duplicate code so that each version is handled in the same application and has a totally separate code path, but this would mean a lot of duplication
Re-use a lot of code across the versions, but this would make it harder to maintain as changing one version is more likely to impact a previous version
Is there any best practice to deal with this problem as all options seem to have their own problems?