fix: extract enums from -referenced schemas#137
Conversation
- 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.
There was a problem hiding this comment.
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
$refsupport to schema typing and resolve enum values from either inlineenumor$ref-referenced enum schemas in the CLI. - Extend the toy OpenAPI fixture to include a shared
PetStatusenum 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. |
tests/unit/cli.test.ts
Outdated
| 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) | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
ffec4e3 to
ea0a665
Compare
Fixes issue where CLI only extracted inline enums, not ``-referenced enums.
Changes
tests/fixtures/toy-openapi.jsonPetStatusschema component with enum values:["available", "pending", "adopted"]Petschema'sstatusproperty to use: "#/components/schemas/PetStatus"NewPetschema'sstatusproperty to use: "#/components/schemas/PetStatus"statusquery parameter to/petsGET operation that references the PetStatus enumtests/unit/cli.test.tsextractEnumsFromSpec()helper to support `` resolution (matching CLI implementation)sourcePathsrc/cli.tsOpenAPISchemainterfaceresolveEnumValues()helper for both inline and referenced enumsTest Results
✅ All 342 tests pass
✅ Linting passes
✅ Type checking passes
✅ Format checking passes
Version
Bumped to 0.18.2