0.8.0
Breaking Changes
Code Generation Changes
- Non-200 success status codes now generate explicit
status_codeparameter - Endpoints whose OpenAPI spec defines only non-200 success responses (e.g.,201,202) now includestatus_code=201(or202, etc.) in the generated route decorator. Previously, only204no-content responses received an explicitstatus_code. This changes the generated output for any spec with201,202, or other 2xx-only responses, which will cause diffs when regenerating code. (#610) - Primary non-200 success response now uses
response_modelinstead ofresponsesdict - When an endpoint's only success response is a non-200 code (e.g.,201) with a response body schema, the generated code now usesresponse_model=<Model>andstatus_code=201instead of the previousresponse_model=None, responses={'201': {'model': Model}}. For example:This also changes the return type annotation from# Before @app.post('/subscriptions', response_model=None, responses={'201': {'model': Subscription}}) def create_subscription(body: SubscriptionRequest) -> Optional[Subscription]: # After @app.post('/subscriptions', response_model=Subscription, status_code=201) def create_subscription(body: SubscriptionRequest) -> Subscription:
Optional[Model]orOptional[Union[...]]toModelorUnion[...](removing theOptionalwrapper). Any code depending on the previously generated signatures, response handling, orOptionalreturn types will need updating. (#610) - Non-200 success responses now use
response_modelandstatus_codeinstead ofresponsesdict - Previously, endpoints with non-200 success status codes (e.g., 201) generatedresponse_model=Nonewith the model placed in theresponses={'201': {'model': Model}}dict. Now, the primary success response is promoted toresponse_model=Modelwith an explicitstatus_code=201. This changes the actual HTTP status codes returned by generated FastAPI applications (previously defaulted to 200, now returns the correct status code). Return type annotations also change fromOptional[Model]toModel. Existing generated code that relies on the oldresponsesdict structure or 200 status codes will behave differently after regeneration. (#611)
Before:After:@app.post('/subscriptions', response_model=None, responses={'201': {'model': Subscription}}) def create_subscription(body: SubscriptionRequest) -> Optional[Subscription]:
@app.post('/subscriptions', response_model=Subscription, status_code=201) def create_subscription(body: SubscriptionRequest) -> Subscription:
- Endpoints with non-200 success codes now emit explicit
status_codein decorators - Operations that define only a 201 (or other non-200) success response without a response body now generatestatus_code=201in the route decorator. Previously, nostatus_codewas emitted and FastAPI would default to 200. This affects POST, PUT, and other write endpoints across all templates (default and router). (#611)
Before:After:@app.post('/pets', response_model=None, tags=['pets'])@app.post('/pets', response_model=None, status_code=201, tags=['pets'])
Custom Template Update Required
operation.status_codeis now populated for more endpoints - Custom Jinja2 templates that referenceoperation.status_codewill now receive non-Nonevalues for endpoints with non-200 success status codes (e.g.,201,202), not just204. Templates that already handlestatus_code(like the built-in templates) will work correctly, but custom templates that assumedstatus_codewas only set for204may need review. (#610)operation.responseandoperation.additional_responsesmay differ - For specs where the primary success response is non-200,operation.responsenow contains the actual model type (e.g.,Subscription) instead ofNone, and the primary status code entry is removed fromoperation.additional_responses. Custom templates referencing these fields may produce different output. (#610)operation.status_codeis now set for all non-200 success status codes - Previously,operation.status_codewas only populated for 204 no-content responses. It is now set for any non-200 success status code (201, 202, etc.). Custom Jinja2 templates that use{% if operation.status_code %}will now renderstatus_code=for many more routes than before. Additionally,operation.responsenow contains the actual model name (e.g.,'Subscription') instead of'None'for non-200 success responses, andoperation.additional_responsesno longer includes the primary success response entry. (#611)
Python or Dependency Support Changes
- Minimum
datamodel-code-generatorversion raised from 0.59 to 0.61 - The dependency constraint changed from>=0.59,<0.60to>=0.61,<0.66. Users pinned todatamodel-code-generator0.59 or 0.60 must upgrade to at least 0.61. (#611)
What's Changed
- Fix 201 response generation by @koxudaxi in #610
- Update datamodel-code-generator requirement by @koxudaxi in #611
Full Changelog: 0.7.0...0.8.0