voluptuous-openapi 0.0.2__tar.gz → 0.0.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: voluptuous-openapi
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Convert voluptuous schemas to OpenAPI Schema object
5
5
  Home-page: http://github.com/Shulyaka/voluptuous-openapi
6
6
  Author: Denis Shulyaka
@@ -2,7 +2,7 @@ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name="voluptuous-openapi",
5
- version="0.0.2",
5
+ version="0.0.3",
6
6
  description="Convert voluptuous schemas to OpenAPI Schema object",
7
7
  url="http://github.com/Shulyaka/voluptuous-openapi",
8
8
  author="Denis Shulyaka",
@@ -91,6 +91,33 @@ def test_dict():
91
91
  )
92
92
  )
93
93
 
94
+ assert {"type": "object", "additionalProperties": True} == convert(vol.Schema(dict))
95
+
96
+ assert {"type": "object", "additionalProperties": True} == convert(
97
+ vol.Schema(dict[str, Any])
98
+ )
99
+
100
+ assert {"type": "object", "additionalProperties": {"type": "integer"}} == convert(
101
+ vol.Schema({str: int})
102
+ )
103
+
104
+ assert {
105
+ "type": "object",
106
+ "properties": {"x": {"type": "integer"}},
107
+ "required": [],
108
+ "additionalProperties": {"type": "string"},
109
+ } == convert(vol.Schema({"x": int, str: str}))
110
+
111
+
112
+ def test_tuple():
113
+ assert {"type": "array", "items": {"type": "string"}} == convert(vol.Schema(tuple))
114
+ assert {"type": "array", "items": {"type": "string"}} == convert(
115
+ vol.Schema(tuple[Any])
116
+ )
117
+ assert {"type": "array", "items": {"type": "integer"}} == convert(
118
+ vol.Schema(tuple[int])
119
+ )
120
+
94
121
 
95
122
  def test_marker_description():
96
123
  assert {
@@ -211,6 +238,14 @@ def test_list():
211
238
 
212
239
  assert {"type": "array", "items": {"type": "string"}} == convert(vol.Schema(list))
213
240
 
241
+ assert {"type": "array", "items": {"type": "string"}} == convert(
242
+ vol.Schema(list[Any])
243
+ )
244
+
245
+ assert {"type": "array", "items": {"type": "integer"}} == convert(
246
+ vol.Schema(list[int])
247
+ )
248
+
214
249
 
215
250
  def test_any_of():
216
251
  assert {"anyOf": [{"type": "number"}, {"type": "integer"}]} == convert(
@@ -230,15 +265,22 @@ def test_key_any():
230
265
  "properties": {
231
266
  "name": {
232
267
  "type": "string",
233
- "description": "At least one of ('name', 'area') must be provided",
234
268
  },
235
269
  "area": {
236
270
  "type": "string",
237
- "description": "At least one of ('name', 'area') must be provided",
271
+ "description": "The ID or the area",
238
272
  },
239
273
  },
240
274
  "required": [],
241
- } == convert(vol.Schema({vol.Any("name", "area"): str}))
275
+ } == convert(
276
+ vol.Schema(
277
+ {
278
+ vol.Any(
279
+ "name", vol.Optional("area", description="The ID or the area")
280
+ ): str
281
+ }
282
+ )
283
+ )
242
284
 
243
285
 
244
286
  def test_function():
@@ -53,22 +53,6 @@ def convert(schema: Any, *, custom_serializer: Callable | None = None) -> dict:
53
53
  properties = {}
54
54
  required = []
55
55
 
56
- # Unfold vol.Any in keys
57
- if vol.Any in [type(k) for k in schema.keys()]:
58
- pschema = {}
59
- for key, value in schema.items():
60
- if isinstance(key, vol.Any):
61
- description = key.msg
62
- if not description:
63
- description = (
64
- f"At least one of {key.validators} must be provided"
65
- )
66
- for val in key.validators:
67
- pschema[vol.Optional(val, description=description)] = value
68
- else:
69
- pschema[key] = value
70
- schema = pschema
71
-
72
56
  for key, value in schema.items():
73
57
  description = None
74
58
  if isinstance(key, vol.Marker):
@@ -86,13 +70,30 @@ def convert(schema: Any, *, custom_serializer: Callable | None = None) -> dict:
86
70
  pval["default"] = key.default()
87
71
 
88
72
  pval = ensure_default(pval)
89
- pkey = str(pkey)
90
- properties[pkey] = pval
73
+
74
+ if isinstance(pkey, type) and issubclass(pkey, str):
75
+ if additional_properties is None:
76
+ additional_properties = pval
77
+ elif isinstance(key, vol.Any):
78
+ for val in key.validators:
79
+ if isinstance(val, vol.Marker):
80
+ if val.description:
81
+ properties[str(val.schema)] = pval.copy()
82
+ properties[str(val.schema)]["description"] = val.description
83
+ else:
84
+ properties[str(val)] = pval
85
+ else:
86
+ properties[str(val)] = pval
87
+ else:
88
+ properties[str(pkey)] = pval
91
89
 
92
90
  if isinstance(key, vol.Required):
93
- required.append(pkey)
91
+ required.append(str(pkey))
94
92
 
95
- val = {"type": "object", "properties": properties, "required": required}
93
+ val = {"type": "object"}
94
+ if properties:
95
+ val["properties"] = properties
96
+ val["required"] = required
96
97
  if additional_properties:
97
98
  val["additionalProperties"] = additional_properties
98
99
  return val
@@ -186,6 +187,13 @@ def convert(schema: Any, *, custom_serializer: Callable | None = None) -> dict:
186
187
  if isinstance(schema, (str, int, float, bool)) or schema is None:
187
188
  return {"enum": [schema]}
188
189
 
190
+ if (
191
+ get_origin(schema) is list
192
+ or get_origin(schema) is set
193
+ or get_origin(schema) is tuple
194
+ ):
195
+ schema = [get_args(schema)[0]]
196
+
189
197
  if isinstance(schema, Sequence):
190
198
  if len(schema) == 1:
191
199
  return {
@@ -205,13 +213,19 @@ def convert(schema: Any, *, custom_serializer: Callable | None = None) -> dict:
205
213
  if schema in TYPES_MAP:
206
214
  return {"type": TYPES_MAP[schema]}
207
215
 
208
- if schema is list or schema is set:
209
- return {"type": "array", "items": ensure_default({})}
210
-
211
- if schema is dict:
212
- return {"type": "object", "additionalProperties": True}
216
+ if get_origin(schema) is dict:
217
+ if get_args(schema)[1] is Any or isinstance(get_args(schema)[1], TypeVar):
218
+ schema = dict
219
+ else:
220
+ return convert({get_args(schema)[0]: get_args(schema)[1]})
213
221
 
214
222
  if isinstance(schema, type):
223
+ if schema is dict:
224
+ return {"type": "object", "additionalProperties": True}
225
+
226
+ if schema is list or schema is set or schema is tuple:
227
+ return {"type": "array", "items": ensure_default({})}
228
+
215
229
  if issubclass(schema, Enum):
216
230
  return {"enum": [item.value for item in schema]}
217
231
  elif schema is NoneType:
@@ -231,19 +245,6 @@ def convert(schema: Any, *, custom_serializer: Callable | None = None) -> dict:
231
245
  schema = schema[0]
232
246
  else:
233
247
  return {}
234
- if get_origin(schema) is dict:
235
- schema = get_args(schema)[1]
236
- if schema is Any or isinstance(schema, TypeVar):
237
- schema = dict
238
- else:
239
- return {
240
- "type": "object",
241
- "additionalProperties": convert(
242
- schema, custom_serializer=custom_serializer
243
- ),
244
- }
245
- if get_origin(schema) is list or get_origin(schema) is set:
246
- schema = [get_args(schema)[0]]
247
248
 
248
249
  return convert(schema, custom_serializer=custom_serializer)
249
250
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: voluptuous-openapi
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Convert voluptuous schemas to OpenAPI Schema object
5
5
  Home-page: http://github.com/Shulyaka/voluptuous-openapi
6
6
  Author: Denis Shulyaka