OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.42k stars 6.48k forks source link

Never create inline model for allOf with single $ref #18945

Closed ReneZeidler closed 3 months ago

ReneZeidler commented 3 months ago

Fixes #15077

The previous fix for this in #16096 is incomplete because it still generates unnecessary inline models when readOnly or nullable is used in conjunction with other properties like description. This commit fixes the logic error and adds testcases.

Schema (full schema is included issue_15077.yaml):

    Limits:
      type: object
      properties:
        allOfRef:
          allOf:
            - $ref: '#/components/schemas/NumberRange'
        allOfRefWithDescription:
          description: |
            Description for this property
          allOf:
            - $ref: '#/components/schemas/NumberRange'
        allOfRefWithReadonly:
          readOnly: true
          allOf:
            - $ref: '#/components/schemas/NumberRange'
        allOfRefWithDescriptionAndReadonly:
          description: |
            Description for this readonly property
          readOnly: true
          allOf:
            - $ref: '#/components/schemas/NumberRange'
      required:
        - allOfRef
        - allOfRefWithDescription
        - allOfRefWithReadonly
        - allOfRefWithDescriptionAndReadonly

Previous output (excerpt from output using typescript-angular generator, but the issue is independent of any language-specific generator):

export interface Limits { 
    allOfRef: NumberRange;
    /**
     * Description for this property 
     */
    allOfRefWithDescription: NumberRange;
    readonly allOfRefWithReadonly: NumberRange;
    allOfRefWithDescriptionAndReadonly: LimitsAllOfRefWithDescriptionAndReadonly;
}

The property allOfRefWithDescriptionAndReadonly generates an unnecessary inline schema that is a copy of NumberRange. The description is also missing from the property (and is instead applied to the generated inline model). The readOnly property is completely ignored.

Output with PR:

export interface Limits { 
    allOfRef: NumberRange;
    /**
     * Description for this property 
     */
    allOfRefWithDescription: NumberRange;
    readonly allOfRefWithReadonly: NumberRange;
    /**
     * Description for this readonly property 
     */
    readonly allOfRefWithDescriptionAndReadonly: NumberRange;
}

No unnecessary inline schema is created in all cases. The allOfRefWithDescriptionAndReadonly correctly has its description included as a comment and has the readonly modifier set.

PR checklist

wing328 commented 3 months ago

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

ReneZeidler commented 3 months ago

your commit (as shown in the Commits tab) is not linked to your Github account

Thanks for the note, I fixed the commit

wing328 commented 3 months ago

tested locally to confirm the fix.

--- a/.openapi-generator/FILES
+++ b/.openapi-generator/FILES
@@ -7,7 +7,6 @@ build.gradle
 build.sbt
 docs/DefaultApi.md
 docs/Limits.md
-docs/LimitsAllOfRefWithDescriptionAndReadonly.md
 docs/NumberRange.md
 git_push.sh
 gradle.properties
@@ -38,5 +37,4 @@ src/main/java/org/openapitools/client/auth/HttpBasicAuth.java
 src/main/java/org/openapitools/client/auth/HttpBearerAuth.java
 src/main/java/org/openapitools/client/model/AbstractOpenApiSchema.java
 src/main/java/org/openapitools/client/model/Limits.java
-src/main/java/org/openapitools/client/model/LimitsAllOfRefWithDescriptionAndReadonly.java
 src/main/java/org/openapitools/client/model/NumberRange.java
NikDevPHP commented 3 months ago

@wing328 Hello, when will it be possible to get an update with this fix?