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][TypeScript - axios] Redundant models - the same type is generated multiple times #15077

Closed
2 tasks
MartinNovak1991 opened this issue Mar 30, 2023 · 3 comments · Fixed by #18945
Closed
2 tasks

Comments

@MartinNovak1991
Copy link

MartinNovak1991 commented Mar 30, 2023

Bug Report Checklist

  • [*] Have you provided a full/minimal spec to reproduce the issue?
  • [*] Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • [*] Have you searched for related issues/PRs?
  • [*] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

OpenApi description generated just one model file in 5.4.0. In 6+ (tested with 6.0.0 and 6.4.0) every property (of the same schema) is generating one file per property, and all these files are pointing to the one (same) model/type.

openapi-generator version

5.4.0 ok, but generating with 6.0.0 and 6.4.0 causes issue described above.

OpenAPI declaration file content or url

There is sample of problematic part (in schemas)

    PaginationOffsetType:
      type: object
      properties:
        offset:
          type: integer
          minimum: 0
      required:
        - offset
    OffsetsObjectType:
      type: object
      properties:
        _offsets:
          type: object
          properties:
            self:
              allOf:
                - description: |
                    desc 1
                - $ref: '#/components/schemas/PaginationOffsetType'
            first:
              allOf:
                - description: |
                    desc 2
                - $ref: '#/components/schemas/PaginationOffsetType'
            previous:
              allOf:
                - description: >
                    desc 3
                - $ref: '#/components/schemas/PaginationOffsetType'
            next:
              allOf:
                - description: >
                   desc 4
                - $ref: '#/components/schemas/PaginationOffsetType'
          required:
            - first
            - self
      required:
        - _offsets
Generation Details

"npx openapi-generator-cli generate --generator-key ${yaml-filename}"
I tried "--openapi-normalizer REF_AS_PARENT_IN_ALLOF=true" but no effect

Steps to reproduce

simply run generate sample above ... it will create (useless?) four (suffixed) models:
image

You can see propertyName suffix ... with contents:

offsets-object-type-offsets.ts:

export interface OffsetsObjectTypeOffsets {  
       'self': OffsetsObjectTypeOffsetsSelf;  
       'first': OffsetsObjectTypeOffsetsFirst;  
       'previous'?: OffsetsObjectTypeOffsetsPrevious;   
       'next'?: OffsetsObjectTypeOffsetsNext;
}

offsets-object-type-offsets-first.ts:

export type OffsetsObjectTypeOffsetsFirst = PaginationOffsetType;

offsets-object-type-offsets-next..ts:

export type OffsetsObjectTypeOffsetsNext = PaginationOffsetType;

offsets-object-type-offsets-previous.ts:

export type OffsetsObjectTypeOffsetsPrevious= PaginationOffsetType;

offsets-object-type-offsets-self.ts:

export type OffsetsObjectTypeOffsetsSelf= PaginationOffsetType;

pagination-offset-type.ts:

export interface PaginationOffsetType {
    'offset': number;
}

v5.4.0 was generating:
image

offsets-object-type.ts:

export interface OffsetsObjectType {  
   '_offsets': OffsetsObjectTypeOffsets;
}

offsets-object-type-offsets.ts:

export interface OffsetsObjectTypeOffsets {  
      'self': PaginationOffsetType ;  
      'first': PaginationOffsetType ;  
      'previous'?: PaginationOffsetType ;  
      'next'?: PaginationOffsetType ;
}

pagination-offset-type.ts:

export interface PaginationOffsetType {
    'offset': number;
}

I would exepect the same result, optimized, not reduntant files/types (with "equal" content)

Related issues/PRs

I have not found any related issue

Suggest a fix

No idea

@ReneZeidler
Copy link
Contributor

I'm having the same problem, and it seems to be caused by having a description in combination with allOf.
My declaration looks like this:

"NumberRange": {
  "type": "object",
  "properties": {
    "min": { "type": "number", "description": "Minimum value of the range" },
    "max": { "type": "number", "description": "Maximum value of the range" },
    "step": { "type": "number", "description": "Step size for valid values" }
  },
  "required": ["min", "max"]
},
"MyModel": {
  "type": "object",
  "properties": {
    "range1": {
      "description": "This range describes...",
      "readOnly": true,
      "allOf": [{ "$ref": "#/components/schemas/NumberRange" }]
    },
    "range2": {
      "readOnly": true,
      "allOf": [{ "$ref": "#/components/schemas/NumberRange" }]
    }
  },
  "required": ["range1", "range2"]
}

Using OpenAPI Generator 6.5.0 with REF_AS_PARENT_IN_ALLOF=true already enabled, I get this output (using the typescript-angular generator):

export interface MyModel {
    readonly range1: MyModelRange1;
    readonly range2: NumberRange;
}

/**
 * This range describes...
 */
export interface MyModelRange1 extends NumberRange { 
}

