Skip to content
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

Fix XML annotations on model properties (JavaSpring) #18392

Conversation

chschu
Copy link
Contributor

@chschu chschu commented Apr 15, 2024

This PR fixes the annotations generated by the JavaSpring generator when using withXml: true. It is related to #2417, #5078 and #5764.

  • If the property is not a container:
    • If the property is configured as an XML attribute, the generator will produce @XmlAttribute and @JacksonXmlProperty using the property's XML configuration.
    • If the property is not configured as an XML attribute, the generator will produce @XmlElement and @JacksonXmlProperty using the property's XML configuration.
  • If the property is a container:
    • The generator will produce @XmlElement and @JacksonXmlProperty using the container items' XML configuration.
    • If the property has wrapped: true in its XML configuration, the generator will additionally produce @XmlElementWrapper and @JacksonXmlElementWrapper using the container's XML configuration.
    • If the property doesn't have wrapped: true in its XML configuration, the generator will additionally produce @JacksonXmlElementWrapper(useWrapping = false). IIRC, different Jackson versions behave differently if the annotation is not present

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@chschu chschu changed the title fix XML annotations on model properties (JavaSpring) Fix XML annotations on model properties (JavaSpring) Apr 15, 2024
* generate JAXB annotations for attributes and elements

* generate wrapper annotations (JAXB and Jackson)

* use XML config from items for annotations of containers
@chschu chschu force-pushed the bugfix/JavaSpring-Jackson-XML-Annotations branch from 0879303 to e588bdb Compare April 15, 2024 16:30
@Philzen
Copy link
Contributor

Philzen commented Apr 23, 2024

Shouldn't the changes be done to modules/openapi-generator/src/main/resources/JavaSpring/xmlAnnotation.mustache?

Looking at that file, i believe it would also be cleaner to include it as well at line 217 ... or even better, remove it from there completely – in my tests with Jackson it was totally sufficient to have the annotation on the field rather than the getter.

@chschu
Copy link
Contributor Author

chschu commented Apr 25, 2024

@Philzen: Thanks for having a look.

The xmlAnnotation.mustache is responsible for the class-level annotations, so it cannot be used for the annotations on the getter.

Having the annotations on either the field or the getter doesn't make much of a difference in my experience as well. I had some issues with conflicting annotations on getters and fields in the past, so they should probably be all in one place.

As the @JacksonXmlProperty annotation has recently been moved to the getter, I just added the missing ones there.

@Philzen
Copy link
Contributor

Philzen commented May 2, 2024

The xmlAnnotation.mustache is responsible for the class-level annotations, so it cannot be used for the annotations on the getter.

Of course, you're right, i missed that bit.

Having the annotations on either the field or the getter doesn't make much of a difference in my experience.

I know of at least one edge case that can feel like kind of a footshot: when you have a public variable that has a different name than its getter (e.g. boolean _public with boolean getPublic() getter), then you'll end up with Jackson producing both public and _public on serialization. However i understand that's a completely different discussion given that all of the annotations (not only XML related ones) were basically moved away from fields to getters somewhere between version 4 and 7, so i'm fine with keeping it at getter for the moment.

As the @JacksonXmlProperty annotation has recently been moved to the getter, I just added the missing ones there.

Thanks looking into the file history and providing this commit as context! However the link does not work this way in markdown, leaving the working one here for reference: 69a4a65

@Philzen
Copy link
Contributor

Philzen commented May 2, 2024

@chschu Happy to report i was able to whip up a basic test: Philzen@df55a9d

I would like to add some refactoring, then also fix the default Java generators and finally add some more tests that also cover the JAXB-related annotation aspects before assembling everything into a joint PR. Kindly allow me some more time to finalize that.

If you have any suggestions which aspects should be covered as part of the test as well i'd be more than happy to hear them in the meanwhile.

@Philzen
Copy link
Contributor

Philzen commented May 3, 2024

Note to self: Also add dedicated tests whether xml.name from $ref'd schemas is correctly applied (when it does, we have solved #5989 as well).

@Philzen
Copy link
Contributor

Philzen commented May 3, 2024

Looks like this PR could also close #9371

@chschu
Copy link
Contributor Author

chschu commented May 7, 2024

@Philzen Nice, thank you for the test! I'm really looking forward to the joint PR.

I think the test could also check if the getTags methods has the following annotations (hope I got them right):

@JacksonXmlProperty(localName = "Tag")
@JacksonXmlElementWrapper(localName = "tag")

The property has an explicit XML wrapper name of "tag" (line 91 in the yaml), and an an XML element name of "Tag" (line 60 in the yaml). The wrapper could also be changed to something like "TagList" to distinguish it from the item.

Maybe Pet.name could use xml.attribute: true to cover the attribute branch in the template. This should generate the following annotation on getName:

@JacksonXmlProperty(isAttribute = true, localName = "name")

Are the JAXB annotations (@XmlAttribute, @XmlElement and @XmlElementWrapper) already checked elsewhere, or should the test also cover them?

@Philzen
Copy link
Contributor

Philzen commented May 20, 2024

@chschu Thanks for your valuable feedback!

I think the test could also check if the getTags methods has the following annotations (hope I got them right):

@JacksonXmlProperty(localName = "Tag")
@JacksonXmlElementWrapper(localName = "tag")

The property has an explicit XML wrapper name of "tag" (line 91 in the yaml), and an an XML element name of "Tag" (line 60 in the yaml). The wrapper could also be changed to something like "TagList" to distinguish it from the item.

Done: Philzen@837a0a7

Maybe Pet.name could use xml.attribute: true to cover the attribute branch in the template. This should generate the following annotation on getName:

@JacksonXmlProperty(isAttribute = true, localName = "name")

Done: Philzen@2db2b7b

Are the JAXB annotations (@XmlAttribute, @XmlElement and @XmlElementWrapper) already checked elsewhere, or should the test also cover them?

Done: Philzen@a0afca5

If OK with you, i shall open a new PR when as soon as i'm happy with the branch, as i cannot push to your repo. Before that, i still have a couple of things to review:

@chschu
Copy link
Contributor Author

chschu commented May 22, 2024

@Philzen That's more than OK with me. Thank you for the effort you put into fixing this!

Once your PR is ready, I'll close this one.

@Philzen
Copy link
Contributor

Philzen commented Jun 6, 2024

Closing this in favor of #18870 (which includes your commit)

@Philzen Philzen closed this Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants