qstd-openapi 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. qstd_openapi-0.1.0/CHANGELOG.md +54 -0
  2. qstd_openapi-0.1.0/LICENSE +21 -0
  3. qstd_openapi-0.1.0/MANIFEST.in +4 -0
  4. qstd_openapi-0.1.0/PKG-INFO +324 -0
  5. qstd_openapi-0.1.0/README.md +260 -0
  6. qstd_openapi-0.1.0/docs/README.md +14 -0
  7. qstd_openapi-0.1.0/docs/aggregation.md +100 -0
  8. qstd_openapi-0.1.0/docs/describing-operations.md +183 -0
  9. qstd_openapi-0.1.0/docs/fastapi.md +108 -0
  10. qstd_openapi-0.1.0/docs/getting-started.md +112 -0
  11. qstd_openapi-0.1.0/docs/output-and-ci.md +136 -0
  12. qstd_openapi-0.1.0/docs/sanic.md +140 -0
  13. qstd_openapi-0.1.0/docs/schemas-and-errors.md +186 -0
  14. qstd_openapi-0.1.0/docs/webhooks-and-scopes.md +117 -0
  15. qstd_openapi-0.1.0/pyproject.toml +112 -0
  16. qstd_openapi-0.1.0/setup.cfg +4 -0
  17. qstd_openapi-0.1.0/src/qstd_openapi/__init__.py +56 -0
  18. qstd_openapi-0.1.0/src/qstd_openapi/__main__.py +103 -0
  19. qstd_openapi-0.1.0/src/qstd_openapi/_compat.py +16 -0
  20. qstd_openapi-0.1.0/src/qstd_openapi/contrib/__init__.py +1 -0
  21. qstd_openapi-0.1.0/src/qstd_openapi/contrib/app_errors.py +173 -0
  22. qstd_openapi-0.1.0/src/qstd_openapi/core/__init__.py +56 -0
  23. qstd_openapi-0.1.0/src/qstd_openapi/core/document.py +1029 -0
  24. qstd_openapi-0.1.0/src/qstd_openapi/core/documents.py +296 -0
  25. qstd_openapi-0.1.0/src/qstd_openapi/core/error_responses.py +98 -0
  26. qstd_openapi-0.1.0/src/qstd_openapi/core/filters.py +81 -0
  27. qstd_openapi-0.1.0/src/qstd_openapi/core/schemas.py +554 -0
  28. qstd_openapi-0.1.0/src/qstd_openapi/core/sources.py +107 -0
  29. qstd_openapi-0.1.0/src/qstd_openapi/dialects/__init__.py +19 -0
  30. qstd_openapi-0.1.0/src/qstd_openapi/dialects/base.py +47 -0
  31. qstd_openapi-0.1.0/src/qstd_openapi/dialects/openapi30.py +312 -0
  32. qstd_openapi-0.1.0/src/qstd_openapi/dialects/openapi31.py +82 -0
  33. qstd_openapi-0.1.0/src/qstd_openapi/errors.py +117 -0
  34. qstd_openapi-0.1.0/src/qstd_openapi/fastapi.py +441 -0
  35. qstd_openapi-0.1.0/src/qstd_openapi/markers.py +57 -0
  36. qstd_openapi-0.1.0/src/qstd_openapi/meta/__init__.py +55 -0
  37. qstd_openapi-0.1.0/src/qstd_openapi/meta/merge.py +271 -0
  38. qstd_openapi-0.1.0/src/qstd_openapi/meta/model.py +182 -0
  39. qstd_openapi-0.1.0/src/qstd_openapi/meta/storage.py +112 -0
  40. qstd_openapi-0.1.0/src/qstd_openapi/openapi.py +656 -0
  41. qstd_openapi-0.1.0/src/qstd_openapi/py.typed +0 -0
  42. qstd_openapi-0.1.0/src/qstd_openapi/pydantic.py +494 -0
  43. qstd_openapi-0.1.0/src/qstd_openapi/sanic.py +343 -0
  44. qstd_openapi-0.1.0/src/qstd_openapi/serialization.py +63 -0
  45. qstd_openapi-0.1.0/src/qstd_openapi/tags.py +58 -0
  46. qstd_openapi-0.1.0/src/qstd_openapi/testing.py +98 -0
  47. qstd_openapi-0.1.0/src/qstd_openapi/ui.py +109 -0
  48. qstd_openapi-0.1.0/src/qstd_openapi.egg-info/PKG-INFO +324 -0
  49. qstd_openapi-0.1.0/src/qstd_openapi.egg-info/SOURCES.txt +50 -0
  50. qstd_openapi-0.1.0/src/qstd_openapi.egg-info/dependency_links.txt +1 -0
  51. qstd_openapi-0.1.0/src/qstd_openapi.egg-info/requires.txt +44 -0
  52. qstd_openapi-0.1.0/src/qstd_openapi.egg-info/top_level.txt +1 -0
