Skip to content

Commit

Permalink
Add an test to ensure examples are present in the OpenAPI output
Browse files Browse the repository at this point in the history
This is currently only possible with Pydantic BaseModels see
jcrist/msgspec#689.
  • Loading branch information
pgjones committed May 16, 2024
1 parent de59ee6 commit 93f2620
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/test_openapi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict, List, Optional, Tuple, Type

import pytest
from pydantic import BaseModel, computed_field, Field
from pydantic import BaseModel, computed_field, ConfigDict, Field
from pydantic.dataclasses import dataclass
from quart import Quart

Expand Down Expand Up @@ -301,3 +301,29 @@ async def index() -> EmployeeWithComputedField:
assert "firstName" in response_properties
assert "lastName" in response_properties
assert "fullName" in response_properties


class Example(BaseModel):
a: str

model_config = ConfigDict(json_schema_extra={"examples": [{"a": "Foo"}]})


async def test_model_with_example() -> None:
app = Quart(__name__)
QuartSchema(app, convert_casing=True)

@app.post("/")
@validate_request(Example)
async def index(data: Example) -> str:
return ""

test_client = app.test_client()
response = await test_client.get("/openapi.json")
schema = await response.get_json()

properties = schema["paths"]["/"]["post"]["requestBody"]["content"]["application/json"][
"schema"
]

assert properties["examples"] == [{"a": "Foo"}]

0 comments on commit 93f2620

Please sign in to comment.