Support for propertyNames in OpenAPI schema when using dict[Enum, ...] with Pydantic v2 #14513
-
|
When using dict[StrEnum, ...] or RootModel[dict[StrEnum, ...]] in a FastAPI app with Pydantic v2, the generated OpenAPI schema does not enumerate the enum values used as dictionary keys. Instead, the UI shows [any-key], which does not reflect the actual constraints. As of Pydantic v2.11+, support for emitting propertyNames with enum constraints was added via PR #10478. This allows OpenAPI/JSON Schema to correctly express restrictions on object keys, such as: However, FastAPI currently does not appear to consume or propagate the propertyNames node from Pydantic’s schema output, so these constraints are missing in the OpenAPI spec and Swagger UI. Expected BehaviorFastAPI should correctly include propertyNames in the generated OpenAPI schema when dict[Enum, T] is used as a request/response model with Pydantic v2. This would allow Swagger UI to better reflect the allowed dictionary keys. To ReproduceResulting OpenAPI Schema:Expected Schema (from Pydantic v2.11+):Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 11 comments
-
|
Hi! I'm trying to reproduce this behavior using: Pydantic version: 2.11.7 Python version: 3.11.13 Here’s the relevant snippet from the generated OpenAPI schema: "MyEnum": {
"type": "string",
"enum": ["foo", "bar"],
"title": "MyEnum"
},
"MyModel": {
"additionalProperties": {
"type": "integer"
},
"propertyNames": {
"$ref": "#/components/schemas/MyEnum"
},
"type": "object",
"title": "MyModel"
}Is this the expected output? Or am I missing something? Apologies if this is a basic question — still getting familiar with how all of this fits together. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
I checked it and on current FastAPI (1.116.1) and Pydantic (2.11.7) versions it gives me schema with Swagger shows schema correctly, but struggles to create correct example and doesn't validate input on UI side:
So, I think this is not an issue on FastAPI's side and we should close it. @andrzejdoros, could you please check it? |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
Is this issue still open? I have been using FastAPI for a year now and would love to get some exposure to open-source contributions! Would love to take this issue if it is still open. |
Beta Was this translation helpful? Give feedback.
-
|
I have the exact same issue with current FastAPI, Starlette and Pydantic versions. Dowgrading brings known security vulnerabilities unfortunately. |
Beta Was this translation helpful? Give feedback.
-
I still can't reproduce the issue. "MyEnum": {
"type": "string",
"enum": [
"foo",
"bar"
],
"title": "MyEnum"
},
"MyModel": {
"additionalProperties": {
"type": "integer"
},
"propertyNames": {
"$ref": "#/components/schemas/MyEnum"
},
"type": "object",
"title": "MyModel"
},Full openapi schema{
"openapi": "3.1.0",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/test": {
"post": {
"summary": "Test Endpoint",
"operationId": "test_endpoint_test_post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyModel"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
}
}
}
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError"
},
"type": "array",
"title": "Detail"
}
},
"type": "object",
"title": "HTTPValidationError"
},
"MyEnum": {
"type": "string",
"enum": [
"foo",
"bar"
],
"title": "MyEnum"
},
"MyModel": {
"additionalProperties": {
"type": "integer"
},
"propertyNames": {
"$ref": "#/components/schemas/MyEnum"
},
"type": "object",
"title": "MyModel"
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
},
"type": "array",
"title": "Location"
},
"msg": {
"type": "string",
"title": "Message"
},
"type": {
"type": "string",
"title": "Error Type"
}
},
"type": "object",
"required": [
"loc",
"msg",
"type"
],
"title": "ValidationError"
}
}
}
}@ilgatto88, @andrzejdoros , could you please share the full schema you have (which you think is wrong) and exact versions of FastAPI and Pydantic you are using to generate it? |
Beta Was this translation helpful? Give feedback.
-
|
It seems this was (and should have been) a question, so I'll move it to GitHub Discussions. |
Beta Was this translation helpful? Give feedback.


I still can't reproduce the issue.
With latest FastAPI (0.120.2) and Pydantic (2.12.3) it gives me:
Full openapi schema
{ "openapi": "3.1.0", "info": { "title": "