Java Code Examples for io.swagger.v3.parser.OpenAPIV3Parser#readLocation()

The following examples show how to use io.swagger.v3.parser.OpenAPIV3Parser#readLocation() . 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 testParseOptionsSkipMatchesFalse() {
    final String location = "src/test/resources/skipMatches.yaml";

    final ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setSkipMatches(false);

    final OpenAPIV3Parser parserUnderTest = new OpenAPIV3Parser();

    final SwaggerParseResult result = parserUnderTest.readLocation(location, null, options);

    final OpenAPI openAPI = result.getOpenAPI();

    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertEquals(4, openAPI.getComponents().getSchemas().size());
}
 
Example 2
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseOptionsSkipMatchesTrue() {
    final String location = "src/test/resources/skipMatches.yaml";

    final ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setSkipMatches(true);

    final OpenAPIV3Parser parserUnderTest = new OpenAPIV3Parser();
    final SwaggerParseResult result = parserUnderTest.readLocation(location, null, options);
    final OpenAPI openAPI = result.getOpenAPI();

    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertEquals(6, openAPI.getComponents().getSchemas().size());
}
 
Example 3
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testSampleParser() {
    final String location = "src/test/resources/issue-1211.json";

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

    final OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final SwaggerParseResult result = parser.readLocation(location, null, options);
    System.out.println(result.getMessages());
    OpenAPI openAPI = result.getOpenAPI();

    assertNotNull(result.getOpenAPI());
    assertTrue(result.getMessages().size() > 0);
    assertEquals(result.getMessages().get(0).contains("attribute components.schemas.Pet. writeOnly and readOnly are both present"), true);

}
 
Example 4
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodegenIssue8601() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("codegen-issue-8601.yaml", null, options);

    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI.getComponents().getSchemas().get("status"));
    assertNotNull(openAPI.getComponents().getSchemas().get("body"));
    assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200"));
    assertNotNull(openAPI.getComponents().getSchemas().get("body_1"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Test1"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Test2"));
}
 
Example 5
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldParseApiWithMultipleParameterReferences() {
    // given
    String location = "src/test/resources/issue-1063/api.yaml";
    ParseOptions options = new ParseOptions();
    OpenAPIV3Parser tested = new OpenAPIV3Parser();

    // when
    SwaggerParseResult result = tested.readLocation(location, emptyList(), options);

    // then
    OpenAPI api = result.getOpenAPI();
    Map<String, Parameter> parameters = api.getComponents().getParameters();
    assertThat(parameters.keySet(), equalTo(new HashSet<>(asList("IdParam", "NameParam"))));
    assertThat(parameters.get("IdParam").getName(), equalTo("id"));
    assertThat(parameters.get("NameParam").getName(), equalTo("name"));
    
    assertThat(result.getMessages(), equalTo(emptyList()));

}
 
Example 6
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testPetstore() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readLocation("src/test/resources/petstore.yaml", null, options);

    assertNotNull(result);
    assertTrue(result.getMessages().size()==2);

    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    Set<String> expectedDefinitions = new HashSet<String>();
    expectedDefinitions.add("User");
    expectedDefinitions.add("Category");
    expectedDefinitions.add("Pet");
    expectedDefinitions.add("Tag");
    expectedDefinitions.add("Order");
    expectedDefinitions.add("PetArray");
    assertEquals(definitions.keySet(), expectedDefinitions);

    Schema petModel = definitions.get("Pet");
    Set<String> expectedPetProps = new HashSet<String>();
    expectedPetProps.add("id");
    expectedPetProps.add("category");
    expectedPetProps.add("name");
    expectedPetProps.add("photoUrls");
    expectedPetProps.add("tags");
    expectedPetProps.add("status");
    assertEquals(petModel.getProperties().keySet(), expectedPetProps);

    ArraySchema petArrayModel = (ArraySchema) definitions.get("PetArray");
    assertEquals(petArrayModel.getType(), "array");
    Schema refProp = petArrayModel.getItems();
    assertEquals(refProp.get$ref(), "#/components/schemas/Pet");
    assertNull(petArrayModel.getProperties());
}
 
Example 7
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void issueRelativeRefs2() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);

    final SwaggerParseResult result = parser.readLocation("src/test/resources/relative-upper-directory/swagger.yaml", null, options);
    Assert.assertNotNull(result.getOpenAPI());
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI.getPaths().get("/api/Address").getGet());
    assertTrue(openAPI.getComponents().getSchemas().size() == 1);
    assertNotNull(openAPI.getComponents().getSchemas().get("AddressEx"));
}
 
Example 8
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void issue682() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);

    final SwaggerParseResult result = parser.readLocation("src/test/resources/sample/SwaggerPetstore.yaml", null, options);
    Assert.assertNotNull(result.getOpenAPI());
    Assert.assertTrue(result.getMessages().isEmpty());
    Assert.assertNotNull(result.getOpenAPI().getPaths().get("/pets").getGet());
}
 
Example 9
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1190() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("issue1190/issue1190.yaml", null, options);

    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI.getComponents().getSchemas().get("SomeObj_lorem"));
}
 
Example 10
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotFlattenComposedSchema() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);

    SwaggerParseResult parseResult = openApiParser.readLocation("flattenComposedSchemaComplete.json", null, options);

    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNull(openAPI.getComponents().getSchemas().get("val_Members_val_member"));
    assertNotNull(openAPI.getComponents().getSchemas().get("val_MemberProducts_val_product"));
}
 
Example 11
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadNestedItemsReferences() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readLocation("src/test/resources/nested-items-references/b.yaml", null, options);
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertTrue(definitions.containsKey("z"));
    assertTrue(definitions.containsKey("w"));
}
 
Example 12
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue_1292() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("issue-1292/petstore.yml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI.getPaths().get("/pets").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/Pets");
    assertNotNull(openAPI.getPaths().get("/pets").getGet().getResponses().getDefault().getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/Error");
    assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Pets"));
    assertNotNull(openAPI.getComponents().getSchemas().get("Error"));
}
 
Example 13
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateHttpStatusCodesYaml() {
    final String location = "src/test/resources/duplicateHttpStatusCodes.yaml";

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

    final OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final SwaggerParseResult result = parser.readLocation(location, null, options);
    assertNull(result.getOpenAPI());
    List<String> messages = result.getMessages();
    assertEquals(1, messages.size());
    assertEquals(messages.get(0), "Duplicate field '200' in `src/test/resources/duplicateHttpStatusCodes.yaml`");

}
 
Example 14
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1309() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("issue-1309.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI);
    assertEquals(parseResult.getMessages().get(0),"attribute components.schemas.customer-not-found.examples is unexpected");
}
 
Example 15
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testRelativePath2() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult readResult = parser.readLocation("src/test/resources/codegen-remote-responses/openapi.yaml", null, options);
    Assert.assertEquals(readResult.getOpenAPI().getPaths().get("/pet/findByTags").getGet().getResponses().get("default").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ErrorModel");
}
 
Example 16
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssueFlattenComposedSchemaInlineModel() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setFlattenComposedSchemas(true);
    options.setCamelCaseFlattenNaming(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("FlattenComposedSchemasAtComponents.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI);
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("contact-base-model")).getAllOf().get(0).get$ref(),"#/components/schemas/ContactBaseModelAllOf_1");
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Test")).getOneOf().get(1).get$ref(),"#/components/schemas/TestOneOf_2");
}
 
Example 17
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testCamelCaseFlattenNamingFalse() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setFlattenComposedSchemas(true);
    options.setCamelCaseFlattenNaming(false);
    SwaggerParseResult parseResult = openApiParser.readLocation("FlattenComposedSchemasAtComponents.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();
    assertNotNull(openAPI);
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("contact-base-model")).getAllOf().get(0).get$ref(),"#/components/schemas/contactbasemodelAllOf_1");
}
 
