Search in sources :

Example 1 with License

use of io.swagger.v3.oas.models.info.License in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testSerializePetstore.

@Test
public void testSerializePetstore() throws Exception {
    final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1.yaml");
    final OpenAPI swagger = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
    assertNotNull(swagger);
    assertEquals(swagger.getInfo().getLicense().getIdentifier(), "test");
    SerializationMatchers.assertEqualsToYaml31(swagger, "openapi: 3.1.0\n" + "info:\n" + "  title: Swagger Petstore\n" + "  license:\n" + "    name: MIT\n" + "    identifier: test\n" + "  version: 1.0.0\n" + "servers:\n" + "- url: http://petstore.swagger.io/v1\n" + "paths:\n" + "  /pets:\n" + "    get:\n" + "      tags:\n" + "      - pets\n" + "      summary: List all pets\n" + "      operationId: listPets\n" + "      parameters:\n" + "      - name: limit\n" + "        in: query\n" + "        description: How many items to return at one time (max 100)\n" + "        required: false\n" + "        schema:\n" + "          type: integer\n" + "          format: int32\n" + "      responses:\n" + "        \"200\":\n" + "          description: An paged array of pets\n" + "          headers:\n" + "            x-next:\n" + "              description: A link to the next page of responses\n" + "              schema:\n" + "                type: string\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Pets'\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "    post:\n" + "      tags:\n" + "      - pets\n" + "      summary: Create a pet\n" + "      operationId: createPets\n" + "      responses:\n" + "        \"201\":\n" + "          description: Null response\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "  /pets/{petId}:\n" + "    get:\n" + "      tags:\n" + "      - pets\n" + "      summary: Info for a specific pet\n" + "      operationId: showPetById\n" + "      parameters:\n" + "      - name: petId\n" + "        in: path\n" + "        description: The id of the pet to retrieve\n" + "        required: true\n" + "        schema:\n" + "          type: string\n" + "      responses:\n" + "        \"200\":\n" + "          description: Expected response to a valid request\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Pets'\n" + "        default:\n" + "          description: unexpected error\n" + "          content:\n" + "            application/json:\n" + "              schema:\n" + "                $ref: '#/components/schemas/Error'\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      required:\n" + "      - id\n" + "      - name\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "        name:\n" + "          type:\n" + "          - string\n" + "          - integer\n" + "        tag:\n" + "          type: string\n" + "    Pets:\n" + "      type: array\n" + "      items:\n" + "        $ref: '#/components/schemas/Pet'\n" + "    Error:\n" + "      required:\n" + "      - code\n" + "      - message\n" + "      properties:\n" + "        code:\n" + "          type: integer\n" + "          format: int32\n" + "        message:\n" + "          type: string\n" + "webhooks:\n" + "  newPet:\n" + "    post:\n" + "      requestBody:\n" + "        description: Information about a new pet in the system\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              $ref: '#/components/schemas/Pet'\n" + "      responses:\n" + "        \"200\":\n" + "          description: Return a 200 status to indicate that the data was received\n" + "            successfully");
    SerializationMatchers.assertEqualsToJson31(swagger, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"info\" : {\n" + "    \"title\" : \"Swagger Petstore\",\n" + "    \"license\" : {\n" + "      \"name\" : \"MIT\",\n" + "      \"identifier\" : \"test\"\n" + "    },\n" + "    \"version\" : \"1.0.0\"\n" + "  },\n" + "  \"servers\" : [ {\n" + "    \"url\" : \"http://petstore.swagger.io/v1\"\n" + "  } ],\n" + "  \"paths\" : {\n" + "    \"/pets\" : {\n" + "      \"get\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"List all pets\",\n" + "        \"operationId\" : \"listPets\",\n" + "        \"parameters\" : [ {\n" + "          \"name\" : \"limit\",\n" + "          \"in\" : \"query\",\n" + "          \"description\" : \"How many items to return at one time (max 100)\",\n" + "          \"required\" : false,\n" + "          \"schema\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int32\"\n" + "          }\n" + "        } ],\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"An paged array of pets\",\n" + "            \"headers\" : {\n" + "              \"x-next\" : {\n" + "                \"description\" : \"A link to the next page of responses\",\n" + "                \"schema\" : {\n" + "                  \"type\" : \"string\"\n" + "                }\n" + "              }\n" + "            },\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Pets\"\n" + "                }\n" + "              }\n" + "            }\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      },\n" + "      \"post\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"Create a pet\",\n" + "        \"operationId\" : \"createPets\",\n" + "        \"responses\" : {\n" + "          \"201\" : {\n" + "            \"description\" : \"Null response\"\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"/pets/{petId}\" : {\n" + "      \"get\" : {\n" + "        \"tags\" : [ \"pets\" ],\n" + "        \"summary\" : \"Info for a specific pet\",\n" + "        \"operationId\" : \"showPetById\",\n" + "        \"parameters\" : [ {\n" + "          \"name\" : \"petId\",\n" + "          \"in\" : \"path\",\n" + "          \"description\" : \"The id of the pet to retrieve\",\n" + "          \"required\" : true,\n" + "          \"schema\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        } ],\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"Expected response to a valid request\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Pets\"\n" + "                }\n" + "              }\n" + "            }\n" + "          },\n" + "          \"default\" : {\n" + "            \"description\" : \"unexpected error\",\n" + "            \"content\" : {\n" + "              \"application/json\" : {\n" + "                \"schema\" : {\n" + "                  \"$ref\" : \"#/components/schemas/Error\"\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"Pet\" : {\n" + "        \"required\" : [ \"id\", \"name\" ],\n" + "        \"properties\" : {\n" + "          \"id\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int64\"\n" + "          },\n" + "          \"name\" : {\n" + "            \"type\" : [\"string\", \"integer\"]\n" + "          },\n" + "          \"tag\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        }\n" + "      },\n" + "      \"Pets\" : {\n" + "        \"type\" : \"array\",\n" + "        \"items\" : {\n" + "          \"$ref\" : \"#/components/schemas/Pet\"\n" + "        }\n" + "      },\n" + "      \"Error\" : {\n" + "        \"required\" : [ \"code\", \"message\" ],\n" + "        \"properties\" : {\n" + "          \"code\" : {\n" + "            \"type\" : \"integer\",\n" + "            \"format\" : \"int32\"\n" + "          },\n" + "          \"message\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"webhooks\" : {\n" + "    \"newPet\" : {\n" + "      \"post\" : {\n" + "        \"requestBody\" : {\n" + "          \"description\" : \"Information about a new pet in the system\",\n" + "          \"content\" : {\n" + "            \"application/json\" : {\n" + "              \"schema\" : {\n" + "                \"$ref\" : \"#/components/schemas/Pet\"\n" + "              }\n" + "            }\n" + "          }\n" + "        },\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"Return a 200 status to indicate that the data was received successfully\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 2 with License

use of io.swagger.v3.oas.models.info.License in project snow-owl by b2ihealthcare.

the class BaseApiConfig method docs.

/**
 * Expose this as @Bean annotated component in the implementation configuration class.
 * @return a configured docket for this API module
 */
protected final GroupedOpenApi docs(final String apiBaseUrl, final String apiGroup, final String apiVersion, final String apiTitle, final String apiTermsOfServiceUrl, final String apiContact, final String apiLicense, final String apiLicenseUrl, final String apiDescription) {
    return GroupedOpenApi.builder().group(apiGroup).pathsToMatch(apiBaseUrl.endsWith("/") ? apiBaseUrl + "**" : apiBaseUrl + "/**").packagesToScan(getApiBasePackages()).addOpenApiCustomiser(api -> {
        Info apiInfo = api.getInfo();
        apiInfo.setTitle(apiTitle);
        apiInfo.setDescription(apiDescription);
        apiInfo.setVersion(apiVersion);
        apiInfo.setTermsOfService(apiTermsOfServiceUrl);
        Contact contact = new Contact();
        contact.setName("B2i Healthcare");
        contact.setEmail(apiContact);
        contact.setUrl(apiLicenseUrl);
        apiInfo.setContact(contact);
        License license = new License();
        license.setName(apiLicense);
        license.setUrl(apiLicenseUrl);
        apiInfo.setLicense(license);
        // configure global security
        api.getComponents().addSecuritySchemes("basic", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")).addSecuritySchemes("bearer", new SecurityScheme().type(SecurityScheme.Type.APIKEY).scheme("bearer").in(In.HEADER).bearerFormat("JWT"));
        // disable servers prop
        api.setServers(List.of());
    }).addOperationCustomizer((operation, method) -> {
        return operation.addSecurityItem(new SecurityRequirement().addList("basic").addList("bearer"));
    }).build();
// .useDefaultResponseMessages(false)
// .alternateTypeRules(getAlternateTypeRules(resolver));
}
Also used : Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) License(io.swagger.v3.oas.models.info.License) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Contact(io.swagger.v3.oas.models.info.Contact) In(io.swagger.v3.oas.models.security.SecurityScheme.In) GroupedOpenApi(org.springdoc.core.GroupedOpenApi) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Info(io.swagger.v3.oas.models.info.Info) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Bean(org.springframework.context.annotation.Bean) ComponentScan(org.springframework.context.annotation.ComponentScan) License(io.swagger.v3.oas.models.info.License) Info(io.swagger.v3.oas.models.info.Info) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Contact(io.swagger.v3.oas.models.info.Contact) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 3 with License

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

the class OpenAPIDeserializerTest method readInfoObject.

@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(), "support@example.com");
    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");
}
Also used : License(io.swagger.v3.oas.models.info.License) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Contact(io.swagger.v3.oas.models.info.Contact) Test(org.testng.annotations.Test)

