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

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

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

    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/swagger/additionalProperties.yaml", auths, options);

    assertNotNull(result);
    assertNotNull(result.getOpenAPI());
    OpenAPI openAPI = result.getOpenAPI();
    Assert.assertEquals(result.getOpenAPI().getOpenapi(), "3.0.0");
    List<String> messages = result.getMessages();

    Assert.assertTrue(openAPI.getComponents().getSchemas().get("someObject").getAdditionalProperties() instanceof Schema);
    Assert.assertTrue(((Schema)(openAPI.getComponents().getSchemas().get("someObject").getProperties().get("innerObject"))).getAdditionalProperties() instanceof Boolean);

}
 
Example 5
Source File: SwaggerConverter.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
private OpenAPI getOpenAPI() throws SwaggerException {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);

    OpenAPI openAPI = new OpenAPIV3Parser().readContents(this.defn, null, options).getOpenAPI();

    if (openAPI == null) {
        // try v2
        Swagger swagger = new SwaggerParser().parse(this.defn);
        if (swagger == null) {
            convertV1ToV2();
        }
        // parse v2
        openAPI = parseV2(options);
    }
    return openAPI;
}
 
Example 6
Source File: VaadinConnectTsGenerator.java    From flow with Apache License 2.0 6 votes vote down vote up
private static SwaggerParseResult getParseResult(
        CodegenConfigurator configurator) {
    try {
        List<AuthorizationValue> authorizationValues = AuthParser
                .parse(configurator.getAuth());
        String inputSpec = configurator.loadSpecContent(
                configurator.getInputSpecURL(), authorizationValues);
        ParseOptions options = new ParseOptions();
        options.setResolve(true);
        return new OpenAPIParser().readContents(inputSpec,
                authorizationValues, options);
    } catch (Exception e) {
        throw new IllegalStateException(
                "Unexpected error while generating vaadin-connect TypeScript endpoint wrappers. "
                        + String.format("Can't read file '%s'",
                                configurator.getInputSpecURL()),
                e);
    }
}
 
Example 7
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnumType() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/issue-1090.yaml", null, options);
    assertNotNull(result.getOpenAPI());
    OpenAPI openAPI = result.getOpenAPI();

    Schema someObj = openAPI.getComponents().getSchemas().get("SomeObj");
    assertNotNull(someObj);

    Map<String, Schema> properties = someObj.getProperties();
    assertNotNull(properties);

    Schema iprop = properties.get("iprop");
    assertNotNull(iprop);
    assertEquals(iprop.getType(), "integer");
    assertEquals(iprop.getFormat(), "int32");

    Schema lprop = properties.get("lprop");
    assertNotNull(lprop);
    assertEquals(lprop.getType(), "integer");
    assertEquals(lprop.getFormat(), "int64");

    Schema nprop = properties.get("nprop");
    assertNotNull(nprop);
    assertEquals(nprop.getType(), "number");
}
 
Example 8
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterOnPathLevel() throws Exception {
    String yaml = "openapi: 3.0.1\n" +
            "info:\n" +
            "  version: '1.0.0'\n" +
            "  title: 'title'\n" +
            "  description: 'description'\n" +
            "paths:\n" +
            "  /foo:\n" +
            "    parameters:\n" +
            "      - in: query\n" +
            "        name: bar\n" +
            "        schema:\n" +
            "          type: string\n" +
            "    get:\n" +
            "      responses:\n" +
            "        '200':\n" +
            "          description: OK";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml, null, options).getOpenAPI();
    assertNotNull(openAPI);
    List<Parameter> getParameters = openAPI.getPaths().get("/foo").getGet().getParameters();
    assertNotNull(getParameters);
    assertEquals(1, getParameters.size());
    assertEquals("bar", getParameters.get(0).getName());
}
 
Example 9
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 10
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue901_2() {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue-901/spec2.yaml",null,options).getOpenAPI();
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    ArraySchema arraySchema = (ArraySchema) openAPI.getComponents().getSchemas().get("Test.Definition").getProperties().get("stuff");
    String internalRef = arraySchema.getItems().get$ref();
    assertEquals(internalRef,"#/components/schemas/TEST.THING.OUT.Stuff");
}
 
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 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 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: OpenAPIParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
 public void testIssue258() {
     ParseOptions options = new ParseOptions();
     options.setResolve(true);
     SwaggerParseResult result = new OpenAPIParser().readLocation("duplicateOperationId.json", null, options);

     System.out.println(result.getMessages());
     assertNotNull(result);
     assertNotNull(result.getOpenAPI());
     assertEquals(result.getMessages().get(0), "attribute paths.'/pets/{id}'(post).operationId is repeated");
}
 
Example 15
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testConverterIssue17() throws Exception {
    String yaml = "openapi: 3.0.0\n" +
            "info:\n" +
            "  version: 0.0.0\n" +
            "  title: nada\n" +
            "paths:\n" +
            "  /persons:\n" +
            "    get:\n" +
            "      parameters:\n" +
            "        - name: testParam\n" +
            "          in: query\n" +
            "          style: form\n" +
            "          schema:\n" +
            "            type: array\n" +
            "            items:\n" +
            "              type: string\n" +
            "      responses:\n" +
            "        '200':\n" +
            "          description: Successful response\n" +
            "          content:\n" +
            "            '*/*':\n" +
            "              schema:\n" +
            "                $ref: '#/components/schemas/Content'\n" +
            "components:\n" +
            "  schemas:\n" +
            "    Content:\n" +
            "      type: object" ;

    ParseOptions options = new ParseOptions();
    options.setResolve(false);
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml,null, options);

    assertNotNull(result.getOpenAPI());
    assertEquals((result.getOpenAPI().getPaths().
            get("/persons").getGet().getResponses().get("200")
            .getContent().get("*/*")
            .getSchema()).get$ref(), "#/components/schemas/Content");
}
 
Example 16
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 17
Source File: ExtensionsUtilTest.java    From swagger-inflector 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.0\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" +
                    "          type: object\n" +
                    "          properties:\n" +
                    "           id:\n" +
                    "             type: integer\n" +
                    "             format: int64\n" +
                    "           name:\n" +
                    "             type: string";

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

    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml,auths,options).getOpenAPI();
    ExtensionsUtil extensionsUtil = new ExtensionsUtil();
    extensionsUtil.addExtensions(openAPI);
    Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
    Assert.assertEquals(schemas.get("SchemaA").getExtensions().get("x-swagger-router-model"),"SchemaA");
    Assert.assertEquals(openAPI.getPaths().get("/selfRefB").getGet().getRequestBody().getContent().get("application/json").getSchema().getExtensions().get("x-swagger-router-model"),"SchemaB");

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

    assertEquals(schema,openAPI.getComponents().getSchemas().get("SchemaB"));
}
 
Example 18
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);
}
 
Example 19
Source File: OpenAPIV3Parser.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public OpenAPI read(String location) {
    final ParseOptions options = new ParseOptions();
    options.setResolve(true);
    return read(location, null, options);
}
 
Example 20
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllOfSchema(@Injectable List<AuthorizationValue> auths){
    String yaml = "openapi: '3.0'\n" +
        "components:\n" +
        "  schemas:\n" +
        "    Pet:\n" +
        "      type: object\n" +
        "      required:\n" +
        "      - pet_type\n" +
        "      properties:\n" +
        "        pet_type:\n" +
        "          type: string\n" +
        "    Cat:\n" +
        "      allOf:\n" +
        "      - $ref: '#/components/schemas/Pet'\n" +
        "      - type: object\n" +
        "        # all other properties specific to a `Cat`\n" +
        "        properties:\n" +
        "          name:\n" +
        "            type: string\n";
  
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
  
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
  
    SwaggerParseResult result = parser.readContents(yaml,auths,options);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
  
    Schema catSchema = result.getOpenAPI().getComponents().getSchemas().get("Cat");
    assertTrue(catSchema != null);
    assertTrue(catSchema instanceof ComposedSchema);
    
    ComposedSchema catCompSchema = (ComposedSchema) catSchema;
    List<Schema> allOfSchemas = catCompSchema.getAllOf();
    assertTrue(allOfSchemas != null);
    assertEquals(allOfSchemas.size(), 2);
    
    Schema refPetSchema = allOfSchemas.get(0);
    assertTrue(refPetSchema != null);
    assertEquals(refPetSchema.get$ref(), "#/components/schemas/Pet");
  
    Schema otherSchema = allOfSchemas.get(1);
    assertTrue(otherSchema != null);
    assertTrue(otherSchema.getProperties() != null);
    Schema nameProp = (Schema) otherSchema.getProperties().get("name");
    assertTrue(nameProp != null);
    assertEquals(nameProp.getType(), "string");
    
}