Skip to content

Conversation

@douglasbgray
Copy link

@douglasbgray douglasbgray commented Jan 23, 2019

As stated in Issue 975, the reference resolver for properties does not fully recurse. This can lead to unresolved references when parsing with ParseOptions.setResolveFully(true) and there are properties with nested arrays/maps/composed schemas. The included test case has 10 variants of properties, of which 7 were not resolved fully before the change.

Prior to the fix, these are the failed test cases:

[ERROR] Failures: 
[ERROR]   OpenAPIV3ParserTest.testIssue975_allOf:1744 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]
[ERROR]   OpenAPIV3ParserTest.testIssue975_anyOf:1737 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]
[ERROR]   OpenAPIV3ParserTest.testIssue975_array_array:1715 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]
[ERROR]   OpenAPIV3ParserTest.testIssue975_array_map:1700 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]
[ERROR]   OpenAPIV3ParserTest.testIssue975_map_array:1707 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]
[ERROR]   OpenAPIV3ParserTest.testIssue975_map_map:1723 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]
[ERROR]   OpenAPIV3ParserTest.testIssue975_oneOf:1730 expected [#/components/schemas/Image] but found [./types.yaml#/components/schemas/Image]

One question - the tests are structured so that each Assert is in a separate test, to make it easy to see which properties were not parsed properly. If preferred, all tests can be merged into one test method, which is more compact. If so, I can close this PR and reissue with a PR in which the tests are replaced with this code:

    @Test(description = "Test that relative references are resolvable when property is a reference to a relative file.")
    @SuppressWarnings("unchecked")
    public void testIssue975() {
        ParseOptions options = new ParseOptions();
        options.setResolve(true);
        OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-975/contract/openapi.yaml", null, options).getOpenAPI();

        assertEquals(2, openAPI.getComponents().getSchemas().size());
        assertTrue(openAPI.getComponents().getSchemas().containsKey("Image"));
        assertTrue(openAPI.getComponents().getSchemas().containsKey("MyResponse"));

        Schema myResponseSchema = openAPI.getComponents().getSchemas().get("MyResponse");
        Map<String, Schema> properties = myResponseSchema.getProperties();
        assertEquals(10, properties.size());

        assertEquals( properties.get("images").get$ref(), "#/components/schemas/Image");
        assertEquals( ((ArraySchema) properties.get("imagesArray")).getItems().get$ref(), "#/components/schemas/Image");
        assertEquals( ((Schema) properties.get("imagesMap").getAdditionalProperties()).get$ref(), "#/components/schemas/Image");
        assertEquals( ((Schema) ((ArraySchema) properties.get("imagesArrayMap")).getItems().getAdditionalProperties()).get$ref(), "#/components/schemas/Image");
        assertEquals( ((ArraySchema) properties.get("imagesMapArray").getAdditionalProperties()).getItems().get$ref(), "#/components/schemas/Image");
        assertEquals( ((ArraySchema) ((ArraySchema) properties.get("imagesArrayArray")).getItems()).getItems().get$ref(), "#/components/schemas/Image");
        assertEquals( ((Schema) ((Schema) properties.get("imagesMapMap").getAdditionalProperties()).getAdditionalProperties()).get$ref(), "#/components/schemas/Image");
        assertEquals( ((ComposedSchema) properties.get("oneOfExample")).getOneOf().get(0).get$ref(), "#/components/schemas/Image");
        assertEquals( ((ComposedSchema) properties.get("anyOfExample")).getAnyOf().get(0).get$ref(), "#/components/schemas/Image");
        assertEquals( ((ComposedSchema) properties.get("allOfExample")).getAllOf().get(0).get$ref(), "#/components/schemas/Image");
    }

Copy link
Contributor

@gracekarina gracekarina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @douglasbgray

@gracekarina gracekarina merged commit a3d3a42 into swagger-api:master Jan 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants