Skip to content

Commit

Permalink
Fix openapi definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Dec 8, 2023
1 parent 4481f97 commit fe2cd67
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 5 deletions.
1 change: 1 addition & 0 deletions esmerald/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ def set_value(value: Any, name: str) -> Any:
set_value(self.openapi_version, "openapi_version")
set_value(self.summary, "summary")
set_value(self.description, "description")
set_value(self.contact, "contact")
set_value(self.tags, "tags")
set_value(self.servers, "servers")
set_value(self.terms_of_service, "terms_of_service")
Expand Down
3 changes: 2 additions & 1 deletion esmerald/config/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def openapi(self, app: Any) -> Dict[str, Any]:
webhooks=self.webhooks,
)
app.openapi_schema = openapi_schema
return openapi_schema
return app.openapi_schema

def enable(self, app: Any) -> None:
"""Enables the OpenAPI documentation"""
Expand All @@ -356,6 +356,7 @@ def enable(self, app: Any) -> None:
@get(path=self.openapi_url)
async def _openapi(request: Request) -> JSONResponse:
root_path = request.scope.get("root_path", "").rstrip("/")

if root_path not in server_urls:
if root_path and self.root_path_in_servers:
self.servers.insert(0, {"url": root_path})
Expand Down
5 changes: 3 additions & 2 deletions esmerald/openapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Union, cast

from orjson import loads
from pydantic import AnyUrl
from pydantic.fields import FieldInfo
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
Expand Down Expand Up @@ -546,5 +547,5 @@ def iterate_routes(
output["tags"] = tags

openapi = OpenAPI(**output)
model_dump = openapi.model_dump(by_alias=True, exclude_none=True)
return model_dump
model_dump = openapi.model_dump_json(by_alias=True, exclude_none=True)
return cast(Dict[str, Any], loads(model_dump))
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ dependencies = [
"openapi-schemas-pydantic>=3.0.0",
"orjson>=3.8.5,<4.0.0",
"msgspec>=0.18.4,<1.0.0",
"nest_asyncio>=1.5.8,<2.0.0",
"rich>=13.3.1,<14.0.0",
"starlette==0.32.0.post1",
"starlette>=0.28.0,<=0.32.0.post1",
]
keywords = [
"api",
Expand Down Expand Up @@ -101,7 +102,7 @@ test = [
"black==23.11.0",
"isort>=5.0.6,<6.0.0",
"aiofiles>=0.8.0,<24",
"a2wsgi>=1.7.0,<2",
"a2wsgi>=1.9.0,<2",
"asyncz>=0.5.0",
"anyio[trio]>=3.6.2,<5.0.0",
"asyncio[trio]>=3.4.3,<4.0.0",
Expand Down
66 changes: 66 additions & 0 deletions tests/openapi/test_contact.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from typing import Dict

from esmerald import Esmerald, Gateway, get
from esmerald.testclient import EsmeraldTestClient
from tests.settings import TestSettings


@get("/bar")
async def bar() -> Dict[str, str]:
return {"hello": "world"}


app = Esmerald(
contact={
"name": "API Support",
"url": "https://www.example.com",
"email": "[email protected]",
},
routes=[Gateway(handler=bar)],
enable_openapi=True,
settings_config=TestSettings,
)


client = EsmeraldTestClient(app)


def test_application(test_client_factory):
response = client.get("/bar")
assert response.status_code == 200, response.json()


def test_openapi_schema(test_client_factory):
response = client.get("/openapi.json")

assert response.status_code == 200, response.text
assert response.json() == {
"openapi": "3.1.0",
"info": {
"title": "Esmerald",
"summary": "Esmerald application",
"description": "Highly scalable, performant, easy to learn and for every application.",
"contact": {
"name": "API Support",
"url": "https://www.example.com/",
"email": "[email protected]",
},
"version": client.app.version,
},
"servers": [{"url": "/"}],
"paths": {
"/bar": {
"get": {
"summary": "Bar",
"operationId": "bar_bar_get",
"responses": {
"200": {
"description": "Successful response",
"content": {"application/json": {"schema": {"type": "string"}}},
}
},
"deprecated": False,
}
}
},
}

0 comments on commit fe2cd67

Please sign in to comment.