Java Code Examples for io.swagger.v3.parser.core.models.SwaggerParseResult#getOpenAPI()

The following examples show how to use io.swagger.v3.parser.core.models.SwaggerParseResult#getOpenAPI() . 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: ExtensionsUtilTest.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@Test
public void testArrayParam(@Injectable final List<AuthorizationValue> auths) throws IOException{
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);

    String pathFile = FileUtils.readFileToString(new File("./src/test/swagger/oas3.yaml"),"UTF-8");
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, auths, options);
    OpenAPI openAPI = result.getOpenAPI();

    new ExtensionsUtil().addExtensions(openAPI);

    Operation operation = openAPI.getPaths().get("/pet").getPost();
    RequestBody body = operation.getRequestBody();

    assertNotNull(body);
    Schema model = body.getContent().get("application/json").getSchema();
    assertEquals("object", model.getType());
}
 
Example 2
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue1169() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue1169.yaml", null, options);
    assertTrue(parseResult.getMessages().size() == 0);
    OpenAPI apispec = parseResult.getOpenAPI();
    assertNotNull(apispec);
}
 
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: OpenAPIParserTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testParsingPrettifiedExtensions() throws Exception {
    String json =
            "{\n" +
                    "  \"openapi\": \"3.0.1\",\n" +
                    "  \"x-some-extension\": \"some-value\"\n" +
                    "}";

    SwaggerParseResult result = new OpenAPIParser().readContents(json, null, null);
    assertNotNull(result);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
    assertNotNull(openAPI.getExtensions());
    assertEquals(openAPI.getExtensions().get("x-some-extension"), "some-value");

    String prettyJson = Json.pretty(openAPI);

    SwaggerParseResult prettyResult = new OpenAPIParser().readContents(prettyJson, null, null);
    assertNotNull(prettyResult);
    OpenAPI prettyOpenAPI = prettyResult.getOpenAPI();
    assertNotNull(prettyOpenAPI);
    assertNotNull(prettyOpenAPI.getExtensions());
    assertEquals(prettyOpenAPI.getExtensions().get("x-some-extension"), "some-value");
}
 
Example 5
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 6
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testSecurity() {
    String json = "{\n" +
            "  \"openapi\": \"3.0.0\",\n" +
            "  \"security\": [\n" +
            "    {\n" +
            "      \"petstore_auth\": [\n" +
            "        \"write:pets\",\n" +
            "        \"read:pets\"\n" +
            "      ]\n" +
            "    }\n" +
            "  ]\n" +
            "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();

    SwaggerParseResult result = parser.readContents(json, null, null);


    OpenAPI openAPI = result.getOpenAPI();

    assertNotNull(openAPI.getSecurity());
    List<SecurityRequirement> security = openAPI.getSecurity();
    Assert.assertTrue(security.size() == 1);
    Assert.assertTrue(security.get(0).size() == 1);

    List<String> requirement = security.get(0).get("petstore_auth");
    Assert.assertTrue(requirement.size() == 2);

    Set<String> requirements = new HashSet(requirement);
    Assert.assertTrue(requirements.contains("read:pets"));
    Assert.assertTrue(requirements.contains("write:pets"));
}
 
Example 7
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1015() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveCombinators(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1015.json", null, options);
    if (parseResult.getMessages() != null && !parseResult.getMessages().isEmpty()) {
        parseResult.getMessages().forEach(s -> System.out.println(s));
        fail("Error while loading apispec!");
    }
    OpenAPI apispec = parseResult.getOpenAPI();
    assertNotNull(apispec);
}
 
Example 8
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1039() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue_1039.yaml", null, options);
    OpenAPI apispec = parseResult.getOpenAPI();
    assertNotNull(apispec);
    assertEquals(apispec.getPaths().get("/pets").getGet().getParameters().get(0).getSchema().getType(),"array");
}
 
Example 9
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 10
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue1071True() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1071-true.yaml", null, options);
    OpenAPI apispec = parseResult.getOpenAPI();
    assertNotNull(apispec);
    Schema test = apispec.getPaths().get("/mapschema").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
    assertTrue(test instanceof MapSchema);
    assertTrue(test.getAdditionalProperties() instanceof Boolean);
    assertTrue((Boolean)test.getAdditionalProperties());
}
 
Example 11
Source File: OAIParser.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Override
public OpenAPI parse(String content) {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult parseResult;

    if (!isLocationUrl(content)) {
        parseResult = parser.readContents(content);
    } else {
        parseResult = parser.readWithInfo(content, (List<AuthorizationValue>) null);
    }

    return (parseResult != null && parseResult.getOpenAPI() != null && parseResult.getOpenAPI().getInfo() != null)
            ? parseResult.getOpenAPI() : null;
}
 
Example 12
Source File: OAS3ParserTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateAPIDefinitionWithExtensions() throws Exception {
    String relativePath = "definitions" + File.separator + "oas3" + File.separator + "oas3Resources.json";
    String oas3Resources = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(relativePath), "UTF-8");
    OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();

    // check remove vendor extensions
    String definition = testGenerateAPIDefinitionWithExtension(oas3Parser, oas3Resources);
    SwaggerParseResult parseAttemptForV3 = openAPIV3Parser.readContents(definition, null, null);
    OpenAPI openAPI = parseAttemptForV3.getOpenAPI();
    boolean isExtensionNotFound = openAPI.getExtensions() == null || !openAPI.getExtensions()
            .containsKey(APIConstants.SWAGGER_X_WSO2_SECURITY);
    Assert.assertTrue(isExtensionNotFound);
    Assert.assertEquals(2, openAPI.getPaths().size());

    Iterator<Map.Entry<String, PathItem>> itr = openAPI.getPaths().entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, PathItem> pathEntry = itr.next();
        PathItem path = pathEntry.getValue();
        for (Operation operation : path.readOperations()) {
            Assert.assertFalse(operation.getExtensions().containsKey(APIConstants.SWAGGER_X_SCOPE));
        }
    }

    // check updated scopes in security definition
    Operation itemGet = openAPI.getPaths().get("/items").getGet();
    Assert.assertTrue(itemGet.getSecurity().get(0).get("default").contains("newScope"));

    // check available scopes in security definition
    SecurityScheme securityScheme = openAPI.getComponents().getSecuritySchemes().get("default");
    OAuthFlow implicityOauth = securityScheme.getFlows().getImplicit();
    Assert.assertTrue(implicityOauth.getScopes().containsKey("newScope"));
    Assert.assertEquals("newScopeDescription", implicityOauth.getScopes().get("newScope"));

    Assert.assertTrue(implicityOauth.getExtensions().containsKey(APIConstants.SWAGGER_X_SCOPES_BINDINGS));
    Map<String, String> scopeBinding =
            (Map<String, String>) implicityOauth.getExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS);
    Assert.assertTrue(scopeBinding.containsKey("newScope"));
    Assert.assertEquals("admin", scopeBinding.get("newScope"));
}
 
Example 13
Source File: FileReferenceTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue336() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/nested-file-references/issue-336.json", null, options);
    assertNotNull(result.getOpenAPI());

    OpenAPI swagger = result.getOpenAPI();
    assertNotNull(swagger.getPaths());
}
 
Example 14
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "data")
public void readInfoObject(JsonNode rootNode) throws Exception {


    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    Assert.assertEquals(openAPI.getOpenapi(),"3.0.1");


    final Info info = openAPI.getInfo();
    Assert.assertNotNull(info);
    Assert.assertEquals(info.getTitle(), "Sample Pet Store App");
    Assert.assertEquals(info.getDescription(), "This is a sample server Petstore");
    Assert.assertEquals(info.getTermsOfService(), "http://swagger.io/terms/");
    Assert.assertNotNull(info.getExtensions().get("x-info"));
    Assert.assertEquals(info.getExtensions().get("x-info").toString(),"info extension");

    final Contact contact = info.getContact();
    Assert.assertNotNull(contact);
    Assert.assertEquals(contact.getName(),"API Support");
    Assert.assertEquals(contact.getUrl(),"http://www.example.com/support");
    Assert.assertEquals(contact.getEmail(),"[email protected]");
    Assert.assertNotNull(contact.getExtensions().get("x-contact"));
    Assert.assertEquals(contact.getExtensions().get("x-contact").toString(),"contact extension");

    final License license = info.getLicense();
    Assert.assertNotNull(license);
    Assert.assertEquals(license.getName(), "Apache 2.0");
    Assert.assertEquals(license.getUrl(), "http://www.apache.org/licenses/LICENSE-2.0.html");
    Assert.assertNotNull(license.getExtensions());

    Assert.assertEquals(info.getVersion(), "1.0.1");

}
 
Example 15
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "data")
public void readExtensions(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    Assert.assertNotNull(openAPI.getExtensions());
    assertTrue(openAPI.getExtensions().containsKey("x-origin"));
    Object object = openAPI.getExtensions().get("x-origin");

    assertTrue(object instanceof List);
    List elements = (List) object;
    Assert.assertEquals(elements.size(), 1);
    Map<String, Object> map = (Map) elements.get(0);
    Assert.assertEquals(map.get("url"), "http://petstore.swagger.io/v2/swagger.json");
    Assert.assertEquals(map.get("format"), "swagger");
    Assert.assertEquals(map.get("version"), "2.0");

    Map<String, Object> converter = (Map<String, Object>) map.get("converter");
    Assert.assertNotNull(converter);
    Assert.assertEquals(converter.get("url"), "https://github.com/mermade/swagger2openapi");
    Assert.assertEquals(converter.get("version"), "1.2.1");

    object = openAPI.getExtensions().get("x-api-title");
    assertTrue(object instanceof String);
    Assert.assertEquals("pet store test api", object.toString());
}
 
Example 16
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 17
Source File: StyleController.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
private OpenAPI loadByYaml(String yaml) {
  SwaggerParseResult parseResult = StyleCheckParser.parseYaml(yaml);
  if (CollectionUtils.isNotEmpty(parseResult.getMessages())) {
    throw new RuntimeException(StringUtils.join(parseResult.getMessages(), ","));
  }
  return parseResult.getOpenAPI();
}
 
Example 18
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testIssue450() {
    String desc = "An array of Pets";
    String xTag = "x-my-tag";
    String xVal = "An extension tag";
    String yaml =
            "openapi: 3.0.0\n" +
                    "servers: []\n" +
                    "info:\n" +
                    "  version: 0.0.0\n" +
                    "  title: Simple API\n" +
                    "paths:\n" +
                    "  /:\n" +
                    "    get:\n" +
                    "      responses:\n" +
                    "        '200':\n" +
                    "          description: OK\n" +
                    "components:\n" +
                    "  schemas:\n" +
                    "    PetArray:\n" +
                    "      type: array\n" +
                    "      items:\n" +
                    "        $ref: '#/components/schemas/Pet'\n" +
                    "      description: An array of Pets\n" +
                    "      x-my-tag: An extension tag\n" +
                    "    Pet:\n" +
                    "      type: object\n" +
                    "      properties:\n" +
                    "        id:\n" +
                    "          type: string";
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml,null,null);
    assertNotNull(result.getOpenAPI());
    final OpenAPI openAPI = result.getOpenAPI();

    Schema petArray = openAPI.getComponents().getSchemas().get("PetArray");
    assertNotNull(petArray);
    assertTrue(petArray instanceof ArraySchema);
    assertEquals(petArray.getDescription(), desc);
    assertNotNull(petArray.getExtensions());
    assertNotNull(petArray.getExtensions().get(xTag));
    assertEquals(petArray.getExtensions().get(xTag), xVal);
}
 
Example 19
Source File: ApiDocReader.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
private boolean isValidOpenApi3Content(String openAPIJsonContent) {
    SwaggerParseResult parseResult = new OpenAPIV3Parser().readContents(openAPIJsonContent);
    return parseResult.getOpenAPI() != null;
}
 
Example 20
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "data")
public void readSecuritySchemesObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    List<String> messages = result.getMessages();
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.openIdConnectUrl is missing"));
    
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.tokenUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_password'.authorizationUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_clientCredentials'.authorizationUrl is missing"));
    
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.flows is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.openIdConnectUrl is missing"));
    
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.flows is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.openIdConnectUrl is missing"));
    
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.flows is missing"));
    
    
    
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);

    final Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
    Assert.assertNotNull(securitySchemes);
    Assert.assertEquals(securitySchemes.size(),10);

    SecurityScheme securityScheme = securitySchemes.get("reference");
    assertTrue(securityScheme.get$ref().equals("#/components/securitySchemes/api_key"));

    securityScheme = securitySchemes.get("remote_reference");
    assertTrue(securityScheme.get$ref().equals("http://localhost:${dynamicPort}/remote/security#/petstore_remote"));
    
    securityScheme = securitySchemes.get("petstore_auth");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.OAUTH2);
    
    securityScheme = securitySchemes.get("petstore_auth_password");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.OAUTH2);
    
    securityScheme = securitySchemes.get("petstore_auth_clientCredentials");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.OAUTH2);
    
    securityScheme = securitySchemes.get("petstore_auth_authorizationCode");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.OAUTH2);
    
    securityScheme = securitySchemes.get("api_key");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.APIKEY);
    assertTrue(securityScheme.getIn()== SecurityScheme.In.HEADER);

    securityScheme = securitySchemes.get("api_key_cookie");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.APIKEY);
    assertTrue(securityScheme.getIn()== SecurityScheme.In.COOKIE);

    securityScheme = securitySchemes.get("http");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.HTTP);

    securityScheme = securitySchemes.get("openID");
    assertTrue(securityScheme.getType()== SecurityScheme.Type.OPENIDCONNECT);
}