Java Code Examples for io.swagger.v3.parser.core.models.ParseOptions#setResolveFully()

The following examples show how to use io.swagger.v3.parser.core.models.ParseOptions#setResolveFully() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue1177(@Injectable final List<AuthorizationValue> auths) {
    String path = "/issue-1177/swagger.yaml";

    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();

    // $ref response with $ref header
    ApiResponse petsListApiResponse = openAPI.getPaths().get("/update-pets").getPost().getResponses().get("200");
    assertNotNull(petsListApiResponse);

    Header sessionIdHeader = petsListApiResponse.getHeaders().get("x-session-id");
    assertNotNull(sessionIdHeader);

    Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
    assertNotNull(petsListSchema);

    assertNotNull(openAPI.getComponents().getHeaders());
    Header sessionIdHeaderComponent = openAPI.getComponents().getHeaders().get("x-session-id");
    assertTrue(sessionIdHeader == sessionIdHeaderComponent);

    assertTrue(petsListApiResponse.getContent().get("application/json").getSchema() == petsListSchema);
}
 
Example 2
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void recursiveIssue984() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(true);
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("issue-984-simple.yaml", null, parseOptions);
    if (openAPI == null) fail("failed parsing issue-984");
    try {
        Json.pretty(openAPI);
        //System.out.println(Json.pretty(openAPI));
    }
    catch (Exception e) {
        e.printStackTrace();
        fail("Recursive loop found");
    }
}
 
Example 3
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testComposedSchemaAdjacent(@Injectable final List<AuthorizationValue> auths) throws Exception {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/composedSchemaRef.yaml", auths, options);

    Assert.assertNotNull(openAPI);

    Assert.assertTrue(openAPI.getComponents().getSchemas().size() == 5);
    Schema schema = openAPI.getPaths().get("/path").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
    Assert.assertTrue(schema.getProperties().size() == 4);

    ComposedSchema schemaOneOf = (ComposedSchema) openAPI.getPaths().get("/oneOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
    Assert.assertTrue(schemaOneOf.getOneOf().size() == 3);

    ComposedSchema schemaAnyOf = (ComposedSchema) openAPI.getPaths().get("/anyOf").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
    Assert.assertTrue(schemaAnyOf.getAnyOf().size() == 3);

}
 
Example 4
Source File: SyntaxChecker.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
public static List<String> check(String oasSpecContent) {

    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(false);
    parseOptions.setResolveFully(false);
    parseOptions.setResolveCombinators(false);
    parseOptions.setFlatten(false);

    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(oasSpecContent, null, parseOptions);
    return result.getMessages() == null ? emptyList() : result.getMessages();

  }
 
Example 5
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1352(@Injectable final List<AuthorizationValue> auths) {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);

    OpenAPI openAPI= new OpenAPIV3Parser().readLocation("issue-1352.json", auths, options).getOpenAPI();
    assertNull(openAPI.getPaths().get("/responses").getPatch().getResponses().get("200").getHeaders().get("x-my-secret-header").getSchema().get$ref());

}
 
Example 6
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void recursiveResolving() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("recursive.yaml", null, parseOptions);
    Assert.assertNotNull(openAPI.getPaths().get("/myPath").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getProperties().get("myProp"));
    try {
        Json.mapper().writeValueAsString(openAPI);
    }
    catch (Exception e) {
        fail("Recursive loop found");
    }

}
 
Example 7
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void referringSpecWithoutComponentsTag() throws Exception {
    ParseOptions resolve = new ParseOptions();
    resolve.setResolveFully(true);
    final OpenAPI openAPI = new OpenAPIV3Parser().read("./ref-without-component/a.yaml", null, resolve);
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    Assert.assertEquals("Example value", schemas.get("CustomerType").getExample());
}
 
