Skip to content

fix: extract enums from -referenced schemas#137

Merged
zedrdave merged 2 commits intomainfrom
fix/ref-enum-extraction
Feb 27, 2026
Merged

fix: extract enums from -referenced schemas#137
zedrdave merged 2 commits intomainfrom
fix/ref-enum-extraction

Conversation

@zedrdave
Copy link
Copy Markdown
Member

@zedrdave zedrdave commented Feb 26, 2026

Fixes issue where CLI only extracted inline enums, not ``-referenced enums.

Changes

tests/fixtures/toy-openapi.json

  • Added new PetStatus schema component with enum values: ["available", "pending", "adopted"]
  • Updated Pet schema's status property to use : "#/components/schemas/PetStatus"
  • Updated NewPet schema's status property to use : "#/components/schemas/PetStatus"
  • Added status query parameter to /pets GET operation that references the PetStatus enum

tests/unit/cli.test.ts

  • Modified extractEnumsFromSpec() helper to support `` resolution (matching CLI implementation)
  • Removed early return to allow extraction from parameters without schemas
  • Removed inline test data, use actual fixture instead
  • Added test that verifies parameter enum extraction via sourcePath

src/cli.ts

  • Added `` support to OpenAPISchema interface
  • Updated parameter extraction to use resolveEnumValues() helper for both inline and referenced enums

Test Results

✅ All 342 tests pass
✅ Linting passes
✅ Type checking passes
✅ Format checking passes

Version

Bumped to 0.18.2

- Add PetStatus schema component to toy-openapi.json
- Update Pet/NewPet status properties to use  to PetStatus
- Add status query parameter to listPets operation
- Update extractEnumsFromSpec helper to support  resolution
- Remove inline test data, use actual fixture instead
- Remove redundant test case

Fixes issue where CLI only extracted inline enums, not -referenced enums.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes enum extraction in the CLI so enums are detected not only when declared inline, but also when a property/parameter schema is a $ref to an enum schema under components.schemas. This improves the generated api-enums.ts output for OpenAPI specs that factor enums into reusable schema components.

Changes:

  • Add $ref support to schema typing and resolve enum values from either inline enum or $ref-referenced enum schemas in the CLI.
  • Extend the toy OpenAPI fixture to include a shared PetStatus enum schema and reference it from schemas and a query parameter.
  • Update CLI unit tests to exercise $ref-based enum extraction logic.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/cli.ts Adds $ref to OpenAPISchema and updates enum extraction to resolve enum values from inline enums or $ref to enum schemas (including operation parameters).
tests/fixtures/toy-openapi.json Introduces PetStatus as a reusable enum schema and references it from Pet, NewPet, and a /pets query parameter.
tests/unit/cli.test.ts Updates the enum extraction test helper to resolve $ref enums and adds assertions intended to cover referenced schema/parameter extraction.

Comment on lines +640 to +647
if (!openApiSpec.components?.schemas) {
return enums
}
const toCase = (str: string, capitalize: boolean): string => {
// If already camelCase or PascalCase, just adjust first letter
if (/[a-z]/.test(str) && /[A-Z]/.test(str)) {
return capitalize ? str.charAt(0).toUpperCase() + str.slice(1) : str.charAt(0).toLowerCase() + str.slice(1)
}
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

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

The helper returns early when components.schemas is missing, which means it will never extract enums that exist only in operation parameters (inline param.schema.enum). The real CLI implementation can still extract inline parameter enums without schemas, so this helper no longer accurately mirrors CLI behavior. Consider removing the early return and letting schemaEnumLookup be empty when schemas are absent.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Addressed: removed early return and updated test to verify parameter enum extraction via sourcePath

- Add PetStatus schema component to toy-openapi.json
- Update Pet/NewPet status properties to use  to PetStatus
- Add status query parameter to listPets operation
- Update extractEnumsFromSpec helper to support  resolution
- Remove inline test data, use actual fixture instead
- Remove early return to allow extraction from parameters without schemas
- Add test that verifies parameter enum extraction via sourcePath

Fixes issue where CLI only extracted inline enums, not -referenced enums.

Bump version to 0.18.2
@zedrdave zedrdave force-pushed the fix/ref-enum-extraction branch from ffec4e3 to ea0a665 Compare February 27, 2026 12:13
@zedrdave zedrdave merged commit a4a9def into main Feb 27, 2026
3 checks passed
@zedrdave zedrdave deleted the fix/ref-enum-extraction branch February 27, 2026 12:14
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