Skip to content

Commit

Permalink
✅ Fixes openapi generator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
perdy committed Jan 19, 2023
1 parent 70f1f54 commit b8b0cdf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
14 changes: 4 additions & 10 deletions flama/schemas/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,10 @@ def _get_schema_references_from_schema(

result = []
for name, prop in schema.get("properties", {}).items():
try:
if "$ref" in prop:
result.append(prop["$ref"])
elif prop.get("type", "") == "array":
try:
result.append(prop["items"]["$ref"])
except (TypeError, KeyError):
...
except KeyError as e:
raise SchemaGenerationError from e
if "$ref" in prop:
result.append(prop["$ref"])
elif prop.get("type", "") == "array" and prop.get("items", {}).get("$ref"):
result.append(prop["items"]["$ref"])

return result

Expand Down
49 changes: 24 additions & 25 deletions tests/schemas/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_empty_init(self):
assert SchemaRegistry() == {}

@pytest.mark.parametrize(
"operation,exception",
"operation,output",
[
pytest.param(
openapi.Operation(
Expand All @@ -65,7 +65,7 @@ def test_empty_init(self):
}
)
),
None,
True,
id="response_reference",
),
pytest.param(
Expand All @@ -88,7 +88,7 @@ def test_empty_init(self):
}
)
),
None,
True,
id="response_schema",
),
pytest.param(
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_empty_init(self):
}
)
),
None,
True,
id="response_array",
),
pytest.param(
Expand All @@ -128,22 +128,22 @@ def test_empty_init(self):
content={
"application/json": openapi.MediaType(
schema=openapi.Schema(
{"type": "object", "properties": {"foo": {"type": "array"}}}
{"type": "object", "properties": {"foo": {"type": "foo"}}}
),
)
},
)
}
)
),
SchemaGenerationError,
False,
id="response_wrong",
),
pytest.param(
openapi.Operation(
requestBody=openapi.Reference(ref="#!/components/schemas/Foo"), responses=openapi.Responses({})
),
None,
True,
id="body_reference",
),
pytest.param(
Expand All @@ -160,7 +160,7 @@ def test_empty_init(self):
),
responses=openapi.Responses({}),
),
None,
True,
id="body_schema",
),
pytest.param(
Expand All @@ -182,7 +182,7 @@ def test_empty_init(self):
),
responses=openapi.Responses({}),
),
None,
True,
id="body_array",
),
pytest.param(
Expand All @@ -191,20 +191,20 @@ def test_empty_init(self):
description="Foo",
content={
"application/json": openapi.MediaType(
schema=openapi.Schema({"type": "object", "properties": {"foo": {"type": "array"}}}),
schema=openapi.Schema({"type": "object", "properties": {"foo": {"type": "foo"}}}),
)
},
),
responses=openapi.Responses({}),
),
SchemaGenerationError,
False,
id="body_wrong",
),
pytest.param(
openapi.Operation(
parameters=[openapi.Reference(ref="#!/components/schemas/Foo")], responses=openapi.Responses({})
),
None,
True,
id="parameter_reference",
),
pytest.param(
Expand All @@ -220,7 +220,7 @@ def test_empty_init(self):
],
responses=openapi.Responses({}),
),
None,
True,
id="parameter_schema",
),
pytest.param(
Expand All @@ -241,7 +241,7 @@ def test_empty_init(self):
],
responses=openapi.Responses({}),
),
None,
True,
id="parameter_array",
),
pytest.param(
Expand All @@ -250,20 +250,20 @@ def test_empty_init(self):
openapi.Parameter(
in_="query",
name="foo",
schema=openapi.Schema({"type": "object", "properties": {"foo": {"type": "array"}}}),
schema=openapi.Schema({"type": "object", "properties": {"foo": {"type": "foo"}}}),
)
],
responses=openapi.Responses({}),
),
SchemaGenerationError,
False,
id="parameter_wrong",
),
pytest.param(
openapi.Operation(
callbacks={"200": openapi.Reference(ref="#!/components/schemas/Foo")},
responses=openapi.Responses({}),
),
None,
True,
id="callback_reference",
),
pytest.param(
Expand Down Expand Up @@ -299,7 +299,7 @@ def test_empty_init(self):
},
responses=openapi.Responses({}),
),
None,
True,
id="callback_schema",
),
pytest.param(
Expand All @@ -318,7 +318,7 @@ def test_empty_init(self):
schema=openapi.Schema(
{
"type": "object",
"properties": {"foo": {"type": "array"}},
"properties": {"foo": {"type": "foo"}},
}
)
)
Expand All @@ -333,16 +333,15 @@ def test_empty_init(self):
},
responses=openapi.Responses({}),
),
SchemaGenerationError,
False,
id="callback_wrong",
),
],
indirect=["exception"],
)
def test_used(self, registry, foo_schema, spec, operation, exception):
with exception:
spec.add_path("/", openapi.Path(get=operation))
assert registry.used(spec) == {id(foo_schema): registry[foo_schema]}
def test_used(self, registry, foo_schema, spec, operation, output):
expected_output = {id(foo_schema): registry[foo_schema]} if output else {}
spec.add_path("/", openapi.Path(get=operation))
assert registry.used(spec) == expected_output

@pytest.mark.parametrize(
"schema,name,exception",
Expand Down

0 comments on commit b8b0cdf

Please sign in to comment.