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] nullable: true is not respected with enum #17264

Closed
5 of 6 tasks
Naktibalda opened this issue Nov 30, 2023 · 1 comment · Fixed by #17265
Closed
5 of 6 tasks

[BUG] [typescript-axios] nullable: true is not respected with enum #17264

Naktibalda opened this issue Nov 30, 2023 · 1 comment · Fixed by #17265

Comments

@Naktibalda
Copy link
Contributor

Naktibalda commented Nov 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

We added a nullable enum attribute to request body of a function in our API. OpenAPI generator generates typescript-axios client code which omits information that the attribute is nullable, so the client must provide enum value which is undesirable, because we want to keep using null for that attribute by default and use enums only in special cases required by a new feature.

OpenApiSpecification:

components:
  schemas:
    SchemaName:
      type: object
      properties:
        field:
          type: string
          nullable: true
          enum: &a5
          - foo
          - bar
          - baz

Generated code:

interface SchemaName {
    /**
     * 
     * @type {string}
     * @memberof SchemaName
     */
    'objectType': FieldEnum;
}

What we actually want:

interface SchemaName {
    /**
     * 
     * @type {string}
     * @memberof SchemaName
     */
    'objectType': FieldEnum | null;
}
openapi-generator version

7.1.0

OpenAPI declaration file content or url
Generation Details
npx openapi-generator-cli generate --generator-key projectname
Related issues/PRs
Suggest a fix

I think that isNullable condition is missing inside #isEnum condition in

'{{baseName}}'{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};

at

'{{baseName}}'{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{#isNullable}} | null{{/isNullable}}{{/isEnum}};

@Naktibalda
Copy link
Contributor Author

It seems that I accidentally solved my problem by creating a reusable enum component instead of inlining it in different params components.

components:
  schemas:
    FieldEnum:
      type: string
      nullable: true
      enum:
      - foo
      - bar
      - baz

    SchemaName:
      field:
        $ref: "#/components/schemas/FieldEnum"

Generates correct code:

  'field': FieldEnum | null;

Naktibalda added a commit to Naktibalda/openapi-generator that referenced this issue Dec 6, 2023
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.

1 participant