Skip to content

[BUG][typescript-fetch] not: enum constraints are ignored by generated type guards #25092

Description

@AndreyVMarkelov

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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

The typescript-fetch generator does not enforce a string property's not: { enum: [...] } constraint in its generated type guard. The generated instanceOfOther guard accepts both the excluded value "known" and an allowed value such as "other". This makes the guards for the Known and Other variants overlap even though their schemas are intended to be mutually exclusive.

openapi-generator version

Reproduced with a local 7.26.0-SNAPSHOT build. The unmodified source baseline was upstream commit 271e4dd6e6d03ee456d8acef6542617371ef827c. The latest master has not been retested separately.

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: TypeScript not enum reproduction
  version: 1.0.0
paths: {}
components:
  schemas:
    Known:
      type: object
      required: [kind]
      properties:
        kind:
          type: string
          enum: [known]

    Other:
      type: object
      required: [kind]
      properties:
        kind:
          type: string
          not:
            enum: [known]

    TaggedUnion:
      oneOf:
        - $ref: '#/components/schemas/Known'
        - $ref: '#/components/schemas/Other'

The spec passes openapi-generator-cli validate with no validation errors. The CLI reports only recommendations that the three models are unused because the spec has no paths. All three model files are generated with the default typescript-fetch options.

Generation Details

Generator: typescript-fetch, with default options.

openapi-generator-cli generate \
  -i typescript-not-enum.yaml \
  -g typescript-fetch \
  -o /tmp/typescript-not-enum
Steps to reproduce
  1. Save the spec above as typescript-not-enum.yaml and generate the client.
  2. Import instanceOfOther from the generated models/Other.ts and evaluate it with { kind: "known" } and { kind: "other" }.

Actual behavior:

instanceOfOther({ kind: "known" }) -> true
instanceOfOther({ kind: "other" }) -> true

Expected behavior:

instanceOfOther({ kind: "known" }) -> false
instanceOfOther({ kind: "other" }) -> true

"known" is explicitly excluded by not: { enum: [known] }. With Known first in this reproduction, TaggedUnion resolution may choose Known before testing Other, so this example does not necessarily throw a multiple-match error. The incorrect Other guard is independently reproducible.

Related issues/PRs

Related Go report: #25090. Related Go fix: #25089. Related Java report: #25065. Related Python report: #25069.

Suggest a fix

Preserve and enforce the nested not.enum constraint when typescript-fetch generates type guards for object properties.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions