Skip to content

Bug: Generating invalid code in composed schema contexts #51

Closed
@jabrandes

Description

@jabrandes

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 };

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions