alexschimpf / fastapi-versionizer

FastAPI Versionizer
MIT License
81 stars 13 forks source link

feat: Added OpenAPI metadata to version docs (#38) #39

Closed worldworm closed 1 year ago

worldworm commented 1 year ago

Pull Request Checklist

Description

closes #38

My first try looked like this:

            async def get_openapi() -> Any:
                openapi_args = {
                    'title': title,
                    'version': version_str,
                    'routes': router.routes,
                    'description': self._app.description,
                    'terms_of_service': self._app.terms_of_service,
                    'contact': self._app.contact,
                    'license_info': self._app.license_info,
                }
                if hasattr(self._app, 'summary'):
                    # Available since OpenAPI 3.1.0, FastAPI 0.99.0
                    openapi_args['summary'] = self._app.summary
                return fastapi.openapi.utils.get_openapi(**openapi_args)

But mypy didn't like that:

fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "str"  [arg-type]
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "str | None"  [arg-type]
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "Sequence[BaseRoute]"  [arg-type]
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "Sequence[BaseRoute] | None"  [arg-type]
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "list[dict[str, Any]] | None"  [arg-type]
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "list[dict[str, str | Any]] | None"  [arg-type]
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "dict[str, str | Any] | None"  [arg-type]
fastapi_versionizer/versionizer.py:231: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
fastapi_versionizer/versionizer.py:231: note: Consider using "Mapping" instead, which is covariant in the value type
fastapi_versionizer/versionizer.py:231: error: Argument 1 to "get_openapi" has incompatible type "**dict[str, Sequence[object] | dict[str, str | Any] | None]"; expected "bool"  [arg-type]
Found 8 errors in 1 file (checked 9 source files)

So this solution is a bit easier. I hope you still like it. 😃

alexschimpf commented 1 year ago

@worldworm Sorry, one last request. Can you update the "simple" test/example to include some of these fields so we can make sure this case has proper test coverage?

worldworm commented 1 year ago

Sure, that's a good idea. I have added description and termsOfService. I think they look better than a license_info or your name as a contact. 😄 I thought about checking the summary attribute, but having a version dependency in the test case doesn't look "simple.py" to me. Hope you are fine with my pick.

alexschimpf commented 1 year ago

:tada: This PR is included in version 2.1.4 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: