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

[Spring][server] oneOf Interface for Spring Boot #10392

Closed
wants to merge 28 commits into from

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Sep 14, 2021

Resolved merge conflicts for #8091

Credits: @jburgess

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/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @nmuesch (2021/01)

alexsuperdev and others added 28 commits March 22, 2020 11:15
added x-is-one-of-interface extension for oneOf interface in mustache
template
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf

Fix #5381
parcelableModel is not required
removed not needed methods
catch NPE cases in preprocessOpenAPI
updated samples
fixed generation of oneOf Models
addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring
NPE fix
…rator into spring_fix_5381

� Conflicts:
�	modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
spring: fixed use of oneOf Models in API
implementing oneOf for spring lib overriding methods with different behavior from default
added x-is-one-of-interface extension for oneOf interface in mustache
template
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf

Fix #5381
removed not needed methods

Fix #5381
fixed generation of oneOf Models

Fix #5381
addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring

Fix #5381
NPE fix for tests
fixed handing of composed schema with array
fixed NPE in addOneOfInterfaceModel
fixed generation of oneOf models with descriminator
# Conflicts:
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
# Conflicts:
#	modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java
# Conflicts:
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
#	modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java
Copy link

@Orachigami Orachigami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a check for JACKSON library. Otherwise, code with GSON will be not compilable.
See PR #10286


@Override
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) {
Copy link

@Orachigami Orachigami Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. Can you please submit a PR against this branch with the suggested fix?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 I created PR against jburgess-spring_fix_5381_jb #10433 and resolved conflicts

for (Map<String, Object> mo : models) {
CodegenModel cm = (CodegenModel) mo.get("model");
if (this.serializableModel) {
cm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use new ArrayList<String>(1) as you know the size upfront

if (p.defaultValue == null) {
return;
}
Boolean fixLong = (p.isLong && "l".equals(p.defaultValue.substring(p.defaultValue.length()-1)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use boolean lowercase as values can't be null

@Override
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) {
Map<String, String> oneImport = new HashMap<String, String>() {{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final Map<String, String> oneImport = new HashMap<>(1);
oneImport.put("import", importMapping.get(i));

it's faster and more readable. using {{ }} is normally considered antipattern unless in static {} block

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless in static {} block

A lot of people would complain on this too

@Orachigami Orachigami mentioned this pull request Sep 22, 2021
5 tasks
@wing328 wing328 modified the milestones: 5.3.0, 5.3.1 Oct 25, 2021
@wing328 wing328 modified the milestones: 5.3.1, 5.4.0 Dec 29, 2021
}

@Override
public void postProcessParameter(CodegenParameter p) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime, postProcessParameter() moved to AbstractJavaCodeGen with [Java][Jaxrs-Resteasy] Fixes generator devaultValues for int64/float/… (#8988)

CodegenModel cm = (CodegenModel) mo.get("model");
if (this.serializableModel) {
cm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>());
((ArrayList<String>) cm.getVendorExtensions().get("x-implements")).add("Serializable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x-implements Serializable is at least duplicated in JavaClientCodeGen.

@cachescrubber
Copy link
Contributor

@wing328 I really would like to get this done. Should I support / bring this PR up to date?

@pedrodovale
Copy link

Hi,

I'm also following this PR with great interested. Thanks for the contributor's work on this.

I have a question about this. Taking the committed example ByRefOrValue document as an example: Why is it that the generated FooRefOrValue is an interface and not a class extended by the Foo and FooRef? I ask this because this would be useful in the case where there are common fields. They could be defined in an abstract class and then there could exist getters for more than just the discriminator.propertyName @type.

Apologies if this is explained elsewhere. I didn't find it.

@Orachigami
Copy link

Orachigami commented Jan 12, 2022

@cachescrubber @pedrodovale Have you checked this PR #10463 ?

@cachescrubber
Copy link
Contributor

@Orachigami Thx for the heads up. I'll have a look at #10463.

@wing328 This PR is still tagged with the 5.4.0 milestone and not closed. Why is that?

@cachescrubber
Copy link
Contributor

cachescrubber commented Jan 13, 2022

@wing328 , anyone. Which of the Java generators (if any) has the most complete and stable support for polymorphism (oneOf/anyOf)?

EDIT: The java generator (libraries okhttp-gson-nextgen and jersey2) include an implementation where a wrapper class holding the oneOfs is generated, including a custom Deserializer.

@pedrodovale
Copy link

@cachescrubber @pedrodovale Have you checked this PR #10463 ?

Yes @Orachigami, I did. Thanks to the oneOf unit tests in the fork I was able to model a type and its subtypes and obtain the classes with the desired JsonSubTypes annotations. I've also got the expected sources using the code in this PR.

Here is the class that contains the unit tests (hope it helps someone): SpringCodegenTest

Cheers!

@cachescrubber
Copy link
Contributor

@wing328 The branch has merge conflicts. Could you please merge in the current master? If you need any help merging pojo.java feel free to contact me.

@cachescrubber
Copy link
Contributor

cachescrubber commented Jan 27, 2022

@pedrodovale The classes enumerated in the oneOf might allready extend other classes. Thus the marker interface as an implemenation strategy. Your suggestion is also quite reasonable and reminds me on the XSD way to model extensions - which works quite well indeed.

For example the following XSD would generate code as you describe, ie AcceptanceNotificationType extends NotificationType.

  <xs:complexType name="NotificationType">
    <xs:sequence>
      <xs:element name="status" type="com:StatusType"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="AcceptanceNotificationType">
    <xs:complexContent>
      <xs:extension base="com:NotificationType">
        <xs:sequence>
          <xs:element name="details" type="com:OrderDetailsType" minOccurs="0"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  <xs:complexType name="CompletionNotificationType">
    <xs:complexContent>
      <xs:extension base="com:NotificationType">
        <xs:sequence>
          <xs:element name="completionDate" type="xs:date"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType> 

@cachescrubber
Copy link
Contributor

cachescrubber commented Jan 27, 2022

@wing328 @Orachigami, There will be a clash if the class denoted by the oneOf contains a type property, which is an Enum. The Interface will generate String getType() the concrete class MyType getType(). Should we support this, or is it considered bad modelling?

    Location:
      description: The LocationType that is used in the Order. When Order info is retrieved and the UnitNumber is known, the Location will always be of type LocationWohnEinheitIdBased
      oneOf:
        - $ref: '#/components/schemas/LocationHouseIdBased'
        - $ref: '#/components/schemas/LocationWohnEinheitIdBased'
      example:
        type: HOUSE_ID_BASED
        houseId: "01234ABC-123-A"
      discriminator:
        propertyName: type
        mapping:
          HOUSE_ID_BASED: '#/components/schemas/LocationHouseIdBased'
          WOHNEINHEIT_ID_BASED: '#/components/schemas/LocationWohnEinheitIdBased'

    LocationType:
      type: string
      default: HOUSE_ID_BASED
      description: the type of a Location
      enum:
        - HOUSE_ID_BASED
        - WOHNEINHEIT_ID_BASED

The OAS in question could be downloaded here. Disclaimer: I'am neither the Author nor Api Owner.

@wing328
Copy link
Member Author

wing328 commented Mar 16, 2022

Closed via #11650

@wing328 wing328 closed this Mar 16, 2022
@wing328 wing328 deleted the jburgess-spring_fix_5381_jb branch April 4, 2022 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants