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: Generating invalid code in composed schema contexts #51

Closed
jabrandes opened this issue Jun 11, 2020 · 4 comments
Closed

Bug: Generating invalid code in composed schema contexts #51

jabrandes opened this issue Jun 11, 2020 · 4 comments
Assignees
Labels
bug Something isn't working next release

Comments

@jabrandes
Copy link

jabrandes commented Jun 11, 2020

An invalid Typescript type is generated in contexts where all of the following criteria are met:

  • The schema is a composed schema with an allOf specification
  • The allOf contains at least two type specifications
  • One of the allOf types contains an array property of enum values
  • That array contains an enum value with a hyphen (-)

I suspect the issue to be the following code snippet:

if (content.includes(" & ")) {
return content.split(" & ").map(checkAndRenameModelName).join(" & ");
}
if (content.includes(" | ")) {
return content.split(" | ").map(checkAndRenameModelName).join(" | ");
}

Since checkAndRenameModelName will find a hyphen in the described situation, Lodash's startCase function and whitespace removal is executed on the whole subtype, causing distortions in the output as shown below (undesired casing, missing punctuation and missing spacing). It might be a sufficient fix to just remove the checkAndRenameModelName there.

Swagger specification for reproduction

openapi: 3.0.1
info:
  title: Test
  version: test
paths: {}
components:
  schemas:
    Test:
      type: object
      allOf:
        - type: object
          properties:
            x:
              type: array
              items:
                type: string
                enum:
                  - A-B
        - type: object
          properties:
            y:
              type: string

Generated code

export type Test = XAB & { y?: string };
@jabrandes jabrandes changed the title Bug: Corner-Case generating invalid code in polymorphic contexts Bug: Generating invalid code in composed schema contexts Jun 11, 2020
@js2me
Copy link
Member

js2me commented Jun 12, 2020

Hello @jabrandes !
Thanks for this issue, it will be fixed at the next release!

@js2me js2me self-assigned this Jun 12, 2020
@js2me js2me added bug Something isn't working next release labels Jun 12, 2020
@js2me
Copy link
Member

js2me commented Jun 13, 2020

@jabrandes the generated type should look like ?

export type Test = { x: "A-B" } & { y?: string };

@jabrandes
Copy link
Author

jabrandes commented Jun 13, 2020

Like

export ​type​ ​Test​ ​=​ ​{​ ​x?: (​"A-B"​ )[]}​ & ​{​ ​y​?: ​string​ ​}​;

I'm not sure if I got the nullability of x right, but x should be an array/subset of enum values.

@js2me js2me added next major release This changes of code will added at next major release (*.0.0) next release and removed next release next major release This changes of code will added at next major release (*.0.0) labels Jun 14, 2020
@js2me js2me mentioned this issue Jun 14, 2020
@js2me
Copy link
Member

js2me commented Jun 14, 2020

It should be fixed in release 1.8.3 🙂

@js2me js2me closed this as completed Jun 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working next release
Projects
None yet
Development

No branches or pull requests

2 participants