Skip to content

Commit 1dd0ad9

Browse files
author
svlyubovsk
committed
* fix tests
* return one_of
1 parent f6f2b4b commit 1dd0ad9

File tree

13 files changed

+38
-35
lines changed

13 files changed

+38
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from litestar.openapi.plugins import SwaggerRenderPlugin
22

3-
swagger_plugin = SwaggerRenderPlugin(version="5.1.3", path="/swagger")
3+
swagger_plugin = SwaggerRenderPlugin(version="5.18.2", path="/swagger")

litestar/_openapi/schema_generation/schema.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,10 @@ def for_optional_field(self, field_definition: FieldDefinition) -> Schema:
426426
)
427427
if isinstance(schema_or_reference, Schema) and isinstance(schema_or_reference.one_of, list):
428428
result = schema_or_reference.one_of
429-
elif isinstance(schema_or_reference, Schema) and isinstance(schema_or_reference.any_of, list):
430-
result = schema_or_reference.any_of
431429
else:
432430
result = [schema_or_reference]
433431

434-
return Schema(any_of=[*result, Schema(type=OpenAPIType.NULL)])
432+
return Schema(one_of=[*result, Schema(type=OpenAPIType.NULL)])
435433

436434
def for_union_field(self, field_definition: FieldDefinition) -> Schema:
437435
"""Create a Schema for a union FieldDefinition.

litestar/openapi/controller.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class OpenAPIController(Controller):
3636
"""Base styling of the html body."""
3737
redoc_version: str = "next"
3838
"""Redoc version to download from the CDN."""
39-
swagger_ui_version: str = "5.1.3"
39+
swagger_ui_version: str = "5.18.2"
4040
"""SwaggerUI version to download from the CDN."""
4141
stoplight_elements_version: str = "7.7.18"
4242
"""StopLight Elements version to download from the CDN."""

litestar/openapi/plugins.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ class SwaggerRenderPlugin(OpenAPIRenderPlugin):
499499

500500
def __init__(
501501
self,
502-
version: str = "5.1.3",
502+
version: str = "5.18.2",
503503
js_url: str | None = None,
504504
css_url: str | None = None,
505505
standalone_preset_js_url: str | None = None,

tests/unit/test_contrib/test_piccolo_orm/test_piccolo_orm_dto.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,14 @@ def test_piccolo_dto_openapi_spec_generation() -> None:
139139
assert concert_schema
140140
assert concert_schema.to_schema() == {
141141
"properties": {
142-
"band_1": {"oneOf": [{"type": "null"}, {"type": "integer"}]},
143-
"band_2": {"oneOf": [{"type": "null"}, {"type": "integer"}]},
144-
"venue": {"oneOf": [{"type": "null"}, {"type": "integer"}]},
142+
"band_1": {"oneOf": [{"type": "integer"}, {"type": "null"}]},
143+
"band_2": {
144+
"oneOf": [
145+
{"type": "integer"},
146+
{"type": "null"},
147+
]
148+
},
149+
"venue": {"oneOf": [{"type": "integer"}, {"type": "null"}]},
145150
},
146151
"required": [],
147152
"title": "CreateConcertConcertRequestBody",
@@ -152,10 +157,10 @@ def test_piccolo_dto_openapi_spec_generation() -> None:
152157
assert record_studio_schema
153158
assert record_studio_schema.to_schema() == {
154159
"properties": {
155-
"facilities": {"oneOf": [{"type": "null"}, {"type": "string"}]},
156-
"facilities_b": {"oneOf": [{"type": "null"}, {"type": "string"}]},
157-
"microphones": {"oneOf": [{"type": "null"}, {"items": {"type": "string"}, "type": "array"}]},
158-
"id": {"oneOf": [{"type": "null"}, {"type": "integer"}]},
160+
"facilities": {"oneOf": [{"type": "string"}, {"type": "null"}]},
161+
"facilities_b": {"oneOf": [{"type": "string"}, {"type": "null"}]},
162+
"microphones": {"oneOf": [{"items": {"type": "string"}, "type": "array"}, {"type": "null"}]},
163+
"id": {"oneOf": [{"type": "integer"}, {"type": "null"}]},
159164
},
160165
"required": [],
161166
"title": "RetrieveStudioRecordingStudioResponseBody",
@@ -166,8 +171,8 @@ def test_piccolo_dto_openapi_spec_generation() -> None:
166171
assert venue_schema
167172
assert venue_schema.to_schema() == {
168173
"properties": {
169-
"id": {"oneOf": [{"type": "null"}, {"type": "integer"}]},
170-
"name": {"oneOf": [{"type": "null"}, {"type": "string"}]},
174+
"id": {"oneOf": [{"type": "integer"}, {"type": "null"}]},
175+
"name": {"oneOf": [{"type": "string"}, {"type": "null"}]},
171176
},
172177
"required": [],
173178
"title": "RetrieveVenuesVenueResponseBody",

tests/unit/test_openapi/test_endpoints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_default_redoc_cdn_urls(
3939
def test_default_swagger_ui_cdn_urls(
4040
person_controller: Type[Controller], pet_controller: Type[Controller], config: OpenAPIConfig
4141
) -> None:
42-
default_swagger_ui_version = "5.1.3"
42+
default_swagger_ui_version = "5.18.2"
4343
default_swagger_bundles = [
4444
f"https://cdn.jsdelivr.net/npm/swagger-ui-dist@{default_swagger_ui_version}/swagger-ui.css",
4545
f"https://cdn.jsdelivr.net/npm/swagger-ui-dist@{default_swagger_ui_version}/swagger-ui-bundle.js",

tests/unit/test_openapi/test_parameters.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ def test_create_parameters(person_controller: Type[Controller]) -> None:
105105
assert is_schema_value(gender.schema)
106106
assert gender.schema == Schema(
107107
one_of=[
108-
Schema(type=OpenAPIType.NULL),
109108
Reference(ref="#/components/schemas/tests_unit_test_openapi_utils_Gender"),
110109
Schema(
111110
type=OpenAPIType.ARRAY,
112111
items=Reference(ref="#/components/schemas/tests_unit_test_openapi_utils_Gender"),
113112
examples=[[Gender.MALE]],
114113
),
114+
Schema(type=OpenAPIType.NULL),
115115
],
116116
examples=[Gender.MALE, [Gender.MALE, Gender.OTHER]],
117117
)
@@ -390,8 +390,8 @@ async def handler(
390390
app = Litestar([handler])
391391
assert app.openapi_schema.paths["/{path_param}"].get.parameters[0].schema.type == OpenAPIType.STRING # type: ignore[index, union-attr]
392392
assert app.openapi_schema.paths["/{path_param}"].get.parameters[1].schema.one_of == [ # type: ignore[index, union-attr]
393-
Schema(type=OpenAPIType.NULL),
394393
Schema(type=OpenAPIType.STRING),
394+
Schema(type=OpenAPIType.NULL),
395395
]
396396
assert app.openapi_schema.paths["/{path_param}"].get.parameters[2].schema.type == OpenAPIType.STRING # type: ignore[index, union-attr]
397397
assert (

tests/unit/test_openapi/test_schema.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class TypedDictGeneric(TypedDict, Generic[T]):
389389
@pytest.mark.parametrize("cls", annotations)
390390
def test_schema_generation_with_generic_classes(cls: Any) -> None:
391391
expected_foo_schema = Schema(type=OpenAPIType.INTEGER)
392-
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.NULL), Schema(type=OpenAPIType.INTEGER)])
392+
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.INTEGER), Schema(type=OpenAPIType.NULL)])
393393

394394
properties = get_schema_for_field_definition(
395395
FieldDefinition.from_kwarg(name=get_name(cls), annotation=cls)
@@ -443,7 +443,7 @@ def test_schema_generation_with_generic_classes_constrained() -> None:
443443
)
444444
def test_schema_generation_with_pagination(annotation: Any) -> None:
445445
expected_foo_schema = Schema(type=OpenAPIType.INTEGER)
446-
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.NULL), Schema(type=OpenAPIType.INTEGER)])
446+
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.INTEGER), Schema(type=OpenAPIType.NULL)])
447447

448448
properties = get_schema_for_field_definition(FieldDefinition.from_annotation(annotation).inner_types[-1]).properties
449449

@@ -478,11 +478,11 @@ class Foo(Enum):
478478
assert isinstance(schema, Schema)
479479
assert schema.type is None
480480
assert schema.one_of is not None
481-
null_schema = schema.one_of[0]
481+
null_schema = schema.one_of[1]
482482
assert isinstance(null_schema, Schema)
483483
assert null_schema.type is not None
484484
assert null_schema.type is OpenAPIType.NULL
485-
enum_ref = schema.one_of[1]
485+
enum_ref = schema.one_of[0]
486486
assert isinstance(enum_ref, Reference)
487487
assert enum_ref.ref == "#/components/schemas/tests_unit_test_openapi_test_schema_test_optional_enum.Foo"
488488
enum_schema = creator.schema_registry.from_reference(enum_ref).schema
@@ -569,9 +569,9 @@ class ModelB(base_type): # type: ignore[no-redef, misc]
569569
FieldDefinition.from_kwarg(name="Lookup", annotation=Union[ModelA, ModelB, None])
570570
)
571571
assert schema.one_of == [
572-
Schema(type=OpenAPIType.NULL),
573572
Reference(ref="#/components/schemas/tests_unit_test_openapi_test_schema_test_type_union_with_none.ModelA"),
574573
Reference("#/components/schemas/tests_unit_test_openapi_test_schema_test_type_union_with_none.ModelB"),
574+
Schema(type=OpenAPIType.NULL),
575575
]
576576

577577

tests/unit/test_openapi/test_spec_generation.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def handler(data: cls) -> cls:
2727
"first_name": {"type": "string"},
2828
"last_name": {"type": "string"},
2929
"id": {"type": "string"},
30-
"optional": {"oneOf": [{"type": "null"}, {"type": "string"}]},
30+
"optional": {"oneOf": [{"type": "string"}, {"type": "null"}]},
3131
"complex": {
3232
"type": "object",
3333
"additionalProperties": {
@@ -37,11 +37,11 @@ def handler(data: cls) -> cls:
3737
},
3838
"pets": {
3939
"oneOf": [
40-
{"type": "null"},
4140
{
4241
"items": {"$ref": "#/components/schemas/DataclassPet"},
4342
"type": "array",
4443
},
44+
{"type": "null"},
4545
]
4646
},
4747
},
@@ -189,8 +189,8 @@ def test_recursive_schema_generation(
189189
"properties": {
190190
"a": {"$ref": "#/components/schemas/A"},
191191
"b": {"$ref": "#/components/schemas/B"},
192-
"opt_a": {"oneOf": [{"type": "null"}, {"$ref": "#/components/schemas/A"}]},
193-
"opt_b": {"oneOf": [{"type": "null"}, {"$ref": "#/components/schemas/B"}]},
192+
"opt_a": {"oneOf": [{"$ref": "#/components/schemas/A"}, {"type": "null"}]},
193+
"opt_b": {"oneOf": [{"$ref": "#/components/schemas/B"}, {"type": "null"}]},
194194
"list_a": {"items": {"$ref": "#/components/schemas/A"}, "type": "array"},
195195
"list_b": {"items": {"$ref": "#/components/schemas/B"}, "type": "array"},
196196
},
@@ -202,8 +202,8 @@ def test_recursive_schema_generation(
202202
"properties": {
203203
"a": {"$ref": "#/components/schemas/A"},
204204
"b": {"$ref": "#/components/schemas/B"},
205-
"opt_a": {"oneOf": [{"type": "null"}, {"$ref": "#/components/schemas/A"}]},
206-
"opt_b": {"oneOf": [{"type": "null"}, {"$ref": "#/components/schemas/B"}]},
205+
"opt_a": {"oneOf": [{"$ref": "#/components/schemas/A"}, {"type": "null"}]},
206+
"opt_b": {"oneOf": [{"$ref": "#/components/schemas/B"}, {"type": "null"}]},
207207
"list_a": {"items": {"$ref": "#/components/schemas/A"}, "type": "array"},
208208
"list_b": {"items": {"$ref": "#/components/schemas/B"}, "type": "array"},
209209
},

tests/unit/test_plugins/test_attrs/test_schema_plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AttrsGeneric(Generic[T]):
2323
def test_schema_generation_with_generic_classes() -> None:
2424
cls = AttrsGeneric[int]
2525
expected_foo_schema = Schema(type=OpenAPIType.INTEGER)
26-
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.NULL), Schema(type=OpenAPIType.INTEGER)])
26+
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.INTEGER), Schema(type=OpenAPIType.NULL)])
2727

2828
field_definition = FieldDefinition.from_kwarg(name=get_name(cls), annotation=cls)
2929
properties = get_schema_for_field_definition(field_definition, plugins=[AttrsSchemaPlugin()]).properties

tests/unit/test_plugins/test_attrs/test_schema_spec_generation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def handler(data: Person) -> Person:
2929
"first_name": {"type": "string"},
3030
"last_name": {"type": "string"},
3131
"id": {"type": "string"},
32-
"optional": {"oneOf": [{"type": "null"}, {"type": "string"}]},
32+
"optional": {"oneOf": [{"type": "string"}, {"type": "null"}]},
3333
"complex": {
3434
"type": "object",
3535
"additionalProperties": {
@@ -39,11 +39,11 @@ def handler(data: Person) -> Person:
3939
},
4040
"pets": {
4141
"oneOf": [
42-
{"type": "null"},
4342
{
4443
"items": {"$ref": "#/components/schemas/DataclassPet"},
4544
"type": "array",
4645
},
46+
{"type": "null"},
4747
]
4848
},
4949
},

tests/unit/test_plugins/test_pydantic/test_openapi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def handler(data: cls) -> cls:
465465
"first_name": {"type": "string"},
466466
"last_name": {"type": "string"},
467467
"id": {"type": "string"},
468-
"optional": {"oneOf": [{"type": "null"}, {"type": "string"}]},
468+
"optional": {"oneOf": [{"type": "string"}, {"type": "null"}]},
469469
"complex": {
470470
"type": "object",
471471
"additionalProperties": {
@@ -476,11 +476,11 @@ def handler(data: cls) -> cls:
476476
"union": {"oneOf": [{"type": "integer"}, {"items": {"type": "string"}, "type": "array"}]},
477477
"pets": {
478478
"oneOf": [
479-
{"type": "null"},
480479
{
481480
"items": {"$ref": "#/components/schemas/DataclassPet"},
482481
"type": "array",
483482
},
483+
{"type": "null"},
484484
]
485485
},
486486
},

tests/unit/test_plugins/test_pydantic/test_schema_plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_schema_generation_with_generic_classes(model: Type[Union[PydanticV1Gene
3838
field_definition = FieldDefinition.from_kwarg(name=get_name(cls), annotation=cls)
3939
properties = get_schema_for_field_definition(field_definition, plugins=[PydanticSchemaPlugin()]).properties
4040
expected_foo_schema = Schema(type=OpenAPIType.INTEGER)
41-
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.NULL), Schema(type=OpenAPIType.INTEGER)])
41+
expected_optional_foo_schema = Schema(one_of=[Schema(type=OpenAPIType.INTEGER), Schema(type=OpenAPIType.NULL)])
4242

4343
assert properties
4444
assert properties["foo"] == expected_foo_schema

0 commit comments

Comments
 (0)