Does FastAPI document Pydantic extra fields in OpenAPI request schemas?
0 reputation · 31 Oct 2023, 06:05 UTC
0 reputation · 31 Oct 2023, 06:05 UTC
Goal: Determine whether the OpenAPI schema generated by a FastAPI endpoint should list request-body fields that Pydantic accepts at runtime when extra='allow' is set on the model.
Constraint: FastAPI 0.100+ typically uses Pydantic v2, which rejects undeclared extra fields by default unless the model declares extra='allow'. When extra='allow' is enabled, the endpoint may accept JSON properties not defined in the model, but the generated OpenAPI request schema might not document those properties. This creates a potential mismatch between runtime validation and API documentation.
Assumption: The application runs on FastAPI 0.100 or later with Pydantic v2 active, and no dependency overrides replace the default body dependency.
extra='allow', the runtime accepts any JSON property not explicitly declared, but the generated OpenAPI schema does not list those extra fields by name. Instead, the schema includes the keyword additionalProperties: true within the requestBody's JSON schema, signaling that undeclared properties are permitted. In FastAPI 0.100+ with Pydantic v2, setting extra='allow' on a model causes the OpenAPI schema to render additionalProperties: true. Conversely, extra='forbid' or the default extra='ignore' results in additionalProperties: false (or its omission), producing a stricter schema that rejects unexpected properties at validation time. The OpenAPI specification uses additionalProperties to describe whether a schema accepts properties beyond those defined. FastAPI maps Pydantic's extra behavior directly to this keyword, but it does not enumerate which extra fields are expected or permitted. Therefore, the schema conveys permission, not enumeration. When should a team rely on the model class definition instead of the generated schema for client-side validation? Always prefer the Pydantic model. The OpenAPI schema is a documentation and client-generation tool; it indicates whether extra fields are allowed, but it does not validate or list them. Client code that depends on the schema alone may miss required extra fields or incorrectly assume fields are absent. 1. Define your Pydantic model with the desired extra='allow' setting: class Item(BaseModel): name: str; extra_data: dict = Field(default_factory=dict, extra='allow'). 2. Generate or inspect the OpenAPI JSON for the endpoint (e.g., /openapi.json) and locate the requestBody schema. 3. Check the schema for additionalProperties: true (extra allowed) or additionalProperties: false (extra rejected). 4. Test the endpoint with an undeclared JSON property at runtime to confirm that Pydantic's validation behaves as the schema suggests. To pinpoint the exact behavior for your setup, verify whether your Pydantic model explicitly declares extra='allow' (or the alternative) and whether any custom Body or Depends overrides the default schema generation. This detail determines whether the schema will surface additionalProperties: true or a stricter constraint.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.