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

[BUG] "allOf" including a $ref and a 'description' generates unwanted new java-models #16332

Closed
maxl2287 opened this issue Aug 15, 2023 · 13 comments

Comments

@maxl2287
Copy link

maxl2287 commented Aug 15, 2023

Description

When I am using the generator for generating java classes, I face the problem that "allOf" creates unneccessarry classes.
I am not allowed to edit the schemas in this yaml.
So I need a way of configuration for the generator.

The Class "DevicePorts" should have a PortSpecs inside.
Instead now it will create a new class called "CreateRequestDevicePorts", which is not needed and unwanted here.

openapi-generator version

6.6.0

OpenAPI declaration file content or url
  schemas:
    CreateRequest:
      type: object
      properties:
        devicePorts:
          allOf:
            - $ref: "#/components/schemas/PortsSpec"
        appPorts:
          allOf:
            - $ref: "#/components/schemas/PortsSpec"
    
    Port:
      description: TCP or UDP port number
      type: integer
      minimum: 0
      maximum: 65535
    
    PortsSpec:
      type: object
      minProperties: 1
      properties:
        ranges:
          type: array
          minItems: 1
          items:
            type: object
            required:
              - from
              - to
            properties:
              from:
                $ref: "#/components/schemas/Port"
              to:
                $ref: "#/components/schemas/Port"
        ports:
          type: array
          minItems: 1
          items:
            $ref: "#/components/schemas/Port"
      example:
        ranges:
          - from: 5010
            to: 5020
        ports:
          - 5060
          - 5070
    
Generation Details

I am using the maven plugin in the pom.xml.

<plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>6.6.0</version>
        <executions>
          <execution>
            <id>server</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/static/someyaml.yml</inputSpec>
              <generatorName>spring</generatorName>
              <apiPackage>,,,</apiPackage>
              <modelPackage>...</modelPackage>
              <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
              <configOptions>
                <useSpringBoot3>true</useSpringBoot3>
                <delegatePattern>true</delegatePattern>
                <useJakartaEe>true</useJakartaEe>
              </configOptions>
              <generateApiTests>false</generateApiTests>
              <generateModelTests>false</generateModelTests>
            </configuration>
          </execution>
        </executions>
      </plugin>
Steps to reproduce
Related issues/PRs
Suggest a fix
@maxl2287 maxl2287 changed the title [BUG] Description "allOf" generates unnecessarry java-classes Aug 15, 2023
@wing328
Copy link
Member

wing328 commented Aug 15, 2023

please try 7.0.0-beta, which should no longer created those allOf sub-schemas as models separately

@maxl2287
Copy link
Author

Hi @wing328,

I tried 7.0.0-beta now, but it seems that it's still generating these files "CreateRequestDevicePorts".
Is there any configuration flag needed, which I need to set?

@maxl2287 maxl2287 changed the title "allOf" generates unnecessarry java-classes [BUG] "allOf" generates unnecessarry java-classes Aug 16, 2023
@wing328
Copy link
Member

wing328 commented Aug 16, 2023

can you try the latest master instead?

snapshot version can be found in the project's readme

@maxl2287
Copy link
Author

maxl2287 commented Aug 16, 2023

I used now 7.0.0-SNAPSHOT and even there CreateRequestDevicePorts is created.

So it seems that this hasn't changed anything.

It would be good at the end (even when it's created now) that it would extend from PortsSpecs.
But PortsSpecs, as it is defined as a $ref in the "allOf", is not referenced anywhere in the created files.

@wing328
Copy link
Member

wing328 commented Aug 17, 2023

I did a test with the latest master and I got the following in CreateRequest.java:


@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-08-17T10:35:20.658382+08:00[Asia/Hong_Kong]")
public class CreateRequest {

  private PortsSpec devicePorts;

  private PortsSpec appPorts;

  public CreateRequest devicePorts(PortsSpec devicePorts) {
    this.devicePorts = devicePorts;
    return this;
  }

CreateRequestDevicePorts is not created:

[main] INFO  o.o.c.languages.AbstractJavaCodegen - Processing operation pingGet
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/src/main/java/org/openapitools/model/CreateRequest.java
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/src/main/java/org/openapitools/model/PortsSpec.java
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/src/main/java/org/openapitools/model/PortsSpecRangesInner.java
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/src/main/java/org/openapitools/api/PingApiController.java
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/src/main/java/org/openapitools/api/PingApi.java
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/pom.xml
[main] INFO  o.o.codegen.TemplateManager - writing file /tmp/ping2/README.md

@maxl2287
Copy link
Author

@wing328 I found now the issue, why it's working on your side but not on my side:

    CreateRequest:
      type: object
      properties:
        devicePorts:
          allOf:
            - description: Here is some description which will cause a new ModelClass
            - $ref: "#/components/schemas/PortsSpec"
        appPorts:
          allOf:
            - $ref: "#/components/schemas/PortsSpec"

It's because I have locally a description besides of the $ref.
This causes a new generated class, where you can see that "appPorts" is having PortsSpec and devicePorts have the newly generated class.

The class

public class CreateRequest {
    private CreateRequestDevicePorts devicePorts;
    private PortsSpec appPorts;

    public CreateRequest() {
    }

    public CreateRequest devicePorts(CreateRequestDevicePorts devicePorts) {
        this.devicePorts = devicePorts;
        return this;
    }

    @Schema(
        name = "devicePorts",
        requiredMode = RequiredMode.NOT_REQUIRED
    )
    @JsonProperty("devicePorts")
    public @Valid CreateRequestDevicePorts getDevicePorts() {
        return this.devicePorts;
    }

    public void setDevicePorts(CreateRequestDevicePorts devicePorts) {
        this.devicePorts = devicePorts;
    }

    public CreateRequest appPorts(PortsSpec appPorts) {
        this.appPorts = appPorts;
        return this;
    }

@maxl2287 maxl2287 changed the title [BUG] "allOf" generates unnecessarry java-classes [BUG] "allOf" including a $ref and a 'description' generates unwanted new java-models Aug 17, 2023
@wing328
Copy link
Member

wing328 commented Aug 17, 2023

I think you should do the following instead:

        devicePorts:
          allOf:
            - $ref: "#/components/schemas/PortsSpec"
          description: Here is some description which will cause a new ModelClass

@maxl2287
Copy link
Author

Yes that's working fine.
In the case that I am not allowed to change the spec, I need to create an issue there to change it.
I guess an other configurable option is not here, isn't it?

@wing328
Copy link
Member

wing328 commented Aug 17, 2023

Right

@maxl2287
Copy link
Author

Thanks for your help, @wing328 ! 🚀

@wing328
Copy link
Member

wing328 commented Aug 17, 2023

👍

(optional) You may consider supporting this project financially via https://opencollective.com/openapi_generator.

@maxl2287
Copy link
Author

Hi again @wing328,

it seems that this is only working with the 7.0.0-SNAPSHOT.
Otherwise it will, also with the description outside of the "allOf" generate the "unwanted" classes like "CreateRequestDevicePorts".

It's not working with 7.0.0-beta.
I cannot use the SNAPSHOT version when it's not officially released in the, e.g., Maven Repository.

2 questions now

1.) When will be the next release of 7.0.0?
2.) Is there any configuration for 7.0.0-beta to avoid this behavior?

Thanks in advance.

@wing328
Copy link
Member

wing328 commented Aug 24, 2023

v7.0.0 will be released this week or next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants