Search in sources :

Example 26 with Info

use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testPR246.

@Test
public void testPR246() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  description: 'Tests the allOf API for parent, interface and child models.'\n" + "  version: 2.0.0\n" + "  title: Test allOf API\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "    parameters: []\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      type: object\n" + "      required:\n" + "        - id\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "    Furry:\n" + "      type: object\n" + "      required:\n" + "        - coatColour\n" + "      properties:\n" + "        coatColour:\n" + "          type: string\n" + "    Dog:\n" + "      allOf:\n" + "        - $ref: '#/components/schemas/Pet'\n" + "        - $ref: '#/components/schemas/Furry'\n" + "        - type: object\n" + "          required:\n" + "            - name\n" + "          properties:\n" + "            name:\n" + "              type: string";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    Schema dog = openAPI.getComponents().getSchemas().get("Dog");
    assertNotNull(dog);
    assertTrue(dog instanceof ComposedSchema);
    ComposedSchema composed = (ComposedSchema) dog;
    assertTrue(composed.getAllOf().get(0).get$ref() != null);
    assertTrue(composed.getAllOf().size() == 3);
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 27 with Info

use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testParameterOnPathLevel.

@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());
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 28 with Info

use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSettingsAddParametersToEachOperationDisabled.

@Test
public void testSettingsAddParametersToEachOperationDisabled() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: test spec\n" + "  version: '1.0'\n" + "paths:\n" + "  '/test/{id}':\n" + "    parameters:\n" + "      - name: id\n" + "        in: path\n" + "        required: true\n" + "        schema:\n" + "          type: string\n" + "    get:\n" + "      description: test get\n" + "      parameters:\n" + "        - name: page\n" + "          in: query\n" + "          schema:\n" + "            type: string\n" + "      responses:\n" + "        default:\n" + "          description: test response";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI swagger = result.getOpenAPI();
    final OpenAPI resolved = new OpenAPIResolver(swagger, null, null, new OpenAPIResolver.Settings().addParametersToEachOperation(false)).resolve();
    assertEquals(resolved.getPaths().get("/test/{id}").getParameters().size(), 1);
    PathParameter pp = (PathParameter) resolved.getPaths().get("/test/{id}").getParameters().get(0);
    assertEquals(pp.getName(), "id");
    assertEquals(resolved.getPaths().get("/test/{id}").getGet().getParameters().size(), 1);
    QueryParameter qp = (QueryParameter) resolved.getPaths().get("/test/{id}").getGet().getParameters().get(0);
    assertEquals(qp.getName(), "page");
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) Test(org.testng.annotations.Test)

Example 29 with Info

use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue1644_NullValue.

@Test
public void testIssue1644_NullValue() throws Exception {
    ParseOptions options = new ParseOptions();
    String issue1644 = "openapi: 3.0.0\n" + "info:\n" + "  title: Operations\n" + "  version: 0.0.0\n" + "paths:\n" + "  \"/operations\":\n" + "    post:\n" + "      parameters:\n" + "        - name: param0\n" + "          schema:\n" + "            type: string\n" + "      responses:\n" + "        default:\n" + "          description: None\n";
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1644, null, options);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getOpenAPI());
    assertEquals(result.getMessages().size(), 1);
    assertTrue(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is missing"));
    assertFalse(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is not of type `string`"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Example 30 with Info

use of io.swagger.v3.oas.models.info.Info in project swagger-parser by swagger-api.

the class OpenAPIDeserializer method getInfo.

public Info getInfo(ObjectNode node, String location, ParseResult result) {
    if (node == null)
        return null;
    Info info = new Info();
    String value = getString("title", node, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        info.setTitle(value);
    }
    value = getString("description", node, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        info.setDescription(value);
    }
    value = getString("termsOfService", node, false, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        info.setTermsOfService(value);
    }
    ObjectNode obj = getObject("contact", node, false, "contact", result);
    Contact contact = getContact(obj, String.format("%s.%s", location, "contact"), result);
    if (obj != null) {
        info.setContact(contact);
    }
    obj = getObject("license", node, false, location, result);
    License license = getLicense(obj, String.format("%s.%s", location, "license"), result);
    if (obj != null) {
        info.setLicense(license);
    }
    value = getString("version", node, true, location, result);
    if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
        info.setVersion(value);
    }
    Map<String, Object> extensions = getExtensions(node);
    if (extensions != null && extensions.size() > 0) {
        info.setExtensions(extensions);
    }
    Set<String> keys = getKeys(node);
    for (String key : keys) {
        if (!INFO_KEYS.contains(key) && !key.startsWith("x-")) {
            result.extra(location, key, node.get(key));
        }
    }
    return info;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) License(io.swagger.v3.oas.models.info.License) Info(io.swagger.v3.oas.models.info.Info) Contact(io.swagger.v3.oas.models.info.Contact)

Aggregations

Test (org.testng.annotations.Test)91 OpenAPI (io.swagger.v3.oas.models.OpenAPI)77 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)60 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)56 Info (io.swagger.v3.oas.models.info.Info)39 Schema (io.swagger.v3.oas.models.media.Schema)24 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)22 StringSchema (io.swagger.v3.oas.models.media.StringSchema)22 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)19 Components (io.swagger.v3.oas.models.Components)18 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)18 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)17 Parameter (io.swagger.v3.oas.models.parameters.Parameter)17 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)15 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)15 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)15 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)14 MapSchema (io.swagger.v3.oas.models.media.MapSchema)14 DateSchema (io.swagger.v3.oas.models.media.DateSchema)13 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)13