Example 8
Source File: ResponseExamplesTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testRandomRequestedXmlExample(@Injectable final List<io.swagger.v3.parser.core.models.AuthorizationValue> auths) throws Exception {
    Configuration config = new Configuration();
    List<String> exampleProcessor = new ArrayList<>();
    exampleProcessor.add("random");
    config.setExampleProcessors(exampleProcessor);
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readLocation( "src/test/swagger/oas3.yaml",auths, options).getOpenAPI();
    Operation operation = openAPI.getPaths().get( "/mockResponses/objectMultipleExamples").getGet();

    OpenAPIOperationController controller = new OpenAPIOperationController(
            config, "/mockResponses/objectMultipleExamples", "GET", operation, "" ,openAPI.getComponents().getSchemas() );

    ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
    UriInfo uriInfo = mock( UriInfo.class );

    stub( uriInfo.getPath()).toReturn( "/mockResponses/objectMultipleExamples");
    stub( uriInfo.getQueryParameters()).toReturn( new MultivaluedHashMap<String, String>());
    stub( uriInfo.getPathParameters()).toReturn( new MultivaluedHashMap<String, String>());


    stub( requestContext.getHeaders()).toReturn( new MultivaluedHashMap<String, String>());
    requestContext.getHeaders().add("Accept","application/xml");
    stub( requestContext.getUriInfo()).toReturn( uriInfo );

    Response response = controller.apply( requestContext );

    assertEquals( 200, response.getStatus() );
    assertEquals( "xml", response.getMediaType().getSubtype() );

    assertNotNull( response.getEntity());

}
 
Example 9
Source File: ResponseExamplesTest.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
@Test
public void testRandomRequestedJsonExample(@Injectable final List<io.swagger.v3.parser.core.models.AuthorizationValue> auths) throws Exception {
    Configuration config = new Configuration();
    List<String> exampleProcessor = new ArrayList<>();
    exampleProcessor.add("random");
    config.setExampleProcessors(exampleProcessor);
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readLocation( "src/test/swagger/oas3.yaml",auths, options).getOpenAPI();
    Operation operation = openAPI.getPaths().get( "/mockResponses/objectMultipleExamples").getGet();

    OpenAPIOperationController controller = new OpenAPIOperationController(
            config, "/mockResponses/objectMultipleExamples", "GET",operation,"", openAPI.getComponents().getSchemas() );

    ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
    UriInfo uriInfo = mock( UriInfo.class );

    stub( uriInfo.getPath()).toReturn( "/mockResponses/objectMultipleExamples");
    stub( uriInfo.getQueryParameters()).toReturn( new MultivaluedHashMap<String, String>());
    stub( uriInfo.getPathParameters()).toReturn( new MultivaluedHashMap<String, String>());


    stub( requestContext.getHeaders()).toReturn( new MultivaluedHashMap<String, String>());
    requestContext.getHeaders().add("Accept","application/json");
    stub( requestContext.getUriInfo()).toReturn( uriInfo );

    Response response = controller.apply( requestContext );

    assertEquals( 200, response.getStatus() );
    assertEquals( "json", response.getMediaType().getSubtype() );

    assertNotNull( response.getEntity());

}
 
Example 10
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue915() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/issue_918.yaml", null, parseOptions);
    Map<String, Header> headers = openAPI.getPaths().get("/2.0/users/").getGet().getResponses().get("200").getHeaders();
    String description = headers.get("X-Rate-Limit").getDescription();
    assertEquals(description, "The number of allowed requests in the current period");
}
 
Example 11
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveFully() throws Exception{
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
    pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);

    SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, new ArrayList<>(), options  );

    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getOpenAPI());
    assertEquals(result.getOpenAPI().getOpenapi(), "3.0.1");
    assertEquals(result.getOpenAPI().getComponents().getSchemas().get("OrderRef").getType(),"object");
}
 
Example 12
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyNameMixup() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("simple.yaml", null, parseOptions);
    assertEquals(((StringSchema)openAPI.getComponents().getSchemas().get("Manufacturer").getProperties().get("name")).getExample(), "ACME Corporation");
    Schema schema = openAPI.getPaths().get("/inventory").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
    assertEquals(((ObjectSchema) ((ArraySchema) schema).getItems().getProperties().get("manufacturer")).getProperties().get("name").getExample(), "ACME Corporation");
}
 
Example 13
Source File: BodyGeneratorUnitTest.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
private OpenAPI parseResource(String fileName) throws IOException {
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);
    String defn =
            FileUtils.readFileToString(
                    new File(this.getClass().getResource(fileName).getFile()), "UTF-8");
    return new OpenAPIV3Parser().readContents(defn, new ArrayList<>(), options).getOpenAPI();
}
 
