Skip to content
This repository was archived by the owner on Aug 19, 2023. It is now read-only.

Commit 56b6264

Browse files
authored
Add x-module-name extension to generated schema (#157)
1 parent c1dbcbd commit 56b6264

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

dataclasses_jsonschema/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,8 @@ def _get_field_schema(cls, field: Union[Field, Type], schema_options: SchemaOpti
645645
# Note: Unlike swagger, JSON schema does not support extensions
646646
if schema_options.schema_type in (SchemaType.SWAGGER_V2, SchemaType.SWAGGER_V3):
647647
field_schema['x-enum-name'] = field_type_name
648+
if schema_options.schema_type == SchemaType.SWAGGER_V3:
649+
field_schema['x-module-name'] = field_type.__module__
648650
elif field_type_name == 'Union':
649651
if schema_options.schema_type == SchemaType.SWAGGER_V2:
650652
raise TypeError('Type unions unsupported in Swagger 2.0')
@@ -777,6 +779,9 @@ def json_schema(
777779
'properties': properties
778780
}
779781

782+
if schema_options.schema_type == SchemaType.OPENAPI_3:
783+
schema['x-module-name'] = cls.__module__
784+
780785
if not cls.__allow_additional_props:
781786
schema["additionalProperties"] = False
782787

tests/test_apispec_plugin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def random_pet():
6767
"components": {
6868
"schemas": {
6969
"Category": {
70+
"x-module-name": "tests.test_apispec_plugin",
7071
"description": "Pet category",
7172
"type": "object",
7273
"properties": {
@@ -82,6 +83,7 @@ def random_pet():
8283
]
8384
},
8485
"Pet": {
86+
"x-module-name": "tests.test_apispec_plugin",
8587
"description": "A pet",
8688
"type": "object",
8789
"properties": {
@@ -109,7 +111,8 @@ def random_pet():
109111
],
110112
"properties": {
111113
"colour": {"type": "string"}
112-
}
114+
},
115+
"x-module-name": "tests.test_apispec_plugin"
113116
}
114117
],
115118
"description": "A cat"
@@ -126,7 +129,8 @@ def random_pet():
126129
],
127130
"properties": {
128131
"favourite_food": {"type": "string"}
129-
}
132+
},
133+
"x-module-name": "tests.test_apispec_plugin"
130134
}
131135
],
132136
"description": "A dog"

tests/test_core.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
from _decimal import Decimal
3+
from copy import deepcopy
34
from enum import Enum
45

56
from dataclasses import dataclass, field
@@ -178,7 +179,13 @@ def test_embeddable_json_schema():
178179
assert expected == Foo.json_schema(embeddable=True)
179180
expected = {'Point': POINT_SCHEMA, 'Foo': SWAGGER_V2_FOO_SCHEMA}
180181
assert expected == SubSchemas.all_json_schemas(schema_type=SchemaType.SWAGGER_V2)
181-
expected = {'Point': POINT_SCHEMA, 'Foo': SWAGGER_V3_FOO_SCHEMA}
182+
expected = {
183+
'Point': deepcopy(POINT_SCHEMA),
184+
'Foo': deepcopy(SWAGGER_V3_FOO_SCHEMA),
185+
}
186+
expected['Point']['x-module-name'] = 'tests.conftest'
187+
expected['Foo']['x-module-name'] = 'tests.conftest'
188+
expected['Foo']['properties']['d']['x-module-name'] = 'tests.conftest'
182189
assert expected == SubSchemas.all_json_schemas(schema_type=SchemaType.SWAGGER_V3)
183190
expected = {
184191
'Point': POINT_SCHEMA,
@@ -441,9 +448,11 @@ class Test(JsonSchemaMixin):
441448
expected_full_schema = compose_schema(expected_schema)
442449
assert Test.json_schema() == expected_full_schema
443450
expected_schema['properties']['name']['x-field-group'] = 1
451+
expected_schema['x-module-name'] = 'tests.test_core'
444452
assert Test.json_schema(schema_type=SchemaType.OPENAPI_3, embeddable=True) == {'Test': expected_schema}
445453
expected_schema['properties']['name']['example'] = 'foo'
446454
del expected_schema['properties']['name']['examples']
455+
del expected_schema['x-module-name']
447456
assert Test.json_schema(schema_type=SchemaType.SWAGGER_V2, embeddable=True) == {'Test': expected_schema}
448457

449458

@@ -632,7 +641,8 @@ class Employee(JsonSchemaMixin):
632641
"name": {"type": "string"},
633642
"manager": {"type": "string", "nullable": True}
634643
},
635-
"required": ["name"]
644+
"required": ["name"],
645+
"x-module-name": "tests.test_core"
636646
}
637647
expected_json_schema = compose_schema({
638648
"type": "object",
@@ -696,7 +706,8 @@ class Dog(Pet):
696706
{
697707
"type": "object",
698708
"properties": {"breed": {"type": "string"}},
699-
"required": ["breed"]
709+
"required": ["breed"],
710+
"x-module-name": "tests.test_core",
700711
},
701712
]
702713
},
@@ -708,7 +719,8 @@ class Dog(Pet):
708719
"name": {"type": "string"}
709720
},
710721
"required": ["name", "PetType"],
711-
"discriminator": {"propertyName": "PetType"}
722+
"discriminator": {"propertyName": "PetType"},
723+
"x-module-name": "tests.test_core",
712724
}
713725
}
714726

@@ -951,3 +963,18 @@ class Pet(JsonSchemaMixin):
951963

952964
p = Pet.from_dict({'name': ' Fido ', 'type': 'dog'})
953965
assert p.name == 'Fido'
966+
967+
968+
def test_module_name_extension():
969+
class PetType(Enum):
970+
DOG = "dog"
971+
CAT = "cat"
972+
973+
@dataclass
974+
class Pet(JsonSchemaMixin):
975+
name: str
976+
type: PetType
977+
978+
schema = Pet.json_schema(embeddable=True, schema_type=SchemaType.OPENAPI_3)
979+
assert schema['Pet']['x-module-name'] == 'tests.test_core'
980+
assert schema['Pet']['properties']['type']['x-module-name'] == 'tests.test_core'

0 commit comments

Comments
 (0)