Skip to content

Enum generation descriptions - allow for nulls/empty strings #2393

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

Merged
merged 5 commits into from
Aug 11, 2025

Conversation

simon-curtis
Copy link
Contributor

@simon-curtis simon-curtis commented Aug 8, 2025

Changes

Expands the condition for skipping enum descriptions to also ignore null and empty strings.

Details

Since NSwag.AspNetCore v14.5, it seems that a descriptions array is always generated for enums, with null values for any undefined entries. This appears to be intended to maintain positional alignment with enum values, and there’s no built-in way to override it due to being a private field. This change ensures such null or empty entries are not appended.

I can't see anything in the spec that would suggest that null is an invalid value for a description but at the moment you get a null reference exception:

file:///D:/git/cma/main/Technical/SourceCode/TestHarness%202.0/TestHarness.UI/node_modules/openapi-typescript/dist/lib/ts.mjs:271
    " ".concat(metadata.description.trim()),
                                    ^

TypeError: Cannot read properties of null (reading 'trim')
    at tsEnumMember (file:///D:/git/cma/main/Technical/SourceCode/TestHarness%202.0/TestHarness.UI/node_modules/openapi-typescript/dist/lib/ts.mjs:271:37)

How to Review

Give it an openapi spec like this and check that the process runs to completion with no errors and that no descriptions are generated.

{
  "schemas": {
    "ProjectType": {
      "type": "integer",
      "description": "",
      "x-enumNames": [
        "Normal",
        "Snapshot"
      ],
      "x-enum-descriptions": [
        null,
        null,
      ],
      "enum": [
        0,
        1
      ]
    }
  }
}

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@simon-curtis simon-curtis requested a review from a team as a code owner August 8, 2025 17:02
@simon-curtis simon-curtis requested a review from drwpow August 8, 2025 17:02
Copy link

netlify bot commented Aug 8, 2025

Deploy Preview for openapi-ts canceled.

Name Link
🔨 Latest commit e086728
🔍 Latest deploy log https://app.netlify.com/projects/openapi-ts/deploys/6899a3a6583ddc00091ea5d9

Copy link

changeset-bot bot commented Aug 8, 2025

🦋 Changeset detected

Latest commit: e086728

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
openapi-typescript Patch
swr-openapi Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@drwpow
Copy link
Contributor

drwpow commented Aug 11, 2025

This fix looks good! Could you add a patch changeset? We could merge & release with that!

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@drwpow drwpow merged commit 6943ccf into openapi-ts:main Aug 11, 2025
11 checks passed
@openapi-ts-bot openapi-ts-bot mentioned this pull request Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants