diff --git a/flama/schemas/generator.py b/flama/schemas/generator.py index 602ffbd9..43012fdd 100644 --- a/flama/schemas/generator.py +++ b/flama/schemas/generator.py @@ -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 diff --git a/tests/schemas/test_generator.py b/tests/schemas/test_generator.py index 5882946e..db0afac8 100644 --- a/tests/schemas/test_generator.py +++ b/tests/schemas/test_generator.py @@ -48,7 +48,7 @@ def test_empty_init(self): assert SchemaRegistry() == {} @pytest.mark.parametrize( - "operation,exception", + "operation,output", [ pytest.param( openapi.Operation( @@ -65,7 +65,7 @@ def test_empty_init(self): } ) ), - None, + True, id="response_reference", ), pytest.param( @@ -88,7 +88,7 @@ def test_empty_init(self): } ) ), - None, + True, id="response_schema", ), pytest.param( @@ -116,7 +116,7 @@ def test_empty_init(self): } ) ), - None, + True, id="response_array", ), pytest.param( @@ -128,7 +128,7 @@ 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"}}} ), ) }, @@ -136,14 +136,14 @@ def test_empty_init(self): } ) ), - 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( @@ -160,7 +160,7 @@ def test_empty_init(self): ), responses=openapi.Responses({}), ), - None, + True, id="body_schema", ), pytest.param( @@ -182,7 +182,7 @@ def test_empty_init(self): ), responses=openapi.Responses({}), ), - None, + True, id="body_array", ), pytest.param( @@ -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( @@ -220,7 +220,7 @@ def test_empty_init(self): ], responses=openapi.Responses({}), ), - None, + True, id="parameter_schema", ), pytest.param( @@ -241,7 +241,7 @@ def test_empty_init(self): ], responses=openapi.Responses({}), ), - None, + True, id="parameter_array", ), pytest.param( @@ -250,12 +250,12 @@ 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( @@ -263,7 +263,7 @@ def test_empty_init(self): callbacks={"200": openapi.Reference(ref="#!/components/schemas/Foo")}, responses=openapi.Responses({}), ), - None, + True, id="callback_reference", ), pytest.param( @@ -299,7 +299,7 @@ def test_empty_init(self): }, responses=openapi.Responses({}), ), - None, + True, id="callback_schema", ), pytest.param( @@ -318,7 +318,7 @@ def test_empty_init(self): schema=openapi.Schema( { "type": "object", - "properties": {"foo": {"type": "array"}}, + "properties": {"foo": {"type": "foo"}}, } ) ) @@ -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",