@@ -0,0 +1,54 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+ Before 1.0 a minor release may change the public API; such changes are
8
+ listed here.
9
+
10
+ ---
11
+
12
+ ## [0.1.0] - 2026-10-01
13
+
14
+ First public release.
15
+
16
+ ### Added
17
+
18
+ - Declarative description of operations: decorators, a single `describe()`
19
+ call and `attach()` for your own decorators, middlewares and validators;
20
+ descriptions survive `functools.wraps` and are stored on the function, not
21
+ in a global registry.
22
+ - `OpenAPI` document builder: OpenAPI 3.1 and 3.0.3 from the same
23
+ description, strict by default (conflicting descriptions, operations,
24
+ components and `operationId`s fail the build naming both sources),
25
+ `scope` filters for several documents from one code base, tag rules by
26
+ path.
27
+ - Schemas from Pydantic 2 models, dataclasses, `typing` constructs, a
28
+ `TypeAdapter` or raw JSON Schema; models become components; validation
29
+ schemas for requests and serialization schemas for responses;
30
+ `type_overrides` to match the project's own serializer.
31
+ - Error classes as responses through error providers; `AppErrors` for
32
+ "static error classes" with a code, a message and payload fields.
33
+ - `operationId` for every operation, from the route name or the function
34
+ name, unless set explicitly.
35
+ - Named `examples` for request bodies, responses and parameters.
36
+ - Sanic integration: routes read from the application, path parameters from
37
+ the URL template, a typed router wrapper, document and Redoc / Swagger UI
38
+ endpoints.
39
+ - Webhooks collected per module (`WebhookSet`).
40
+ - Aggregation of ready OpenAPI documents with path prefixes, component
41
+ namespaces, `$ref` rewriting and configurable conflict policies; callable
42
+ policies receive a public `Conflict` value.
43
+ - JSON and YAML output, `tags_from_markdown()` for tag descriptions,
44
+ `OpenAPI(validate=True)`, `python -m qstd_openapi dump` and
45
+ `qstd_openapi.testing.assert_matches_snapshot()` for CI.
46
+ - Typed public API (`py.typed`): option names and values are checked by
47
+ mypy and pyright.
48
+
49
+ ### Experimental
50
+
51
+ - FastAPI integration (`augment` mode): covered by tests, not yet checked on
52
+ a production project; merging rules may change in a minor release.
53
+ - Pydantic 1 support (`pydantic-v1` extra): expected to be removed in a
54
+ future release.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 QuisEgoSum
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ global-exclude tests/*
2
+ global-exclude examples/*
3
+ include CHANGELOG.md
4
+ recursive-include docs *.md
@@ -0,0 +1,324 @@
1
+ Metadata-Version: 2.4
2
+ Name: qstd-openapi
3
+ Version: 0.1.0
4
+ Summary: A framework-agnostic, declarative OpenAPI 3.1/3.0 generator: decorators, error classes, webhooks and document aggregation, with Pydantic and Sanic integrations (FastAPI and Pydantic 1 — experimental).
5
+ Author-email: QuisEgoSum <subbotin.evdokim@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/QuisEgoSum/qstd-openapi
8
+ Project-URL: Repository, https://github.com/QuisEgoSum/qstd-openapi
9
+ Project-URL: Changelog, https://github.com/QuisEgoSum/qstd-openapi/blob/main/CHANGELOG.md
10
+ Project-URL: Documentation, https://github.com/QuisEgoSum/qstd-openapi/tree/main/docs
11
+ Keywords: openapi,swagger,redoc,pydantic,sanic,fastapi,documentation
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Topic :: Software Development :: Documentation
15
+ Classifier: Framework :: Pydantic :: 2
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Operating System :: OS Independent
23
+ Classifier: Typing :: Typed
24
+ Requires-Python: >=3.9
25
+ Description-Content-Type: text/markdown
26
+ License-File: LICENSE
27
+ Requires-Dist: typing_extensions>=4.7
28
+ Provides-Extra: pydantic
29
+ Requires-Dist: pydantic<3,>=2.6; extra == "pydantic"
30
+ Provides-Extra: pydantic-v1
31
+ Requires-Dist: pydantic<2,>=1.10; extra == "pydantic-v1"
32
+ Provides-Extra: validate
33
+ Requires-Dist: openapi-spec-validator>=0.7; extra == "validate"
34
+ Provides-Extra: sanic
35
+ Requires-Dist: sanic>=23.12; extra == "sanic"
36
+ Provides-Extra: fastapi
37
+ Requires-Dist: fastapi>=0.110; extra == "fastapi"
38
+ Provides-Extra: yaml
39
+ Requires-Dist: PyYAML>=6; extra == "yaml"
40
+ Provides-Extra: all
41
+ Requires-Dist: pydantic<3,>=2.6; extra == "all"
42
+ Requires-Dist: openapi-spec-validator>=0.7; extra == "all"
43
+ Requires-Dist: sanic>=23.12; extra == "all"
44
+ Requires-Dist: fastapi>=0.110; extra == "all"
45
+ Requires-Dist: PyYAML>=6; extra == "all"
46
+ Provides-Extra: dev
47
+ Requires-Dist: pydantic<3,>=2.6; extra == "dev"
48
+ Requires-Dist: openapi-spec-validator>=0.7; extra == "dev"
49
+ Requires-Dist: sanic>=23.12; extra == "dev"
50
+ Requires-Dist: sanic-testing>=23.12; extra == "dev"
51
+ Requires-Dist: fastapi>=0.110; extra == "dev"
52
+ Requires-Dist: httpx>=0.24; extra == "dev"
53
+ Requires-Dist: PyYAML>=6; extra == "dev"
54
+ Requires-Dist: pytest<9.0,>=8.0; extra == "dev"
55
+ Requires-Dist: tox>=4.26.0; extra == "dev"
56
+ Requires-Dist: pytest-cov<7.0,>=6.0; extra == "dev"
57
+ Requires-Dist: ruff>=0.11.2; extra == "dev"
58
+ Requires-Dist: black>=25.1.0; extra == "dev"
59
+ Requires-Dist: build>=1.2.2; extra == "dev"
60
+ Requires-Dist: twine>=6.1.0; extra == "dev"
61
+ Requires-Dist: pyright>=1.1.402; extra == "dev"
62
+ Requires-Dist: mypy<2,>=1.16; extra == "dev"
63
+ Dynamic: license-file
64
+
65
+ # qstd-openapi
66
+
67
+ Build OpenAPI 3.1 or 3.0 documents from Python code without tying the
68
+ description to one web framework.
69
+
70
+ `qstd-openapi` lets handlers, validators, authentication decorators and other
71
+ layers describe the part of an operation they own. It combines those pieces,
72
+ turns Python types into schemas, reports conflicting descriptions and can
73
+ publish or snapshot the resulting document.
74
+
75
+ The primary integration is Sanic. FastAPI support is experimental.
76
+
77
+ ## Why qstd-openapi
78
+
79
+ - Keep API documentation next to the code that implements each concern.
80
+ - Describe application exceptions as reusable error responses.
81
+ - Use Pydantic models, dataclasses, Python types or raw JSON Schema.
82
+ - Build several documents from the same application with scope filters.
83
+ - Generate stable JSON or YAML for review and CI.
84
+ - Combine existing service documents with path prefixes and component
85
+ namespaces.
86
+ - Produce OpenAPI 3.1 and 3.0.3 from the same description.
87
+
88
+ Metadata is stored on the decorated functions, not in a global registry.
89
+ Creating two `OpenAPI` instances in one process does not mix their state.
90
+
91
+ ## Installation
92
+
93
+ Choose the extras needed by your application:
94
+
95
+ ```bash
96
+ # Sanic with Pydantic schemas and YAML output
97
+ pip install "qstd-openapi[pydantic,sanic,yaml]"
98
+
99
+ # FastAPI integration (experimental)
100
+ pip install "qstd-openapi[pydantic,fastapi]"
101
+
102
+ # Framework-independent document builder
103
+ pip install "qstd-openapi[pydantic]"
104
+ ```
105
+
106
+ Other extras:
107
+
108
+ | Extra | Enables |
109
+ |---|---|
110
+ | `pydantic` | Pydantic 2 models, dataclasses and annotated types |
111
+ | `pydantic-v1` | Pydantic 1.10 support; experimental and incompatible with `pydantic` |
112
+ | `validate` | Validation through `openapi-spec-validator` |
113
+ | `sanic` | Sanic route discovery and documentation endpoints |
114
+ | `fastapi` | FastAPI document augmentation; experimental |
115
+ | `yaml` | YAML input and output |
116
+ | `all` | Every extra except `pydantic-v1` |
117
+
118
+ Python 3.9–3.13 is supported. The base package depends only on
119
+ `typing_extensions`.
120
+
121
+ ## Quick start with Sanic
122
+
123
+ Save this as `app.py`:
124
+
125
+ ```python
126
+ from datetime import datetime, timezone
127
+
128
+ from pydantic import BaseModel, Field
129
+ from sanic import Blueprint, Request, Sanic, response
130
+
131
+ from qstd_openapi import OpenAPI
132
+ from qstd_openapi.sanic import OpenAPIBlueprint, SanicRoutes, mount
133
+ from qstd_openapi.ui import Redoc
134
+
135
+
136
+ class UserRegisterInput(BaseModel):
137
+ email: str
138
+ password: str = Field(min_length=8)
139
+
140
+
141
+ class UserDTO(BaseModel):
142
+ id: int
143
+ email: str
144
+ created_at: datetime
145
+
146
+
147
+ users = OpenAPIBlueprint(Blueprint('Users', url_prefix='/users'))
148
+
149
+
150
+ @users.post(
151
+ '/register',
152
+ tags=['Users'],
153
+ body=UserRegisterInput,
154
+ responses={201: UserDTO},
155
+ )
156
+ async def register_user(request: Request):
157
+ """Register a user."""
158
+ payload = UserRegisterInput.model_validate(request.json)
159
+ user = UserDTO(
160
+ id=1,
161
+ email=payload.email,
162
+ created_at=datetime.now(timezone.utc),
163
+ )
164
+ return response.json(user.model_dump(mode='json'), status=201)
165
+
166
+
167
+ app = Sanic('users_api')
168
+ app.blueprint(users.blueprint)
169
+
170
+ spec = OpenAPI(info={'title': 'Users API', 'version': '1.0.0'})
171
+ spec.include(SanicRoutes(app))
172
+ mount(
173
+ app,
174
+ spec,
175
+ json_path='/openapi.json',
176
+ ui={'/docs': Redoc()},
177
+ )
178
+ ```
179
+
180
+ Run it:
181
+
182
+ ```bash
183
+ sanic app:app --dev
184
+ ```
185
+
186
+ Open `http://127.0.0.1:8000/docs`. The document contains the request and
187
+ response schemas, the docstring summary and an automatically generated
188
+ `operationId`.
189
+
190
+ `OpenAPIBlueprint` accepts Sanic route options and documentation options in
191
+ the same call. If you prefer ordinary framework decorators, the equivalent
192
+ description can be written separately:
193
+
194
+ ```python
195
+ from sanic import Blueprint
196
+
197
+ from qstd_openapi import openapi
198
+
199
+ users_blueprint = Blueprint('Users', url_prefix='/users')
200
+
201
+
202
+ @users_blueprint.post('/register')
203
+ @openapi.describe(
204
+ tags=['Users'],
205
+ body=UserRegisterInput,
206
+ responses={201: UserDTO},
207
+ )
208
+ async def register_user(request):
209
+ ...
210
+ ```
211
+
212
+ ## Documentation can come from any layer
213
+
214
+ A project decorator can declare the security and errors it adds. The handler
215
+ does not need to repeat them:
216
+
217
+ ```python
218
+ import functools
219
+
220
+ from qstd_openapi import openapi
221
+
222
+
223
+ def require_session(func):
224
+ @functools.wraps(func)
225
+ async def wrapper(request, *args, **kwargs):
226
+ # Authenticate the request or raise UnauthorizedError.
227
+ return await func(request, *args, **kwargs)
228
+
229
+ return openapi.attach(
230
+ wrapper,
231
+ security='UserSession',
232
+ errors=[UnauthorizedError],
233
+ )
234
+ ```
235
+
236
+ Metadata is collected through `functools.wraps` chains. Conflicting scalar
237
+ values, duplicate operations, schemas or `operationId` values fail the build
238
+ with both sources named in the error.
239
+
240
+ ## Keep the document in CI
241
+
242
+ Export a stable, reviewable snapshot:
243
+
244
+ ```bash
245
+ python -m qstd_openapi dump users_api.openapi:build_spec \
246
+ -o docs/openapi.json
247
+
248
+ python -m qstd_openapi dump users_api.openapi:build_spec \
249
+ -o docs/openapi.json --check
250
+ ```
251
+
252
+ The target may be an `OpenAPI` instance or a zero-argument factory. A pytest
253
+ helper is also available:
254
+
255
+ ```python
256
+ from qstd_openapi.testing import assert_matches_snapshot
257
+
258
+
259
+ def test_openapi_document() -> None:
260
+ assert_matches_snapshot(build_spec(), 'docs/openapi.json')
261
+ ```
262
+
263
+ Set `QSTD_OPENAPI_UPDATE_SNAPSHOTS=1` to accept an intentional change.
264
+
265
+ ## Documentation
266
+
267
+ - [Getting started][getting-started] — how descriptions become a
268
+ document and which integration to choose.
269
+ - [Describing operations][describing-operations] — decorators,
270
+ `describe()`, parameters, responses, examples and `operationId`.
271
+ - [Schemas and errors][schemas-and-errors] — Pydantic, raw schemas,
272
+ serializer overrides and application error classes.
273
+ - [Sanic integration][sanic] — route discovery, router wrapper and
274
+ documentation endpoints.
275
+ - [FastAPI integration][fastapi] — experimental augment mode and its
276
+ merge rules.
277
+ - [Webhooks and scopes][webhooks-and-scopes] — outbound operations and
278
+ several documents from one application.
279
+ - [Document aggregation][aggregation] — combining existing OpenAPI
280
+ files and rewriting references.
281
+ - [Output and CI][output-and-ci] — JSON/YAML, validation, snapshots and
282
+ OpenAPI 3.0 output.
283
+
284
+ ## Examples
285
+
286
+ The [examples index][examples] contains commands and expected
287
+ results for:
288
+
289
+ - a minimal, single-file Sanic application;
290
+ - a small Sanic service with errors, authentication and a webhook;
291
+ - FastAPI document augmentation;
292
+ - a gateway document assembled from two YAML service documents.
293
+
294
+ Examples return payloads that match their documented schemas and are exercised
295
+ through HTTP clients by the test suite.
296
+
297
+ ## Support status
298
+
299
+ | Area | Status |
300
+ |---|---|
301
+ | OpenAPI 3.1 and 3.0.3 | Supported |
302
+ | Sanic ≥ 23.12 | Supported |
303
+ | Pydantic ≥ 2.6 | Supported |
304
+ | FastAPI ≥ 0.110 | Experimental |
305
+ | Pydantic 1.10 | Experimental; planned for removal |
306
+
307
+ Experimental integrations are covered by automated tests but have not yet
308
+ been verified in a production application. Their behavior may change in a
309
+ minor release before 1.0.
310
+
311
+ ## License
312
+
313
+ MIT, see [LICENSE][license].
314
+
315
+ [aggregation]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/aggregation.md
316
+ [describing-operations]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/describing-operations.md
317
+ [examples]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/examples/README.md
318
+ [fastapi]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/fastapi.md
319
+ [getting-started]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/getting-started.md
320
+ [license]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/LICENSE
321
+ [output-and-ci]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/output-and-ci.md
322
+ [sanic]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/sanic.md
323
+ [schemas-and-errors]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/schemas-and-errors.md
324
+ [webhooks-and-scopes]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/webhooks-and-scopes.md
@@ -0,0 +1,260 @@
1
+ # qstd-openapi
2
+
3
+ Build OpenAPI 3.1 or 3.0 documents from Python code without tying the
4
+ description to one web framework.
5
+
6
+ `qstd-openapi` lets handlers, validators, authentication decorators and other
7
+ layers describe the part of an operation they own. It combines those pieces,
8
+ turns Python types into schemas, reports conflicting descriptions and can
9
+ publish or snapshot the resulting document.
10
+
11
+ The primary integration is Sanic. FastAPI support is experimental.
12
+
13
+ ## Why qstd-openapi
14
+
15
+ - Keep API documentation next to the code that implements each concern.
16
+ - Describe application exceptions as reusable error responses.
17
+ - Use Pydantic models, dataclasses, Python types or raw JSON Schema.
18
+ - Build several documents from the same application with scope filters.
19
+ - Generate stable JSON or YAML for review and CI.
20
+ - Combine existing service documents with path prefixes and component
21
+ namespaces.
22
+ - Produce OpenAPI 3.1 and 3.0.3 from the same description.
23
+
24
+ Metadata is stored on the decorated functions, not in a global registry.
25
+ Creating two `OpenAPI` instances in one process does not mix their state.
26
+
27
+ ## Installation
28
+
29
+ Choose the extras needed by your application:
30
+
31
+ ```bash
32
+ # Sanic with Pydantic schemas and YAML output
33
+ pip install "qstd-openapi[pydantic,sanic,yaml]"
34
+
35
+ # FastAPI integration (experimental)
36
+ pip install "qstd-openapi[pydantic,fastapi]"
37
+
38
+ # Framework-independent document builder
39
+ pip install "qstd-openapi[pydantic]"
40
+ ```
41
+
42
+ Other extras:
43
+
44
+ | Extra | Enables |
45
+ |---|---|
46
+ | `pydantic` | Pydantic 2 models, dataclasses and annotated types |
47
+ | `pydantic-v1` | Pydantic 1.10 support; experimental and incompatible with `pydantic` |
48
+ | `validate` | Validation through `openapi-spec-validator` |
49
+ | `sanic` | Sanic route discovery and documentation endpoints |
50
+ | `fastapi` | FastAPI document augmentation; experimental |
51
+ | `yaml` | YAML input and output |
52
+ | `all` | Every extra except `pydantic-v1` |
53
+
54
+ Python 3.9–3.13 is supported. The base package depends only on
55
+ `typing_extensions`.
56
+
57
+ ## Quick start with Sanic
58
+
59
+ Save this as `app.py`:
60
+
61
+ ```python
62
+ from datetime import datetime, timezone
63
+
64
+ from pydantic import BaseModel, Field
65
+ from sanic import Blueprint, Request, Sanic, response
66
+
67
+ from qstd_openapi import OpenAPI
68
+ from qstd_openapi.sanic import OpenAPIBlueprint, SanicRoutes, mount
69
+ from qstd_openapi.ui import Redoc
70
+
71
+
72
+ class UserRegisterInput(BaseModel):
73
+ email: str
74
+ password: str = Field(min_length=8)
75
+
76
+
77
+ class UserDTO(BaseModel):
78
+ id: int
79
+ email: str
80
+ created_at: datetime
81
+
82
+
83
+ users = OpenAPIBlueprint(Blueprint('Users', url_prefix='/users'))
84
+
85
+
86
+ @users.post(
87
+ '/register',
88
+ tags=['Users'],
89
+ body=UserRegisterInput,
90
+ responses={201: UserDTO},
91
+ )
92
+ async def register_user(request: Request):
93
+ """Register a user."""
94
+ payload = UserRegisterInput.model_validate(request.json)
95
+ user = UserDTO(
96
+ id=1,
97
+ email=payload.email,
98
+ created_at=datetime.now(timezone.utc),
99
+ )
100
+ return response.json(user.model_dump(mode='json'), status=201)
101
+
102
+
103
+ app = Sanic('users_api')
104
+ app.blueprint(users.blueprint)
105
+
106
+ spec = OpenAPI(info={'title': 'Users API', 'version': '1.0.0'})
107
+ spec.include(SanicRoutes(app))
108
+ mount(
109
+ app,
110
+ spec,
111
+ json_path='/openapi.json',
112
+ ui={'/docs': Redoc()},
113
+ )
114
+ ```
115
+
116
+ Run it:
117
+
118
+ ```bash
119
+ sanic app:app --dev
120
+ ```
121
+
122
+ Open `http://127.0.0.1:8000/docs`. The document contains the request and
123
+ response schemas, the docstring summary and an automatically generated
124
+ `operationId`.
125
+
126
+ `OpenAPIBlueprint` accepts Sanic route options and documentation options in
127
+ the same call. If you prefer ordinary framework decorators, the equivalent
128
+ description can be written separately:
129
+
130
+ ```python
131
+ from sanic import Blueprint
132
+
133
+ from qstd_openapi import openapi
134
+
135
+ users_blueprint = Blueprint('Users', url_prefix='/users')
136
+
137
+
138
+ @users_blueprint.post('/register')
139
+ @openapi.describe(
140
+ tags=['Users'],
141
+ body=UserRegisterInput,
142
+ responses={201: UserDTO},
143
+ )
144
+ async def register_user(request):
145
+ ...
146
+ ```
147
+
148
+ ## Documentation can come from any layer
149
+
150
+ A project decorator can declare the security and errors it adds. The handler
151
+ does not need to repeat them:
152
+
153
+ ```python
154
+ import functools
155
+
156
+ from qstd_openapi import openapi
157
+
158
+
159
+ def require_session(func):
160
+ @functools.wraps(func)
161
+ async def wrapper(request, *args, **kwargs):
162
+ # Authenticate the request or raise UnauthorizedError.
163
+ return await func(request, *args, **kwargs)
164
+
165
+ return openapi.attach(
166
+ wrapper,
167
+ security='UserSession',
168
+ errors=[UnauthorizedError],
169
+ )
170
+ ```
171
+
172
+ Metadata is collected through `functools.wraps` chains. Conflicting scalar
173
+ values, duplicate operations, schemas or `operationId` values fail the build
174
+ with both sources named in the error.
175
+
176
+ ## Keep the document in CI
177
+
178
+ Export a stable, reviewable snapshot:
179
+
180
+ ```bash
181
+ python -m qstd_openapi dump users_api.openapi:build_spec \
182
+ -o docs/openapi.json
183
+
184
+ python -m qstd_openapi dump users_api.openapi:build_spec \
185
+ -o docs/openapi.json --check
186
+ ```
187
+
188
+ The target may be an `OpenAPI` instance or a zero-argument factory. A pytest
189
+ helper is also available:
190
+
191
+ ```python
192
+ from qstd_openapi.testing import assert_matches_snapshot
193
+
194
+
195
+ def test_openapi_document() -> None:
196
+ assert_matches_snapshot(build_spec(), 'docs/openapi.json')
197
+ ```
198
+
199
+ Set `QSTD_OPENAPI_UPDATE_SNAPSHOTS=1` to accept an intentional change.
200
+
201
+ ## Documentation
202
+
203
+ - [Getting started][getting-started] — how descriptions become a
204
+ document and which integration to choose.
205
+ - [Describing operations][describing-operations] — decorators,
206
+ `describe()`, parameters, responses, examples and `operationId`.
207
+ - [Schemas and errors][schemas-and-errors] — Pydantic, raw schemas,
208
+ serializer overrides and application error classes.
209
+ - [Sanic integration][sanic] — route discovery, router wrapper and
210
+ documentation endpoints.
211
+ - [FastAPI integration][fastapi] — experimental augment mode and its
212
+ merge rules.
213
+ - [Webhooks and scopes][webhooks-and-scopes] — outbound operations and
214
+ several documents from one application.
215
+ - [Document aggregation][aggregation] — combining existing OpenAPI
216
+ files and rewriting references.
217
+ - [Output and CI][output-and-ci] — JSON/YAML, validation, snapshots and
218
+ OpenAPI 3.0 output.
219
+
220
+ ## Examples
221
+
222
+ The [examples index][examples] contains commands and expected
223
+ results for:
224
+
225
+ - a minimal, single-file Sanic application;
226
+ - a small Sanic service with errors, authentication and a webhook;
227
+ - FastAPI document augmentation;
228
+ - a gateway document assembled from two YAML service documents.
229
+
230
+ Examples return payloads that match their documented schemas and are exercised
231
+ through HTTP clients by the test suite.
232
+
233
+ ## Support status
234
+
235
+ | Area | Status |
236
+ |---|---|
237
+ | OpenAPI 3.1 and 3.0.3 | Supported |
238
+ | Sanic ≥ 23.12 | Supported |
239
+ | Pydantic ≥ 2.6 | Supported |
240
+ | FastAPI ≥ 0.110 | Experimental |
241
+ | Pydantic 1.10 | Experimental; planned for removal |
242
+
243
+ Experimental integrations are covered by automated tests but have not yet
244
+ been verified in a production application. Their behavior may change in a
245
+ minor release before 1.0.
246
+
247
+ ## License
248
+
249
+ MIT, see [LICENSE][license].
250
+
251
+ [aggregation]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/aggregation.md
252
+ [describing-operations]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/describing-operations.md
253
+ [examples]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/examples/README.md
254
+ [fastapi]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/fastapi.md
255
+ [getting-started]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/getting-started.md
256
+ [license]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/LICENSE
257
+ [output-and-ci]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/output-and-ci.md
258
+ [sanic]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/sanic.md
259
+ [schemas-and-errors]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/schemas-and-errors.md
260
+ [webhooks-and-scopes]: https://github.com/QuisEgoSum/qstd-openapi/blob/main/docs/webhooks-and-scopes.md
@@ -0,0 +1,14 @@
1
+ # Documentation
2
+
3
+ Start with [Getting started](getting-started.md), then follow the guide for
4
+ the part of the library you are using:
5
+
6
+ - [Describing operations](describing-operations.md)
7
+ - [Schemas and errors](schemas-and-errors.md)
8
+ - [Sanic integration](sanic.md)
9
+ - [FastAPI integration](fastapi.md)
10
+ - [Webhooks and scopes](webhooks-and-scopes.md)
11
+ - [Document aggregation](aggregation.md)
12
+ - [Output and CI](output-and-ci.md)
13
+
14
+ Runnable applications are kept in [`examples/`](../examples/README.md).