Example 14
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void allOfExampleGeneration(@Injectable final List<AuthorizationValue> auths) throws JsonProcessingException {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/simpleAllOf.yaml", null, options);

    Assert.assertNotNull(openAPI);
    Object withoutExample = openAPI.getPaths().get("/foo").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getExample();
    Assert.assertNull(withoutExample);

    Object withExample = openAPI.getPaths().get("/bar").getGet().getResponses().get("200").getContent().get("application/json").getSchema().getExample();
    Assert.assertEquals("{\"someProperty\":\"ABC\",\"someOtherProperty\":42}", Json.mapper().writeValueAsString(withExample));
}
 
Example 15
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolveEmpty(@Injectable final List<AuthorizationValue> auths) throws Exception{
    String pathFile = FileUtils.readFileToString(new File("src/test/resources/empty-oas.yaml"));
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);

    SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, auths, options  );

    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getOpenAPI());
}
 
Example 16
Source File: ResponseExamplesTest.java    From swagger-inflector with Apache License 2.0 4 votes vote down vote up
public void testRandomJsonExample(@Injectable final List<io.swagger.v3.parser.core.models.AuthorizationValue> auths) throws Exception {
    Configuration config = new Configuration();
    List<String> exampleProcessor = new ArrayList<>();
    exampleProcessor.add("random");
    config.setExampleProcessors(exampleProcessor);
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readLocation( "src/test/swagger/oas3.yaml",auths, options).getOpenAPI();
    Operation operation = openAPI.getPaths().get( "/mockResponses/objectMultipleExamples").getGet();

    OpenAPIOperationController controller = new OpenAPIOperationController(
            config, "/mockResponses/objectMultipleExamples", "GET", operation,"" ,openAPI.getComponents().getSchemas() );

    ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
    UriInfo uriInfo = mock( UriInfo.class );

    stub( uriInfo.getPath()).toReturn( "/mockResponses/objectMultipleExamples");
    stub( uriInfo.getQueryParameters()).toReturn( new MultivaluedHashMap<String, String>());
    stub( uriInfo.getPathParameters()).toReturn( new MultivaluedHashMap<String, String>());

    stub( requestContext.getAcceptableMediaTypes()).toReturn(Arrays.asList(MediaType.APPLICATION_JSON_TYPE));
    stub( requestContext.getHeaders()).toReturn( new MultivaluedHashMap<String, String>());
    stub( requestContext.getUriInfo()).toReturn( uriInfo );

    Response response = controller.apply( requestContext );

    assertEquals( 200, response.getStatus() );
    io.swagger.v3.oas.models.examples.Example example1 = (Example) response.getEntity();
    assertNotNull( Json.mapper().writeValueAsString(example1));

    Response response1 = controller.apply( requestContext );

    assertEquals( 200, response1.getStatus() );
    io.swagger.v3.oas.models.examples.Example example2 = (Example) response1.getEntity();
    assertNotNull( Json.mapper().writeValueAsString(example2));


    if(example1 != example2) {
        different++;
        assertNotEquals(example1, example2);
    }else{
        equal++;
        assertEquals(example1, example2);
    }
}
 
Example 17
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void selfReferenceTest(@Injectable final List<AuthorizationValue> auths) {
    String yaml = "" +
            "openapi: 3.0.1\n" +
            "paths:\n" +
            "  /selfRefA:\n" +
            "    get:\n" +
            "      requestBody:\n" +
            "        content:\n" +
            "          application/json:\n" +
            "            schema:\n" +
            "              $ref: '#/components/schemas/ModelA'\n" +
            "      responses:\n" +
            "        default:\n" +
            "          description: Default response\n" +
            "  /selfRefB:\n" +
            "    get:\n" +
            "      requestBody:\n" +
            "        content:\n" +
            "          application/json:\n" +
            "            schema:\n" +
            "              $ref: '#/components/schemas/ModelB'\n" +
            "      responses:\n" +
            "        default:\n" +
            "          description: Default response\n" +
            "  /selfRefC:\n" +
            "    get:\n" +
            "      requestBody:\n" +
            "        content:\n" +
            "          application/json:\n" +
            "            schema:\n" +
            "              $ref: '#/components/schemas/ModelC'\n" +
            "      responses:\n" +
            "        default:\n" +
            "          description: Default response\n" +
            "  /selfRefD:\n" +
            "    get:\n" +
            "      responses:\n" +
            "        default:\n" +
            "          description: Default response\n" +
            "          content:\n" +
            "            '*/*':\n" +
            "              schema:\n" +
            "                $ref: '#/components/schemas/ModelA'\n" +
            "  /selfRefE:\n" +
            "    get:\n" +
            "      responses:\n" +
            "        default:\n" +
            "          description: Default response\n" +
            "          content:\n" +
            "            '*/*':\n" +
            "              schema:\n" +
            "                type: array\n" +
            "                items:\n" +
            "                  $ref: '#/components/schemas/ModelA'\n" +
            "info:\n" +
            "  version: ''\n" +
            "  title: ''\n" +
            "components:\n" +
            "  schemas:\n" +
            "    ModelA:\n" +
            "      properties:\n" +
            "        modelB:\n" +
            "          $ref: '#/components/schemas/ModelB'\n" +
            "    ModelB:\n" +
            "      properties:\n" +
            "        modelB:\n" +
            "          $ref: '#/components/schemas/ModelB'\n" +
            "    ModelC:\n" +
            "      properties:\n" +
            "        modelA:\n" +
            "          $ref: '#/components/schemas/ModelA'";

    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml,auths,options).getOpenAPI();

    Schema schemaB = openAPI.getPaths().get("/selfRefB").getGet().getRequestBody().getContent().get("application/json").getSchema();
    assertTrue(schemaB instanceof Schema);

    assertEquals(openAPI.getComponents().getSchemas().get("ModelB"), schemaB);

    Schema schema = openAPI.getPaths().get("/selfRefE").getGet().getResponses().get("default").getContent().get("*/*").getSchema();
    assertTrue(schema instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) schema;
    assertEquals(openAPI.getComponents().getSchemas().get("ModelA"), arraySchema.getItems());

}
 
