Support for propertyNames in OpenAPI schema when using dict[Enum, ...] with Pydantic v2
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:
{ "type": "object", "propertyNames": { "type": "string", "enum": ["foo", "bar"] }, "additionalProperties": { "type": "integer" } }
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 Behavior
FastAPI 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 Reproduce
from enum import StrEnum from pydantic import RootModel from fastapi import FastAPI class MyEnum(StrEnum): foo = "foo" bar = "bar" class MyModel(RootModel[dict[MyEnum, int]]): pass app = FastAPI() @app.post("/test") def test_endpoint(model: MyModel): return model
Resulting OpenAPI Schema:
{ "type": "object", "additionalProperties": { "type": "integer" } }
Expected Schema (from Pydantic v2.11+):
{ "type": "object", "propertyNames": { "type": "string", "enum": ["foo", "bar"] }, "additionalProperties": { "type": "integer" } }
Environment
- FastAPI version: latest
- Pydantic version: 2.11.x+
- Python version: 3.10+
- Swagger UI version: [auto-generated by FastAPI]
Support for propertyNames in OpenAPI schema when using dict[Enum, ...] with Pydantic v2
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:
{ "type": "object", "propertyNames": { "type": "string", "enum": ["foo", "bar"] }, "additionalProperties": { "type": "integer" } }
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 Behavior
FastAPI 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 Reproduce
from enum import StrEnum from pydantic import RootModel from fastapi import FastAPI class MyEnum(StrEnum): foo = "foo" bar = "bar" class MyModel(RootModel[dict[MyEnum, int]]): pass app = FastAPI() @app.post("/test") def test_endpoint(model: MyModel): return model
Resulting OpenAPI Schema:
{ "type": "object", "additionalProperties": { "type": "integer" } }
Expected Schema (from Pydantic v2.11+):
{ "type": "object", "propertyNames": { "type": "string", "enum": ["foo", "bar"] }, "additionalProperties": { "type": "integer" } }
Environment
- FastAPI version: latest
- Pydantic version: 2.11.x+
- Python version: 3.10+
- Swagger UI version: [auto-generated by FastAPI]