|
7 | 7 |
|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | | -from dataclasses import dataclass |
11 | | -from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar |
| 10 | +from dataclasses import dataclass, field |
| 11 | +from typing import TYPE_CHECKING, Any, Literal, TypeAlias, TypeVar |
| 12 | + |
| 13 | +from typing_extensions import TypedDict |
12 | 14 |
|
13 | 15 | from datamodel_code_generator.enums import JsonSchemaVersion, OpenAPIVersion |
14 | 16 |
|
15 | 17 | if TYPE_CHECKING: |
16 | 18 | from datamodel_code_generator.types import Types |
17 | 19 |
|
18 | 20 |
|
| 21 | +class FeatureMetadata(TypedDict): |
| 22 | + """Metadata for schema feature documentation. |
| 23 | +
|
| 24 | + This metadata is used by scripts/build_schema_docs.py to generate |
| 25 | + the feature compatibility matrix in docs/supported_formats.md. |
| 26 | + """ |
| 27 | + |
| 28 | + introduced: str |
| 29 | + """Version when feature was introduced (e.g., "Draft 6", "2020-12", "OAS 3.0").""" |
| 30 | + doc_name: str |
| 31 | + """Display name for documentation (e.g., "prefixItems", "Null in type array").""" |
| 32 | + description: str |
| 33 | + """User-facing description of the feature.""" |
| 34 | + status: Literal["supported", "partial", "not_supported"] |
| 35 | + """Implementation status: supported, partial, or not_supported.""" |
| 36 | + |
| 37 | + |
19 | 38 | @dataclass(frozen=True) |
20 | 39 | class JsonSchemaFeatures: |
21 | 40 | """Feature flags for JSON Schema versions. |
22 | 41 |
|
23 | 42 | This is the base class for schema features. OpenAPISchemaFeatures |
24 | 43 | extends this to add OpenAPI-specific features. |
25 | 44 |
|
26 | | - Attributes: |
27 | | - null_in_type_array: Draft 2020-12 allows null in type arrays. |
28 | | - defs_not_definitions: Draft 2019-09+ uses $defs instead of definitions. |
29 | | - prefix_items: Draft 2020-12 uses prefixItems instead of items array. |
30 | | - boolean_schemas: Draft 6+ allows boolean values as schemas. |
31 | | - id_field: The field name for schema ID ("id" for Draft 4, "$id" for Draft 6+). |
32 | | - definitions_key: The key for definitions ("definitions" or "$defs"). |
33 | | - exclusive_as_number: Draft 6+ uses numeric exclusiveMin/Max (Draft 4 uses boolean). |
34 | | - read_only_write_only: Draft 7+ supports readOnly/writeOnly keywords. |
| 45 | + Each field includes metadata for documentation generation. |
| 46 | + Use `dataclasses.fields(JsonSchemaFeatures)` to access metadata. |
35 | 47 | """ |
36 | 48 |
|
37 | | - null_in_type_array: bool |
38 | | - defs_not_definitions: bool |
39 | | - prefix_items: bool |
40 | | - boolean_schemas: bool |
41 | | - id_field: str |
42 | | - definitions_key: str |
43 | | - exclusive_as_number: bool |
44 | | - read_only_write_only: bool |
| 49 | + null_in_type_array: bool = field( |
| 50 | + default=False, |
| 51 | + metadata=FeatureMetadata( |
| 52 | + introduced="2020-12", |
| 53 | + doc_name="Null in type array", |
| 54 | + description="Allows `type: ['string', 'null']` syntax for nullable types", |
| 55 | + status="supported", |
| 56 | + ), |
| 57 | + ) |
| 58 | + defs_not_definitions: bool = field( |
| 59 | + default=False, |
| 60 | + metadata=FeatureMetadata( |
| 61 | + introduced="2019-09", |
| 62 | + doc_name="$defs", |
| 63 | + description="Uses `$defs` instead of `definitions` for schema definitions", |
| 64 | + status="supported", |
| 65 | + ), |
| 66 | + ) |
| 67 | + prefix_items: bool = field( |
| 68 | + default=False, |
| 69 | + metadata=FeatureMetadata( |
| 70 | + introduced="2020-12", |
| 71 | + doc_name="prefixItems", |
| 72 | + description="Tuple validation using `prefixItems` keyword", |
| 73 | + status="supported", |
| 74 | + ), |
| 75 | + ) |
| 76 | + boolean_schemas: bool = field( |
| 77 | + default=False, |
| 78 | + metadata=FeatureMetadata( |
| 79 | + introduced="Draft 6", |
| 80 | + doc_name="Boolean schemas", |
| 81 | + description="Allows `true` and `false` as valid schemas", |
| 82 | + status="supported", |
| 83 | + ), |
| 84 | + ) |
| 85 | + id_field: str = field( |
| 86 | + default="$id", |
| 87 | + metadata=FeatureMetadata( |
| 88 | + introduced="Draft 6", |
| 89 | + doc_name="$id", |
| 90 | + description="Schema identifier field (`id` in Draft 4, `$id` in Draft 6+)", |
| 91 | + status="supported", |
| 92 | + ), |
| 93 | + ) |
| 94 | + definitions_key: str = field( |
| 95 | + default="$defs", |
| 96 | + metadata=FeatureMetadata( |
| 97 | + introduced="Draft 4", |
| 98 | + doc_name="definitions/$defs", |
| 99 | + description="Key for reusable schema definitions", |
| 100 | + status="supported", |
| 101 | + ), |
| 102 | + ) |
| 103 | + exclusive_as_number: bool = field( |
| 104 | + default=False, |
| 105 | + metadata=FeatureMetadata( |
| 106 | + introduced="Draft 6", |
| 107 | + doc_name="exclusiveMinimum/Maximum as number", |
| 108 | + description="Numeric `exclusiveMinimum`/`exclusiveMaximum` (boolean in Draft 4)", |
| 109 | + status="supported", |
| 110 | + ), |
| 111 | + ) |
| 112 | + read_only_write_only: bool = field( |
| 113 | + default=False, |
| 114 | + metadata=FeatureMetadata( |
| 115 | + introduced="Draft 7", |
| 116 | + doc_name="readOnly/writeOnly", |
| 117 | + description="Field visibility hints for read-only and write-only properties", |
| 118 | + status="supported", |
| 119 | + ), |
| 120 | + ) |
45 | 121 |
|
46 | 122 | @classmethod |
47 | 123 | def from_version(cls, version: JsonSchemaVersion) -> JsonSchemaFeatures: |
@@ -110,13 +186,28 @@ class OpenAPISchemaFeatures(JsonSchemaFeatures): |
110 | 186 |
|
111 | 187 | Extends JsonSchemaFeatures with OpenAPI-specific features. |
112 | 188 |
|
113 | | - Attributes: |
114 | | - nullable_keyword: OpenAPI 3.0 uses nullable: true (deprecated in 3.1). |
115 | | - discriminator_support: All OpenAPI versions support discriminator. |
| 189 | + Each field includes metadata for documentation generation. |
| 190 | + Use `dataclasses.fields(OpenAPISchemaFeatures)` to access metadata. |
116 | 191 | """ |
117 | 192 |
|
118 | | - nullable_keyword: bool |
119 | | - discriminator_support: bool |
| 193 | + nullable_keyword: bool = field( |
| 194 | + default=False, |
| 195 | + metadata=FeatureMetadata( |
| 196 | + introduced="OAS 3.0", |
| 197 | + doc_name="nullable", |
| 198 | + description="Uses `nullable: true` for nullable types (deprecated in 3.1)", |
| 199 | + status="supported", |
| 200 | + ), |
| 201 | + ) |
| 202 | + discriminator_support: bool = field( |
| 203 | + default=True, |
| 204 | + metadata=FeatureMetadata( |
| 205 | + introduced="OAS 3.0", |
| 206 | + doc_name="discriminator", |
| 207 | + description="Polymorphism support via `discriminator` keyword", |
| 208 | + status="supported", |
| 209 | + ), |
| 210 | + ) |
120 | 211 |
|
121 | 212 | @classmethod |
122 | 213 | def from_openapi_version(cls, version: OpenAPIVersion) -> OpenAPISchemaFeatures: |
@@ -310,6 +401,7 @@ def get_data_formats(*, is_openapi: bool = False) -> DataFormatMapping: |
310 | 401 |
|
311 | 402 | __all__ = [ |
312 | 403 | "DataFormatMapping", |
| 404 | + "FeatureMetadata", |
313 | 405 | "JsonSchemaFeatures", |
314 | 406 | "OpenAPISchemaFeatures", |
315 | 407 | "SchemaFeaturesT", |
|
0 commit comments