Example 18
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssueFlattenArraySchemaItemsInlineModelTrue() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setFlattenComposedSchemas(true);
    options.setCamelCaseFlattenNaming(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("flattenArrayItems.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();

    //responses
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_items200")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw");
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_400"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_400")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw3");

    //parameters
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw1");
    assertNotNull(openAPI.getComponents().getSchemas().get("Pagelimit"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Pagelimit")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw2");

    //requestBodies
    assertNotNull(openAPI.getComponents().getSchemas().get("Body"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Body")).getAllOf().get(1).get$ref(),"#/components/schemas/requestBodiesAllOf_2");
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_body_items_applicationxml_requestBodies")).getAllOf().get(1).get$ref(),"#/components/schemas/ApplicationxmlAllOf_2");

    //components
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest")).getOneOf().get(1).get$ref(),"#/components/schemas/ArrayTestOneOf_2");
}
 
Example 19
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateHttpStatusCodesJson() {
    final String location = "src/test/resources/duplicateHttpStatusCodes.json";

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

    final OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final SwaggerParseResult result = parser.readLocation(location, null, options);
    assertNull(result.getOpenAPI());
    List<String> messages = result.getMessages();
    assertEquals(1, messages.size());
    assertEquals(messages.get(0), "Duplicate field '200' in `src/test/resources/duplicateHttpStatusCodes.json`");

}
 
Example 20
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldParseApiWithParametersUsingContentvsSchema() {
    // Tests that the content method of specifying the format of a parameter
    // gets resolved.
    // Test checks if an API's single parameter of array type gets fully resolved to 
    // referenced definitions.
    String location = "src/test/resources/issue-1078/api.yaml";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    // This test uses an Array in the parameters, test if it get's fully resolved.
    options.setResolveFully(true);
    OpenAPIV3Parser tested = new OpenAPIV3Parser();
    
    // Parse yaml
    SwaggerParseResult result = tested.readLocation(location, emptyList(), options);

    OpenAPI api = result.getOpenAPI();
    Paths paths = api.getPaths();

    // First ensure all schemas were resolved, this is important when this library 
    // is used to generate code
    Components components = api.getComponents();
    assertNotNull(components);
    assertThat(components.getSchemas().size(), equalTo(4));
    assertNotNull(components.getSchemas().get("LocationType"));
    assertNotNull(components.getSchemas().get("Lat"));
    assertNotNull(components.getSchemas().get("Long"));
    assertNotNull(components.getSchemas().get("SearchResult"));
    
    PathItem apiEndpoint = paths.get("/api-endpoint-1");
    List<Parameter> parameters = apiEndpoint.getGet().getParameters();
    
    // Ensure there's only one parameter in this test
    assertThat(parameters.size(), equalTo(1));
    
    // We are testing content for a parameter so make sure its there.
    Content content = parameters.get(0).getContent();
    assertNotNull(content);
    // spec says only one content is permitted in 3.x
    assertThat( content.size(), equalTo(1));

    // Ensure there's a media type
    MediaType mediaType = content.entrySet().iterator().next().getValue();
    assertNotNull(mediaType);

    // This test has a single parameter of type array
    Schema parameterSchema = mediaType.getSchema();
    Assert.assertTrue(parameterSchema instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema)parameterSchema;

    // Test if the item schema was resolved properly
    Schema itemSchema = arraySchema.getItems();
    assertNotNull(itemSchema);
    Assert.assertTrue(itemSchema instanceof ObjectSchema);

    // Ensure the referenced item's schema has been resolved.
    ObjectSchema objSchema = (ObjectSchema)itemSchema;
    Map<String, Schema> objectItemSchemas = objSchema.getProperties();
    assertThat( objectItemSchemas.size(), equalTo(2));
    Assert.assertTrue(objectItemSchemas.get("lat") instanceof IntegerSchema);
    Assert.assertTrue(objectItemSchemas.get("long") instanceof IntegerSchema);
}