Skip to content

Conversation

@sentik
Copy link
Contributor

@sentik sentik commented Oct 29, 2025

Support propertyNames enum constraint (OpenAPI 3.1)

Problem

OpenAPI 3.1 allows constraining object property names via propertyNames with enum values. Orval generated the [key: string] index signature instead of a union like [key: 'foo' | 'bar'].

Before:

export interface MyObject {
  [key: string]: string;
}

Solution

  • Detect propertyNames with enum values
  • Generate a union type for the index signature key

After:

export interface MyObject {
  [key: 'foo' | 'bar']: string;
}

Changes

  • Added getPropertyNamesEnum() to extract enum values
  • Added getIndexSignatureKey() to format keys
  • Updated getObject() index signature generation
  • Added tests

Testing

  • propertyNames with enum
  • Boolean additionalProperties with enum
  • Enum with specific types
  • Fallback to string when no enum
  • Works with existing properties and enum together

All tests pass.

Related

Fixes #2196

…enAPI 3.1

Generate union type for index signature keys when propertyNames
has enum values, providing better type safety in TypeScript.
Example:
Before: [key: string]: string
After: [key: 'foo' | 'bar']: string
Added helper functions to extract and format enum values from
propertyNames schema constraint. Updated all index signature
generation points in getObject function.
@melloware melloware added the openapi_31 OpenAPI 3.1 related issue label Oct 29, 2025
@melloware melloware merged commit 42f7e28 into orval-labs:master Oct 29, 2025
0 of 2 checks passed
melloware added a commit that referenced this pull request Oct 30, 2025
* feature #2196: support propertyNames enum constraint for OpenAPI 3.1
Generate union type for index signature keys when propertyNames
has enum values, providing better type safety in TypeScript.
Example:
Before: [key: string]: string
After: [key: 'foo' | 'bar']: string
Added helper functions to extract and format enum values from
propertyNames schema constraint. Updated all index signature
generation points in getObject function.

* generateInterface fix

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

Labels

openapi_31 OpenAPI 3.1 related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support enum propertyNames

2 participants