It creates a new model for each property with a description, which in reality are many more than just one. The new models extend the base model (because of REF_AS_PARENT_IN_ALLOF), but this is still suboptimal. Without REF_AS_PARENT_IN_ALLOF, the new models would be copies of the base model.
The property description is retained in some form, but as the description of the new model instead of the property on the main model. Therefore it won't be shown by IDEs when autocompleting the property name.

Of course I can remove the description, which gets rid of the additional models:

export interface MyModel {
    readonly range1: NumberRange;
    readonly range2: NumberRange;
}

However, especially in a case like this, where the model of the property is something general (like NumberRange here), having a description would be really helpful.


The output I really want is this, which is what OpenAPI Generator 5.4.0 generated:

export interface MyModel {
    /**
     * This range describes...
     */
    readonly range1: NumberRange;
    readonly range2: NumberRange;
}

This issue is realted to #12838, where special case handling of allOf in combination with nullable and readOnly has already been added, however description was missed.

The temporary workaround was supposed to be replaced by a Normalizer ruleset, but as far as I can tell none of the currently available rulesets solve this issue.

@MartinNovak1991
Copy link
Author

MartinNovak1991 commented Jun 19, 2023

Any update?

I tried v6.6.0 ... same result > It generates 4 same type (OffsetsObjectTypeOffsetsSUFFIX.ts files). I noticed this in console:

[srcFile] [main] WARN o.o.codegen.InlineModelResolver - allOf schema "null" containing multiple types (not model) is not supported at the moment.

[srcFile] [main] INFO o.o.codegen.InlineModelResolver - Inline schema created as OffsetsObjectType__offsets_self. To have complete control of the model name, set the "title" field or use the inlineSchemaNameMapping option (--inline-schema-name-mappings in CLI).

[srcFile] [main] WARN o.o.codegen.InlineModelResolver - allOf schema "null" containing multiple types (not model) is not supported at the moment.

[srcFile] [main] INFO o.o.codegen.InlineModelResolver - Inline schema created as OffsetsObjectType__offsets_first. To have complete control of the model name, set the "title" field or use the inlineSchemaNameMapping option (--inline-schema-name-mappings in CLI).

[srcFile] [main] WARN o.o.codegen.InlineModelResolver - allOf schema "null" containing multiple types (not model) is not supported at the moment.

[srcFile] [main] INFO o.o.codegen.InlineModelResolver - Inline schema created as OffsetsObjectType__offsets_previous. To have complete control of the model name, set the "title" field or use the inlineSchemaNameMapping option (--inline-schema-name-mappings in CLI).

[srcFile] [main] WARN o.o.codegen.InlineModelResolver - allOf schema "null" containing multiple types (not model) is not supported at the moment.

[srcFile] [main] INFO o.o.codegen.InlineModelResolver - Inline schema created as OffsetsObjectType__offsets_next. To have complete control of the model name, set the "title" field or use the inlineSchemaNameMapping option (--inline-schema-name-mappings in CLI).

[srcFile] [main] INFO o.o.codegen.InlineModelResolver - Inline schema created as OffsetsObjectType__offsets. To have complete control of the model name, set the "title" field or use the inlineSchemaNameMapping option (--inline-schema-name-mappings in CLI).

I mean this: WARN o.o.codegen.InlineModelResolver - allOf schema "null" containing multiple types (not model) is not supported at the moment.

What to do?

This is part of YAMLfile:

OffsetsObjectType:
type: object
properties:
_offsets:
description: >
desc
type: object
properties:
self:
allOf:
- description: |
desc
- $ref: '#/components/schemas/PaginationOffsetType'
first:
allOf:
- description: |
desc
- $ref: '#/components/schemas/PaginationOffsetType'
previous:
allOf:
- description: >
desc
- $ref: '#/components/schemas/PaginationOffsetType'
next:
allOf:
- description: >
des
- $ref: '#/components/schemas/PaginationOffsetType'
required:
- first
- self
required:
- _offsets

What is wrong? What command can it do correctly without redundancy? Im lost with this :/

ok ... there is problematic location:
this:
image
generates this:
image

and this:
image
generates these redundant types:
image

@ReneZeidler
Copy link
Contributor

This was partially fixed by #16096, however the issue still persists if readOnly and/or nullable are used together with any other property (like description).

ReneZeidler pushed a commit to ReneZeidler/openapi-generator that referenced this issue Jun 17, 2024
Fixes OpenAPITools#15077

The previous fix for this in OpenAPITools#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.
ReneZeidler pushed a commit to ReneZeidler/openapi-generator that referenced this issue Jun 17, 2024
Fixes OpenAPITools#15077

The previous fix for this in OpenAPITools#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.
ReneZeidler added a commit to ReneZeidler/openapi-generator that referenced this issue Jun 19, 2024
Fixes OpenAPITools#15077

The previous fix for this in OpenAPITools#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.
wing328 pushed a commit that referenced this issue Jun 19, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants