Skip to content

Commit

Permalink
πŸ“ Update source examples to use new JSON Schema examples field (#9776)
Browse files Browse the repository at this point in the history
* πŸ“ Update source examples to use new JSON Schema examples field

* βœ… Update tests for JSON Schema examples

* πŸ“ Update highlights in JSON Schema examples
  • Loading branch information
tiangolo authored Jul 1, 2023
1 parent 983f1d3 commit 4d83f98
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 190 deletions.
10 changes: 5 additions & 5 deletions docs/en/docs/tutorial/schema-extra-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ You can of course also pass multiple `examples`:

=== "Python 3.10+"

```Python hl_lines="23-49"
```Python hl_lines="23-38"
{!> ../../../docs_src/schema_extra_example/tutorial004_an_py310.py!}
```

=== "Python 3.9+"

```Python hl_lines="23-49"
```Python hl_lines="23-38"
{!> ../../../docs_src/schema_extra_example/tutorial004_an_py39.py!}
```

=== "Python 3.6+"

```Python hl_lines="24-50"
```Python hl_lines="24-39"
{!> ../../../docs_src/schema_extra_example/tutorial004_an.py!}
```

Expand All @@ -137,7 +137,7 @@ You can of course also pass multiple `examples`:
!!! tip
Prefer to use the `Annotated` version if possible.

```Python hl_lines="19-45"
```Python hl_lines="19-34"
{!> ../../../docs_src/schema_extra_example/tutorial004_py310.py!}
```

Expand All @@ -146,7 +146,7 @@ You can of course also pass multiple `examples`:
!!! tip
Prefer to use the `Annotated` version if possible.

```Python hl_lines="21-47"
```Python hl_lines="21-36"
{!> ../../../docs_src/schema_extra_example/tutorial004.py!}
```

Expand Down
27 changes: 8 additions & 19 deletions docs_src/schema_extra_example/tutorial004.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,18 @@ async def update_item(
item: Item = Body(
examples=[
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
"name": "Bar",
"price": "35.4",
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
),
Expand Down
27 changes: 8 additions & 19 deletions docs_src/schema_extra_example/tutorial004_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,18 @@ async def update_item(
Body(
examples=[
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
"name": "Bar",
"price": "35.4",
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
),
Expand Down
27 changes: 8 additions & 19 deletions docs_src/schema_extra_example/tutorial004_an_py310.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,18 @@ async def update_item(
Body(
examples=[
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
"name": "Bar",
"price": "35.4",
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
),
Expand Down
27 changes: 8 additions & 19 deletions docs_src/schema_extra_example/tutorial004_an_py39.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,18 @@ async def update_item(
Body(
examples=[
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
"name": "Bar",
"price": "35.4",
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
),
Expand Down
27 changes: 8 additions & 19 deletions docs_src/schema_extra_example/tutorial004_py310.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,18 @@ async def update_item(
item: Item = Body(
examples=[
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {
"name": "Bar",
"price": "35.4",
},
"name": "Bar",
"price": "35.4",
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
),
Expand Down
25 changes: 7 additions & 18 deletions tests/test_tutorial/test_schema_extra_example/test_tutorial004.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,15 @@ def test_openapi_schema():
"title": "Item",
"examples": [
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{"name": "Bar", "price": "35.4"},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {"name": "Bar", "price": "35.4"},
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,15 @@ def test_openapi_schema():
"title": "Item",
"examples": [
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{"name": "Bar", "price": "35.4"},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {"name": "Bar", "price": "35.4"},
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,15 @@ def test_openapi_schema(client: TestClient):
"title": "Item",
"examples": [
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{"name": "Bar", "price": "35.4"},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {"name": "Bar", "price": "35.4"},
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,15 @@ def test_openapi_schema(client: TestClient):
"title": "Item",
"examples": [
{
"summary": "A normal example",
"description": "A **normal** item works correctly.",
"value": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
},
{"name": "Bar", "price": "35.4"},
{
"summary": "An example with converted data",
"description": "FastAPI can convert price `strings` to actual `numbers` automatically",
"value": {"name": "Bar", "price": "35.4"},
},
{
"summary": "Invalid data is rejected with an error",
"value": {
"name": "Baz",
"price": "thirty five point four",
},
"name": "Baz",
"price": "thirty five point four",
},
],
}
Expand Down
Loading

0 comments on commit 4d83f98

Please sign in to comment.