Example 4 with License

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

the class OpenAPIDeserializerTest method testLicense.

@Test
public void testLicense() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  license:\n" + "    invalid: true\n" + "    x-valid:\n" + "      isValid: true\n" + "  version: ''\n";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertTrue(messages.contains("attribute info.license.invalid is unexpected"));
    assertTrue(messages.contains("attribute info.title is missing"));
    assertTrue(messages.contains("attribute paths is missing"));
    assertEquals(((Map) result.getOpenAPI().getInfo().getLicense().getExtensions().get("x-valid")).get("isValid"), true);
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 with License

use of io.swagger.v3.oas.models.info.License 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

License (io.swagger.v3.oas.models.info.License)8 Contact (io.swagger.v3.oas.models.info.Contact)5 Info (io.swagger.v3.oas.models.info.Info)5 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)5 OpenAPI (io.swagger.v3.oas.models.OpenAPI)4 Test (org.testng.annotations.Test)4 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)3 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Json (io.swagger.v3.core.util.Json)1 OpenApiResource (io.swagger.v3.jaxrs2.integration.resources.OpenApiResource)1 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)1 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)1 io.swagger.v3.oas.models.security (io.swagger.v3.oas.models.security)1 SecurityRequirement (io.swagger.v3.oas.models.security.SecurityRequirement)1 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)1 In (io.swagger.v3.oas.models.security.SecurityScheme.In)1