0.7.0
Breaking Changes
Code Generation Changes
- Shadowed imports no longer use aliases - Generated code that previously aliased conflicting imports (e.g.,
from datetime import date as date_aliased) now uses the direct type reference (e.g.,from datetime import date). Function signatures also change accordingly (e.g.,date_aliased→date). Users who regenerate code may see different output, and downstream code referencing aliased names will need updating. (#589) - 204 no-content endpoints now include explicit
status_code=204- Previously, endpoints with only a 204 response in the OpenAPI spec generated without astatus_codeparameter (defaulting to HTTP 200 at runtime). Now the generated code includesstatus_code=204in the route decorator. Users who regenerate code from specs containing 204 responses will see different output and may need to update snapshots or tests that compare generated code. (#601) - Removed automatic
status_code=204from generated route decorators - Previously, when an endpoint had a 204 response (with no 200 response), the generated code includedstatus_code=204in the@app.post(...)decorator. This parameter is no longer emitted, changing generated output for any spec using 204 No Content responses. (#595) - Header and cookie parameters now use FastAPI dependency injection - Parameters with
in: headerorin: cookiein OpenAPI specs are now generated withHeader(...)andCookie(...)wrappers instead of plain function parameters. Regenerating code from specs that include header or cookie parameters will produce different output. For example, a header parametertrace_idpreviously generated astrace_id: strwill now generate astrace_id: str = Header(..., alias='trace_id'). This also adds newfrom fastapi import Header, Cookieimports to generated files. (#602) - Non-200 success status codes now included in route decorators - Operations whose only success response is a non-200 code (e.g., 204 No Content) now generate
status_code=204in the@app.get(...)decorator. Previously this was omitted. Regenerated code for such endpoints will differ. (#602) - Vendor extensions filtered from info block - Keys starting with
x-in the OpenAPIinfoobject are now stripped from the generatedFastAPI()constructor arguments. Previously these were passed through, which could cause runtime errors in FastAPI. (#602)
Python or Dependency Support Changes
- datamodel-code-generator minimum version raised from 0.56.1 to 0.59 - The required version of
datamodel-code-generator[http]changed from>=0.56.1,<0.57to>=0.59,<0.60, dropping support for versions 0.56.x through 0.58.x. Users pinned to older versions of datamodel-code-generator will need to upgrade. (#589)
Custom Template Update Required
- New
operation.status_codefield available in templates - A newstatus_codeattribute (Optional[int]) has been added to theOperationmodel passed to Jinja2 templates. Users with custom templates who want correct 204 handling should add the following block afterresponse_model={{operation.response}}:
{% if operation.status_code %}
, status_code={{operation.status_code}}
{% endif %}Existing custom templates will continue to work without this addition but will not emit the status_code parameter for no-content endpoints. (#601)
- Removed
operation.status_codefield from the Operation model - Custom Jinja2 templates referencingoperation.status_code(e.g.,{% if operation.status_code %}, status_code={{operation.status_code}}{% endif %}) must remove those references, as the field no longer exists on the Operation class. (#595) - Default templates now include
status_codeconditional - The built-inmain.jinja2androuters.jinja2templates now emitstatus_code={{operation.status_code}}when set. Users with custom copies of these templates should add the following block after theresponse_model=line to get equivalent behavior:Custom templates that do not add this block will still work but will not emit status codes for 204 endpoints. (#602){% if operation.status_code %} , status_code={{operation.status_code}} {% endif %}
What's Changed
- Fix release followup workflows by @koxudaxi in #576
- Backfill changelog from releases by @koxudaxi in #577
- Support datamodel-code-generator 0.59 by @koxudaxi in #589
- Fix root endpoint path generation by @koxudaxi in #590
- Add OpenAI OpenAPI regression coverage by @koxudaxi in #592
- Fix union body imports by @koxudaxi in #597
- Add router tag sort regression coverage by @koxudaxi in #593
- Add invalid parameter name regression coverage by @koxudaxi in #591
- Add Python 3.10 install smoke test by @koxudaxi in #599
- Fix router tag template rendering by @koxudaxi in #598
- Fix no-content status code generation by @koxudaxi in #601
- Add Field examples regression coverage by @koxudaxi in #594
- Fix vendor extension info fields by @koxudaxi in #600
- Add missing request schema regression coverage by @koxudaxi in #595
- Add formatter pyproject regression coverage by @koxudaxi in #596
- Fix header and cookie parameter generation by @koxudaxi in #602
Full Changelog: 0.6.0...0.7.0