Skip to content

Property with name class generates invalid Java code #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joellp opened this issue Jun 13, 2023 · 8 comments
Closed

Property with name class generates invalid Java code #29

joellp opened this issue Jun 13, 2023 · 8 comments
Labels
bug Something isn't working
Milestone

Comments

@joellp
Copy link

joellp commented Jun 13, 2023

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?

@hauner
Copy link
Member

hauner commented Jun 13, 2023

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.

@hauner hauner added the bug Something isn't working label Jun 13, 2023
@joellp
Copy link
Author

joellp commented Jun 13, 2023

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.

@hauner
Copy link
Member

hauner commented Jun 13, 2023

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...

hauner added a commit that referenced this issue Jun 14, 2023
hauner added a commit that referenced this issue Jun 14, 2023
@hauner hauner added this to the 2023.2.1 milestone Jun 14, 2023
hauner added a commit that referenced this issue Jun 14, 2023
(cherry picked from commit fb4dffa)
hauner added a commit that referenced this issue Jun 14, 2023
(cherry picked from commit 20f0cb5)
hauner added a commit that referenced this issue Jun 14, 2023
@hauner
Copy link
Member

hauner commented Jun 14, 2023

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.

@hauner hauner closed this as completed Jun 14, 2023
@joellp
Copy link
Author

joellp commented Jun 14, 2023

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?

@hauner
Copy link
Member

hauner commented Jun 14, 2023

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

@hauner hauner reopened this Jun 14, 2023
@hauner
Copy link
Member

hauner commented Jun 14, 2023

there is only one get and no set in Object, so it is just getClass.

hauner added a commit that referenced this issue Jun 15, 2023
hauner added a commit that referenced this issue Jun 15, 2023
@hauner hauner modified the milestones: 2023.2.1, 2023.2.x Jun 15, 2023
@hauner
Copy link
Member

hauner commented Jun 15, 2023

next try :-)

fixed in 2023.2.2, https://github.com/openapi-processor/openapi-processor-base/releases/tag/v2023.2.2

@hauner hauner closed this as completed Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants