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

The following examples show how to use io.swagger.v3.parser.core.models.ParseOptions#setFlatten() . 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: RequestParameterTest.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Load the OpenAPI files for endpoints and compares them against the source code
 */
@Test
public void checkOpenApiSpec() {
  OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
  ParseOptions options = new ParseOptions();
  options.setResolveFully(true);
  options.setFlatten(true);

  SwaggerParseResult parseResult = openApiParser.readLocation(OPENAPI_SPEC_PATH, null, options);
  _openAPI = parseResult.getOpenAPI();
  Map<String, Set<String>> schema = parseSchema();
  Assert.assertEquals(schema.keySet(), _endpointToClass.keySet());
  for (Map.Entry<String, Set<String>> endpoint: schema.entrySet()) {
    Assert.assertTrue(_endpointToClass.containsKey(endpoint.getKey()));
    CruiseControlParameters endpointParams = _endpointToClass.get(endpoint.getKey());
    Assert.assertEquals(endpoint.getValue(), endpointParams.caseInsensitiveParameterNames());
  }
}
 
Example 3
Source File: ResponseTest.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Check the consistency for all POST and GET endpoints defined in OpenAPI spec.
 */
@Test
public void checkOpenAPISpec() {
  ParseOptions options = new ParseOptions();
  options.setResolve(true);
  options.setFlatten(true);
  _openAPI = new OpenAPIV3Parser().read(OPENAPI_SPEC_PATH, null, options);
  for (PathItem path : _openAPI.getPaths().values()) {
    if (path.getGet() != null) {
      checkOperation(path.getGet());
    }
    if (path.getPost() != null) {
      checkOperation(path.getPost());
    }
  }
}
 
Example 4
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 5
Source File: OpenAPIParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue799() {
    OpenAPIParser openApiParser = new OpenAPIParser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);

    OpenAPI openAPI = openApiParser.readLocation("issue799.json", null, options).getOpenAPI();
    Assert.assertEquals(((Schema)openAPI.getComponents().getSchemas().get("v1beta3.Binding").getProperties().get("metadata")).get$ref(),"#/components/schemas/v1beta3.ObjectMeta");
    RequestBody bodyParameter = openAPI.getPaths().get("/api/v1beta3/namespaces/{namespaces}/bindings").getPost().getRequestBody();
    Assert.assertEquals( bodyParameter.getContent().get("*/*").getSchema().get$ref(), "#/components/schemas/v1beta3.Binding");
    Assert.assertEquals( openAPI.getPaths().get("/api/v1beta3/namespaces/{namespaces}/componentstatuses/{name}").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/v1beta3.ComponentStatus");
    Assert.assertEquals( openAPI.getPaths().get("/api/v1beta3/namespaces/{namespaces}/componentstatuses").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/v1beta3.ComponentStatusList");
    Schema conditionsProperty = (Schema) openAPI.getComponents().getSchemas().get("v1beta3.ComponentStatus").getProperties().get("conditions");
    assertTrue( conditionsProperty instanceof ArraySchema);
    Schema items = ((ArraySchema)conditionsProperty).getItems();
    assertTrue( items.get$ref() != null);
    Assert.assertEquals( items.get$ref(), "#/components/schemas/v1beta3.ObjectReference");


}
 
Example 6
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssueFlattenAdditionalPropertiesSchemaInlineModelTrue() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setFlattenComposedSchemas(true);
    options.setCamelCaseFlattenNaming(true);
    SwaggerParseResult parseResult = openApiParser.readLocation("additionalPropertiesFlatten.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();

    //responses
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_map200"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_map200")).getOneOf().get(0).get$ref(),"#/components/schemas/Macaw1");
    assertNotNull(openAPI.getComponents().getSchemas().get("Inline_response_map_items404"));
    assertEquals(((ComposedSchema)openAPI.getComponents().getSchemas().get("Inline_response_map_items404")).getAnyOf().get(0).get$ref(),"#/components/schemas/Macaw2");
}
 
Example 7
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testInlineModelResolverByUrl(){
    String url = "http://localhost:${dynamicPort}/remote/json";
    url = url.replace("${dynamicPort}", String.valueOf(this.serverPort));

    ParseOptions options = new ParseOptions();
    options.setFlatten(true);

    SwaggerParseResult result = new OpenAPIV3Parser().readLocation(url,new ArrayList<>(),options);
    Assert.assertNotNull(result);
    OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema address = (Schema)user.getProperties().get("address");

    assertTrue((address.get$ref()!= null));

    Schema userAddress = openAPI.getComponents().getSchemas().get("User_address");
    assertNotNull(userAddress);
    assertNotNull(userAddress.getProperties().get("city"));
    assertNotNull(userAddress.getProperties().get("street"));
}
 
