Generate the OpenAPI Specification
Several languages having tooling that supports both:
- Generating code from an Open API Spec.
- Generating an Open API spec from code.
Note: Typically you would choose a direction - either generate the code or generate the Open API spec - do not try to do both in one project.
How could we ensure that OpenAPI documentation is up-to-date and
compliant when code generators are noted used?
If you don't want to generate any code, you can look into a tool set for your language that will generate the Open API spec for you.
Client Stubs
Generating the OpenAPI spec is only a partial solution, as most of the existing tools are designed so that you can:
- Write the server implementation.
- Include comments or annotations (depending on the language) to define the OpenAPI spec in the code.
- Generate the OpenAPI spec from the server code.
- Generate client stubs (from the OpenAPI spec).
If you insist on no code generation you could "jump through hoops", for example by creating a seperate package that contains the "transport model" (set of classes that are serialized/deserialized - typically to JSON). So that you can use the same package/model in both the server and client code.
However I don't see a problem with generating client stubs.
Since you typically don't need to edit any of the generated code - it might be necessary for someone to open the generated code to get a feel for what the code generator is generating - but once they understand how it works, it is unlikely they will need to look at the generated code again.
Templated Server Side Implementations
sometimes code generators generate code that is not up to agreed
coding standards,
This is only really a problem if you intent to edit the code after it has been generated and typically this only happens when you use a code generator to generate the empty methods for the server implementation.
The way I have solved this problem is to have the code generator generate interfaces. The classes that implement the interfaces are then hand coded - any change to the generated interface causes a compile time error. I typically use an IoC framework to inject the dependencies, but you could just establish a naming convention for your implementation classes.
Unfortunately its unlikely there will be a code generator that exactly meets your needs for this approach, so you will either need to:
- Extend the existing OpenAPI generators - there is good documentation on how to do that.
- (or) Roll your own code generator.
sometimes code generators are out-of-date, or sometimes they may not
be available.
Any code generator you maintain will be completely up to date and contain all the features you need....
Good Documenation
Missing API documentation slows down the development process.
When open API is tied to the code (code/spec generation) - it is very good at enumerating structures / ensuring typo's don't creep in.
But it is not a substitute for good documentation.
So many times I see a field in OpenAPI documented as:
String flibwibble - The current Flibwibble.
Nowhere in the OpenAPI documentation does it explain what a Flibwibble is...
To put it another way you should probably also have good API documentation in addition to the OpenAPI spec.
not up to agreed coding standards
your frameworks and libs aren't either. If they aren't a concern, why the standards of auto-generated code would be a problem? Usually, it's code you don't have to maintain. Not even to track or keep in your SCM. This code is recreated every time you build the code.