(Sample) Problem:
When a yaml source contains conflicting definitions, e.g. myClass
and MyClass
both generating into a java class MyClass
with one overwriting the other, no warning or error occurs.
Other similar cases can occur the same way (e.g. via implicitly generated classes from complex entries of type: array
).
Framing
- has to work without modifying the source file
- there is exactly one source file, for whatever that's worth
- has to work as a Maven plugin
- tested with
swagger-codegen-maven-plugin
2.4.0 and 2.4.22
- tested with
- am familiar with JAXB2 and both of its common Maven plugins:
jaxb2-maven-plugin
maven-jaxb2-plugin
- am not familiar with
swagger-codegen-maven-plugin
- did not find solutions (or even corresponding problem reports) using Google searches
swagger codegen strict
swagger-codegen-maven-plugin strict
swagger codegen name collision
swagger-codegen-maven-plugin name collision
swagger codegen resolve name collision
swagger-codegen-maven-plugin resolve name collision
- found this closed issue describing a similar issue (silently generates corrupt code), which appears to be mostly unrelated and is alledgedly long fixed
Question
What is the proper solution that produces
- an eagerly failing generation process
- amended via externally configured
customBindings
- without touching the
yaml
source file
?
Solution ideas:
Use JAXB2 instead
JAXB2
- fails generation where a generated file already exists
- allows a separate
customBindings.xjs
file to modify generation of classes - can be adapted to work with YAML using
JacksonJaxbYAMLProvider
However it seems that the common Maven plugins jaxb2-maven-plugin
and maven-jaxb2-plugin
do not support YAMl definition files (out of the box?). [1]
Use Mustache files to resolve the name collision
The Mustache language seems to provide the necessary facilities to implement rules similar to JAXBs xjb
.
However
- binding a Mustache template to a class has to be done by name (
<class name>.mustache
), aka. name collision in particular cannot be resolved from an external file [2] - this does not help with the silence of errors in generated code - one cannot fix the error, while unaware of it
1 - Searches for this topic have yielded one result that it does not work out-of-the-box (duh) and no results whether it can be made to work using the extensions
interface both plugins support.
2 - I could not find the relevant part directly within Swagger codegen documentation, but here is a relevant part of the implemented-by-Swagger OpenAPI generator documentation.