Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

419 / Support for parsing callbacks #420

Merged
merged 4 commits into from
Jul 2, 2024
Merged

Conversation

jnu
Copy link
Contributor

@jnu jnu commented May 14, 2024

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 %})

...

@codecov-commenter
Copy link

codecov-commenter commented Jul 2, 2024

Codecov Report

Attention: Patch coverage is 25.00000% with 15 lines in your changes missing coverage. Please review.

Project coverage is 89.78%. Comparing base (cc62b44) to head (82f6b87).
Report is 1 commits behind head on master.

Files Patch % Lines
fastapi_code_generator/parser.py 25.00% 15 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #420      +/-   ##
==========================================
- Coverage   93.01%   89.78%   -3.24%     
==========================================
  Files           5        5              
  Lines         401      421      +20     
==========================================
+ Hits          373      378       +5     
- Misses         28       43      +15     
Flag Coverage Δ
unittests 89.78% <25.00%> (-3.24%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@koxudaxi koxudaxi merged commit 06f0d2b into koxudaxi:master Jul 2, 2024
17 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: support for callbacks
3 participants