Example 18
Source File: OpenApiFactory.java    From swagger-brake with Apache License 2.0 4 votes vote down vote up
private OpenAPI loadV3Api(String path) {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    return new OpenAPIV3Parser().read(path, null, parseOptions);
}
 
Example 19
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testSelfReferenceResolution(@Injectable final List<AuthorizationValue> auths)throws Exception {

    String yaml = "" +
            "openapi: 3.0.1\n" +
            "paths:\n" +
            "  \"/selfRefB\":\n" +
            "    get:\n" +
            "      requestBody:\n" +
            "        description: user to add to the system\\n\"+\n" +
            "        content:\n" +
            "         'application/json':\n" +
            "             schema:\n" +
            "                $ref: '#/components/schemas/SchemaB'\n" +
            "components:\n" +
            "  schemas:\n" +
            "    SchemaA:\n" +
            "      properties:\n" +
            "        name:\n" +
            "          type: string\n" +
            "        modelB:\n" +
            "          $ref: '#/components/schemas/SchemaB'\n" +
            "    SchemaB:\n" +
            "      properties:\n" +
            "        modelB:\n" +
            "          $ref: '#/components/schemas/SchemaB'";

    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml,auths,options).getOpenAPI();
    ResolverFully resolverUtil = new ResolverFully();
    resolverUtil.resolveFully(openAPI);


    RequestBody body = openAPI.getPaths().get("/selfRefB").getGet().getRequestBody();
    Schema schema = body.getContent().get("application/json").getSchema();

    assertEquals(schema,openAPI.getComponents().getSchemas().get("SchemaB"));
}
 
Example 20
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 3 votes vote down vote up
@Test
public void resolveAllOfWithoutAggregatingParameters(@Injectable final List<AuthorizationValue> auths) {
    ParseOptions options = new ParseOptions();
    options.setResolveFully(true);
    options.setResolveCombinators(false);

    // Testing components/schemas
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation("src/test/resources/composed.yaml",auths,options).getOpenAPI();

    ComposedSchema allOf = (ComposedSchema) openAPI.getComponents().getSchemas().get("ExtendedAddress");
    assertEquals(allOf.getAllOf().size(), 2);

    assertTrue(allOf.getAllOf().get(0).getProperties().containsKey("street"));
    assertTrue(allOf.getAllOf().get(1).getProperties().containsKey("gps"));


    // Testing path item
    ComposedSchema schema = (ComposedSchema) openAPI.getPaths().get("/withInvalidComposedModel").getPost().getRequestBody().getContent().get("application/json").getSchema();

    // In fact the schema resolved previously is the same of /withInvalidComposedModel
    assertEquals(schema, allOf);

    assertEquals(schema.getAllOf().size(), 2);

    assertTrue(schema.getAllOf().get(0).getProperties().containsKey("street"));
    assertTrue(schema.getAllOf().get(1).getProperties().containsKey("gps"));

}