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

The following examples show how to use io.swagger.v3.parser.OpenAPIV3Parser#read() . 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 shouldParseExternalSchemaModelHavingReferenceToItsLocalModel() {
    // given
    String location = "src/test/resources/issue-1040/api.yaml";
    OpenAPIV3Parser tested = new OpenAPIV3Parser();

    // when
    OpenAPI result = tested.read(location);

    // then
    Components components = result.getComponents();
    Schema modelSchema = components.getSchemas().get("Value");

    assertThat(modelSchema, notNullValue());
    assertThat(modelSchema.getProperties().get("id"), instanceOf(Schema.class));
    assertThat(((Schema) modelSchema.getProperties().get("id")).get$ref(), equalTo("#/components/schemas/ValueId"));
}
 
Example 2
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodegenPetstore() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/petstore-codegen.yaml");
    Schema enumModel =  openAPI.getComponents().getSchemas().get("Enum_Test");
    assertNotNull(enumModel);
    Schema enumProperty = (Schema)enumModel.getProperties().get("enum_integer");
    assertNotNull(enumProperty);

    assertTrue(enumProperty instanceof IntegerSchema);
    IntegerSchema enumIntegerProperty = (IntegerSchema) enumProperty;
    List<Number> integers =  enumIntegerProperty.getEnum();
    assertEquals(integers.get(0), new Integer(1));
    assertEquals(integers.get(1), new Integer(-1));

    Operation getOrderOperation = openAPI.getPaths().get("/store/order/{orderId}").getGet();
    assertNotNull(getOrderOperation);
    Parameter orderId = getOrderOperation.getParameters().get(0);
    assertTrue(orderId instanceof PathParameter);
    PathParameter orderIdPathParam = (PathParameter) orderId;
    assertNotNull(orderIdPathParam.getSchema().getMinimum());

    BigDecimal minimum = orderIdPathParam.getSchema().getMinimum();
    assertEquals(minimum.toString(), "1");
}
 
Example 3
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue357() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/issue_357.yaml");
    assertNotNull(openAPI);
    List<Parameter> getParams = openAPI.getPaths().get("/testApi").getGet().getParameters();
    assertEquals(2, getParams.size());
    for (Parameter param : getParams) {

        switch (param.getName()) {
            case "pathParam1":
                assertEquals(param.getSchema().getType(), "integer");
                break;
            case "pathParam2":
                assertEquals(param.getSchema().getType(), "string");
                break;
            default:
                fail("Unexpected parameter named " + param.getName());
                break;
        }
    }
}
 
Example 4
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1108() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    OpenAPI openAPI = parser.read("issue-1108.yaml");
    assertNotNull(openAPI);
    assertNotNull(openAPI.getPaths().get("/pets").getGet().getParameters().get(0).getAllowReserved());
}
 
Example 5
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test(description = "A string example should not be over quoted when parsing a yaml string")
public void readingSpecStringShouldNotOverQuotingStringExample() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(false);
    final OpenAPI openAPI = parser.read("src/test/resources/over-quoted-example.yaml", null, options);

    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertEquals("NoQuotePlease", definitions.get("CustomerType").getExample());
}
 
Example 6
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadExternalNestedDefinitions() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/nested-references/b.yaml");

    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertTrue(definitions.containsKey("x"));
    assertTrue(definitions.containsKey("y"));
    assertTrue(definitions.containsKey("z"));
    assertEquals( definitions.get("i").get$ref(),"#/components/schemas/k");
    assertEquals( definitions.get("k").getTitle(), "k-definition");
}
 
Example 7
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileReferenceWithVendorExt() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/file-reference-with-vendor-ext/b.yaml");
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertTrue(definitions.get("z").getExtensions().get("x-foo") instanceof Map);
    assertEquals(((Map) definitions.get("z").getExtensions().get("x-foo")).get("bar"), "baz");
    assertTrue(definitions.get("x").getExtensions().get("x-foo") instanceof Map);
    assertEquals(((Map) definitions.get("x").getExtensions().get("x-foo")).get("bar"), "baz");
}
 
Example 8
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1119() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    parser.setEncoding("ISO-8859-1");
    OpenAPI openAPI = parser.read("src/test/resources/issue-1119.yaml");
    assertNotNull(openAPI);
    assertEquals(openAPI.getPaths().get("/pets").getGet().getParameters().get(0).getDescription(),"Cuántos artículos devolver al mismo tiempo (máximo 100)");
}
 
Example 9
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue358() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/issue_358.yaml");
    assertNotNull(openAPI);
    List<Parameter> parms = openAPI.getPaths().get("/testApi").getGet().getParameters();
    assertEquals(1, parms.size());
    assertEquals("pathParam", parms.get(0).getName());
    assertEquals("string",  parms.get(0).getSchema().getType());
}
 
