Skip to content

Commit 64e1710

Browse files
committed
epoch - tests & doc
1 parent 4f94f67 commit 64e1710

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

docs/source/advanced.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,14 @@ Limiting the concurrency to a certain number of clients:
484484
break
485485
else:
486486
done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
487+
488+
Epoch Types
489+
===========
490+
491+
If installed, pydantic-extra-types is used to provide an epoch data type for integers and float values mapping to datetime.datetime.
492+
493+
A :ref:`Document Plugin <plugin:Document>` can be used to modify a description document to add a format: date-time to the numeric type definition for a posix timestamp.
494+
495+
.. code:: yaml
496+
type: integer
497+
format: date-time

docs/source/plugin.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ Using a Document plugin to modify the parsed description document to state the c
128128
api = OpenAPI.load_sync("https://try.gitea.io/swagger.v1.json", plugins=[ContentType()])
129129
130130
131+
Another example is adding the "format" specifier to an epoch timestamp to have it de-serialized as datetime instead of number/integer.
132+
133+
.. code:: python
134+
135+
class EpochTimestamp(aiopenapi3.plugin.Document):
136+
def parsed(self, ctx):
137+
ctx.document["components"]["schemas"]["LogEvent"]["properties"]["timestamp"]["format"] = "date-time"
138+
return ctx
139+
140+
131141
Message
132142
=======
133143

tests/fixtures/petstore-expanded.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ components:
131131
- type: object
132132
required:
133133
- id
134+
- properties
134135
properties:
135136
id:
136137
type: integer
137138
format: int64
139+
created:
140+
type: integer
138141

139142
NewPet:
140143
type: object

tests/plugin_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import httpx
2-
1+
import datetime
32
from pathlib import Path
43

4+
import httpx
55
import yarl
66

77
from aiopenapi3 import FileSystemLoader, OpenAPI
@@ -43,6 +43,7 @@ def parsed(self, ctx):
4343
},
4444
}
4545
)
46+
ctx.document["components"]["schemas"]["Pet"]["allOf"][1]["properties"]["created"]["format"] = "date-time"
4647
else:
4748
raise ValueError(f"unexpected url {ctx.url.path} expecting {self.url}")
4849

@@ -57,7 +58,7 @@ def sending(self, ctx):
5758
return ctx
5859

5960
def received(self, ctx):
60-
ctx.received = """[{"id":1,"name":"theanimal", "weight": null}]"""
61+
ctx.received = """[{"id":1,"name":"theanimal", "created":4711,"weight": null}]"""
6162
return ctx
6263

6364
def parsed(self, ctx):
@@ -94,3 +95,4 @@ def test_Plugins(httpx_mock, with_plugin_base):
9495
assert item.id == 3
9596
assert item.weight == None # default does not apply as it it unsed
9697
assert item.color == "red" # default does not apply
98+
assert item.created == datetime.datetime.fromtimestamp(4711, tz=datetime.timezone.utc)

0 commit comments

Comments
 (0)