Property with name class generates invalid Java code
Closed this issue · 8 comments
When having properties with a name similar to one of the Java keywords it will generate Java code that is invalid.
Example:
{
"openapi": "3.0.1",
"components": {
"schemas": {
"Fare": {
"type": "object",
"properties": {
"class": {
"allOf": [
{
"$ref": "#/components/schemas/FareClass"
}
],
"description": "Class for which this fare is valid.",
"xml": {
"name": "Class"
},
"example": "First"
}
}
}
}
}
}
This wil generate:
@Generated(value = "openapi-processor-micronaut", version = "2023.2", date = "2023-06-13T08:59:00.859252900+02:00")
public class Fare {
@JsonProperty("class")
private FareClass class; // This does not compile
public FareClass getClass() {
return class; // This does not compile
}
public void setClass(FareClass class) {
this.class = class; // This does not compile
}
}
Is there a way to fix this?
I fear there is no workaround at the moment for this issue except renaming the property.
The processor should check this and replace the invalid identifier with something else (maybe configurable). The current check only handles space and dash.
Unfortunately I cannot change the OpenAPI yaml, because it is from an external service. So changing the properties is not possible.
Please note that this happens for all the Java keywords.
right, it should avoid all keywords.
What I didn't suggest, was excluding the endpoint (https://openapiprocessor.io/spring/2023.2/mapping/endpoint.html#_excluding_endpoints) this would be an option to implement it manually.
Problem with this is, that it still generates the broken interface/classes. One would need to avoid compilation of them...
fixed in 2023.2.1, https://github.com/openapi-processor/openapi-processor-base/releases/tag/v2023.2.1
just published it, it will take about an hour before it is available on maven central.
Thanks for the fix! The code does not yet compile. It still generates the method as getClass
which is not allowed:
public FareClass getClass() { // 'getClass()' cannot override 'getClass()' in 'java.lang.Object'; overridden method is final
return aClass;
}
Do you want me to open a new issue?
grmpf, that was intentional ;-)
I thought the method prefix would avoid that problem so the fix takes care of parameters & properties only.
Guess it should avoid all methods from Object
too.
no need to create a new issue, I re-open this one
there is only one get
and no set
in Object, so it is just getClass
.
next try :-)
fixed in 2023.2.2, https://github.com/openapi-processor/openapi-processor-base/releases/tag/v2023.2.2