Example 10
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue339() throws Exception {
    OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
    OpenAPI api = openAPIV3Parser.read("issue-339.yaml");
    assertNotNull(api);
    Parameter param = api.getPaths().get("/store/order/{orderId}").getGet().getParameters().get(0);
    assertTrue(param instanceof PathParameter);
    PathParameter pp = (PathParameter) param;
    assertTrue(pp.getSchema().getMinimum().toString().equals("1"));
    assertTrue(pp.getSchema().getMaximum().toString().equals("5"));
}
 
Example 11
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedReferences() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/relative-file-references/json/parent.json");
    assertTrue(openAPI.getComponents().getSchemas().containsKey("externalArray"));
    assertTrue(openAPI.getComponents().getSchemas().containsKey("referencedByLocalArray"));
    assertTrue(openAPI.getComponents().getSchemas().containsKey("externalObject"));
    assertTrue(openAPI.getComponents().getSchemas().containsKey("referencedByLocalElement"));
    assertTrue(openAPI.getComponents().getSchemas().containsKey("referencedBy"));
}
 
Example 12
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue146() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/issue_146.yaml");
    assertNotNull(openAPI);
    QueryParameter p = ((QueryParameter) openAPI.getPaths().get("/checker").getGet().getParameters().get(0));
    StringSchema pp = (StringSchema) ((ArraySchema)p.getSchema()).getItems();
    assertTrue("registration".equalsIgnoreCase(pp.getEnum().get(0)));
}
 
Example 13
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test(description = "Test (path & form) parameter's required attribute")
public void testParameterRequired() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/petstore.yaml");
    final List<Parameter> operationParams = openAPI.getPaths().get("/pet/{petId}").getPost().getParameters();
    final PathParameter pathParameter = (PathParameter) operationParams.get(0);
    Assert.assertTrue(pathParameter.getRequired());
}
 
Example 14
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue286() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    OpenAPI openAPI = parser.read("issue_286.yaml");
    Schema response = openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("*/*").getSchema();
    assertTrue(response.get$ref() != null);
    assertEquals(response.get$ref(), "#/components/schemas/issue_286_PetList");
    assertNotNull(openAPI.getComponents().getSchemas().get("issue_286_Allergy"));
}
 
Example 15
Source File: TaskGenerateOpenApiTest.java    From flow with Apache License 2.0 4 votes vote down vote up
private OpenAPI getGeneratedOpenAPI() throws IOException {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    return parser.read(generatedOpenAPI.getAbsolutePath());
}
 
Example 16
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testIssue360() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/issue_360.yaml");
    assertNotNull(openAPI);

    RequestBody body = openAPI.getPaths().get("/pets").getPost().getRequestBody();
    assertNotNull(body);

    assertNotNull(body.getContent().get("application/json").getSchema());
    Schema model = body.getContent().get("application/json").getSchema();

    assertNotNull(model.getProperties().get("foo"));

    Map<String, Object> extensions = body.getExtensions();
    assertNotNull(extensions);

    assertNotNull(extensions.get("x-examples"));
    Object o = extensions.get("x-examples");
    assertTrue(o instanceof Map);

    Map<String, Object> on = (Map<String, Object>) o;

    Object jn = on.get("application/json");
    assertTrue(jn instanceof Map);

    Map<String, Object> objectNode = (Map<String, Object>) jn;
    assertEquals(objectNode.get("foo"), "bar");

    RequestBody stringBodyParameter = openAPI.getPaths().get("/otherPets").getPost().getRequestBody();

    assertTrue(stringBodyParameter.getRequired());

    Schema sbpModel = stringBodyParameter.getContent().get("application/json").getSchema();

    assertEquals(sbpModel.getType(), "string");
    assertEquals(sbpModel.getFormat(), "uuid");
    RequestBody bodyParameter = openAPI.getPaths().get("/evenMorePets").getPost().getRequestBody();
    assertTrue(bodyParameter.getRequired());
    Schema refModel = bodyParameter.getContent().get("application/json").getSchema();
    assertTrue(refModel.get$ref() != null);
    assertEquals(refModel.get$ref(), "#/components/schemas/Pet");
}
 
Example 17
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testIssue255() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    OpenAPI openAPI = parser.read("objectExample.yaml");
    assertEquals(openAPI.getComponents().getSchemas().get("SamplePayload").getExample().toString(), "[{\"op\":\"replace\",\"path\":\"/s\",\"v\":\"w\"}]");
}
 
Example 18
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testIssue108() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/issue_108.yaml");
    assertNotNull(openAPI);
}
 
Example 19
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test(enabled = false, description = "see https://github.com/openAPI-api/openAPI-parser/issues/337")
public void testIssue62() {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/fixtures/v2.0/json/resources/resourceWithLinkedDefinitions.json");
    assertNotNull(openAPI.getPaths().get("/pets/{petId}").getGet());
}
 
Example 20
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testTroublesomeFile() throws Exception {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    final OpenAPI openAPI = parser.read("src/test/resources/troublesome.yaml");
}