Add optional OpenAPI YAML view#766
Conversation
| from server.apps.openapi.config import get_config | ||
|
|
||
| try: | ||
| from dmr.openapi.views import OpenAPIYamlView |
There was a problem hiding this comment.
It should always be available in our test app
|
|
||
| def yaml_dumps(schema: object) -> str: | ||
| """Serialize schema to a decoded YAML string.""" | ||
| return _yaml_dumps(schema) |
There was a problem hiding this comment.
do we need _yaml_dumps? can we just directly use msgspec?
| with suppress(ImportError): | ||
| from dmr.openapi.views.yaml import OpenAPIYamlView as OpenAPIYamlView |
There was a problem hiding this comment.
| with suppress(ImportError): | |
| from dmr.openapi.views.yaml import OpenAPIYamlView as OpenAPIYamlView |
do not import it here, if it needs an expra dependency
| ] | ||
|
|
||
| # openapi: {"openapi_url": "/docs/openapi.json/", "use_urlpatterns": true} # noqa: ERA001 | ||
| # openapi: {"openapi_url": "/docs/openapi.yaml/", "use_urlpatterns": true} # noqa: ERA001, E501 |
kondratevdev
left a comment
There was a problem hiding this comment.
Please make sure that you have checked all the examples and documentation accurately.
| path('docs/stoplight/', StoplightView.as_view(schema), name='stoplight'), | ||
| ] | ||
|
|
||
| urlpatterns.append( |
There was a problem hiding this comment.
Please, move it to previous urlpatterns section
urlpatterns = [
path(router.prefix, include((router.urls, 'server'), namespace='api')),
path('docs/openapi.json/', OpenAPIJsonView.as_view(schema), name='openapi'),
path('docs/redoc/', RedocView.as_view(schema), name='redoc'),
path('docs/scalar/', ScalarView.as_view(schema), name='scalar'),
path('docs/swagger/', SwaggerView.as_view(schema), name='swagger'),
path('docs/stoplight/', StoplightView.as_view(schema), name='stoplight'),
path('docs/openapi.yaml/', OpenAPIYamlView.as_view(schema), name='openapi_yaml')
]| path( | ||
| 'docs/openapi.yaml/', | ||
| OpenAPIYamlView.as_view(schema), | ||
| name='openapi-yaml', | ||
| ), | ||
| ) | ||
|
|
There was a problem hiding this comment.
| path( | |
| 'docs/openapi.yaml/', | |
| OpenAPIYamlView.as_view(schema), | |
| name='openapi-yaml', | |
| ), | |
| ) |
| @@ -0,0 +1,5 @@ | |||
| import msgspec | |||
There was a problem hiding this comment.
@sobolevn I think we should move it to plugins/msgspec folder, because we need try: ... except: condition for msgspec import
| @@ -0,0 +1,5 @@ | |||
| import msgspec | |||
|
|
|||
| def yaml_dumps(schema: object) -> str: | |||
There was a problem hiding this comment.
| def yaml_dumps(schema: object) -> str: | |
| def yaml_dumps(schema: 'ConvertedSchema') -> 'DumpedSchema': |
| This view mirrors :class:`~dmr.openapi.views.json.OpenAPIJsonView`, | ||
| but renders the converted schema using ``msgspec.yaml``. |
There was a problem hiding this comment.
| This view mirrors :class:`~dmr.openapi.views.json.OpenAPIJsonView`, | |
| but renders the converted schema using ``msgspec.yaml``. | |
| Produces a YAML representation of the :class:`~dmr.openapi.objects.OpenAPI` | |
| specification that can be used by API documentation tools | |
| and client code generators. |
| from dmr.test import DMRClient | ||
|
|
||
| try: | ||
| from dmr.openapi.views.yaml import OpenAPIYamlView |
There was a problem hiding this comment.
You must check yaml_dumps import, not view
There was a problem hiding this comment.
| def test_json_returns_correct_structure(dmr_client: DMRClient) -> None: |
| OpenAPIYamlView is None, | ||
| reason='`msgspec` is required for OpenAPI YAML endpoint', | ||
| ) | ||
| def test_yaml_endpoint_returns_correct_structure(dmr_client: DMRClient) -> None: |
There was a problem hiding this comment.
| def test_yaml_endpoint_returns_correct_structure(dmr_client: DMRClient) -> None: | |
| def test_yaml_returns_correct_structure(dmr_client: DMRClient) -> None: |
| assert OpenAPIYamlView is not None | ||
|
|
There was a problem hiding this comment.
| assert OpenAPIYamlView is not None |
| from server.apps.models_example import urls as models_example_urls | ||
| from server.apps.negotiations import urls as negotiations_urls | ||
| from server.apps.openapi.config import get_config | ||
| from dmr.openapi.views.yaml import OpenAPIYamlView |
There was a problem hiding this comment.
| from dmr.openapi.views.yaml import OpenAPIYamlView | |
| from dmr.openapi.views import OpenAPIYamlView |
You need to make re-export (check __init__.py
| StoplightView, | ||
| SwaggerView, | ||
| ) | ||
| from dmr.openapi.views.yaml import OpenAPIYamlView |
There was a problem hiding this comment.
| from dmr.openapi.views.yaml import OpenAPIYamlView | |
| from dmr.openapi.views import OpenAPIYamlView |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #766 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 333 334 +1
Lines 13085 13111 +26
Branches 413 413
=========================================
+ Hits 13085 13111 +26 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@zeel2104 thanks a lot! Great work! I fixed some minor things and merged it. |
I have made things!
AI Policy
and I agree to follow it
if any AI / LLM / coding agent was participating in the making of this PR
and I have carefully manually reviewed the final result produced by the AI
Checklist
Related issues
openapi.yaml#745Summary
This PR adds support for serving the OpenAPI schema as
openapi.yaml.The new YAML view mirrors the existing
openapi.jsonbehavior and is onlyavailable when
msgspecis installed, usingmsgspec.yamlto serialize theschema.
Changes
OpenAPIYamlViewmsgspec.yaml.encodemsgspecis installedopenapi.yamlroute in the test app conditionallyopenapi.yamlTesting