fastapi-code-generator
fastapi-code-generator copied to clipboard
419 / Support for parsing callbacks
Fixes #419
Basic support for parsing callbacks as a list of sub-operations.
This does not modify the default templates to generate callbacks, but this can be done with something like the following:
...
{% for operation in operations %}
{%- if operation.callbacks -%}
# Callbacks for {{ operation.function_name }}, supplied here for documentation.
# See https://fastapi.tiangolo.com/advanced/openapi-callbacks/
{% for key, cb_ops in operation.callbacks.items() -%}
_cb_router_{{operation.function_name}} = APIRouter()
{% for cb_op in cb_ops %}
@_cb_router_{{operation.function_name}}.{{cb_op.method}}('{{cb_op.path}}', response_model={{cb_op.response}})
def {{cb_op.function_name}}({{cb_op.snake_case_arguments}}) -> {{cb_op.return_type}}:
{%- if cb_op.summary or cb_op.description %}
"""{{ cb_op.summary }}
{%- if cb_op.description %}
{{ "\n" }}
{{- cb_op.description -}}
{%- endif -%}
"""
{% endif -%}
pass
{% endfor -%}
{%- endfor -%}
{%- endif -%}
{% if operation.tags[0] == tag %}
@router.{{operation.type}}('{{operation.path}}', response_model={{operation.response}}
{% if operation.additional_responses %}
, responses={
{% for status_code, models in operation.additional_responses.items() %}
'{{ status_code }}': {
{% for key, model in models.items() %}
'{{ key }}': {{ model }}{% if not loop.last %},{% endif %}
{% endfor %}
}{% if not loop.last %},{% endif %}
{% endfor %}
}
{% endif %}
{% if operation.tags%}
, tags={{operation.tags}}
{% endif %}
{% if operation.callbacks %}
, callbacks=_cb_router_{{ operation.function_name }}.routes
{% endif %})
...