Example 8
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue705() throws Exception {
    ParseOptions options = new ParseOptions();
    options.setFlatten(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("issue-705.yaml",null, options);

    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200").getType());
}
 
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 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 12
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 13
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssueFlattenArraySchemaItemsInlineModelFalse() {
    OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    options.setFlattenComposedSchemas(false);
    options.setCamelCaseFlattenNaming(false);
    SwaggerParseResult parseResult = openApiParser.readLocation("flattenArrayItems.yaml", null, options);
    OpenAPI openAPI = parseResult.getOpenAPI();

    //responses
    assertNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));
    assertNull(openAPI.getComponents().getSchemas().get("Inline_response_400"));

    //parameters
    assertNull(openAPI.getComponents().getSchemas().get("Inline_parameter_items_bodylimit"));
    assertNull(openAPI.getComponents().getSchemas().get("Pagelimit"));

    //requestBodies
    assertNull(openAPI.getComponents().getSchemas().get("Body"));
    assertNull(openAPI.getComponents().getSchemas().get("Inline_response_items200"));

    //components
    assertNull(openAPI.getComponents().getSchemas().get("Inline_array_items_ArrayTest"));

}
 
Example 14
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1147() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1147/issue1147.yaml", null, options);
    OpenAPI apispec = parseResult.getOpenAPI();
    assertNotNull(apispec);
    assertEquals(((Schema)apispec.getComponents().getSchemas().get("StringObject").getProperties().get("val")).get$ref(),"#/components/schemas/String");
}
 
Example 15
Source File: StyleCheckParser.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
private static ParseOptions createParseOptions() {

    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(true);
    parseOptions.setResolveCombinators(false);
    parseOptions.setResolveFully(false);
    parseOptions.setFlatten(false);
    return parseOptions;
  }
 
Example 16
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssueResolveCallBacks() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("callbacks-issue/swagger.yaml", null, options);
    OpenAPI spec = parseResult.getOpenAPI();
    assertNotNull(spec);
    assertEquals(spec.getPaths().get("/webhook").getPost().getCallbacks().get("WebhookVerificationEvent").get$ref(),"#/components/callbacks/WebhookVerificationEvent");
}
 
Example 17
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1148_Flatten_Dot() {
    ParseOptions options = new ParseOptions();
    options.setFlatten(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue1148.yaml", null, options);
    OpenAPI apispec = parseResult.getOpenAPI();
    assertNotNull(apispec);
    assertEquals(((Schema)apispec.getComponents().getSchemas().get("Some.User").getProperties().get("address")).get$ref(),"#/components/schemas/Some.User_address");
}
 
Example 18
Source File: CompatibilityCheckParser.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
private static ParseOptions createParseOptions() {

    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolve(true);
    parseOptions.setResolveCombinators(true);
    parseOptions.setResolveFully(true);
    parseOptions.setFlatten(false);
    return parseOptions;
  }
 
Example 19
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 20
Source File: OpenAPIParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testConverterWithFlatten() {
    String yaml = "swagger: \"2.0\"\n" +
            "info:\n" +
            "  description: \"Foo\"\n" +
            "  version: \"1.0.0\"\n" +
            "host: \"something.com\"\n" +
            "basePath: \"/\"\n" +
            "schemes:\n" +
            "  - \"https\"\n" +
            "consumes:\n" +
            "  - \"application/json\"\n" +
            "produces:\n" +
            "  - \"application/json\"\n" +
            "paths:\n" +
            "  /example:\n" +
            "    get:\n" +
            "      responses:\n" +
            "        200:\n" +
            "          description: \"OK\"\n" +
            "          schema:\n" +
            "            $ref: \"#/definitions/Foo\"\n" +
            "    parameters: []\n" +
            "definitions:\n" +
            "  Foo:\n" +
            "    type: \"object\"\n" +
            "    required:\n" +
            "    properties:\n" +
            "      nested:\n" +
            "        type: \"object\"\n" +
            "        properties:\n" +
            "          color:\n" +
            "            type: \"string\"";

    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult result = new OpenAPIParser().readContents(yaml, null, options);
    OpenAPI openAPI = result.getOpenAPI();
    assertEquals(openAPI.getComponents().getSchemas().size(), 2);
}