Search in sources :

Example 1 with Info

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

the class OpenAPIDeserializerTest method testIssue151.

@Test
public void testIssue151() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 2.0.0\n" + "  title: Test Issue 151\n" + "  description: Tests that ComposedSchema vendor extensions are deserialized correctly.\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      type: object\n" + "      required:\n" + "        - id\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "          format: int64\n" + "    Dog:\n" + "      type: object\n" + "      allOf:\n" + "        - $ref: '#/components/schemas/Pet'\n" + "        - required:\n" + "            - name\n" + "          properties:\n" + "            name:\n" + "              type: string\n" + "      x-vendor-ext: some data";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    assertTrue(result.getMessages().isEmpty());
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    assertTrue(definitions.size() == 2);
    Schema allOfModel = definitions.get("Dog");
    assertTrue(allOfModel instanceof ComposedSchema);
    assertFalse(allOfModel.getExtensions().isEmpty());
    assertEquals("some data", allOfModel.getExtensions().get("x-vendor-ext"));
}
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 2 with Info

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

the class OpenAPIDeserializerTest method testSecurityDefinition.

@Test
public void testSecurityDefinition() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + "  /pet:\n" + "    get:\n" + "      security:\n" + "        - basic_auth: []\n" + "          api_key: []\n" + "      responses:\n" + "        default:\n" + "          description: Default response\n" + "info:\n" + "  version: ''\n" + "  title: ''\n" + "components:\n" + "  securitySchemes:\n" + "    basic_auth:\n" + "      type: http\n" + "      x-foo: basicBar\n" + "      scheme: basic\n" + "    api_key:\n" + "      type: apiKey\n" + "      name: api_key\n" + "      in: header\n" + "      description: api key description\n" + "      x-foo: apiKeyBar";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI.getComponents().getSecuritySchemes());
    assertTrue(openAPI.getComponents().getSecuritySchemes().keySet().size() == 2);
    // Basic Authentication
    SecurityScheme definitionBasic = openAPI.getComponents().getSecuritySchemes().get("basic_auth");
    assertNotNull(definitionBasic);
    assertEquals(definitionBasic.getType(), SecurityScheme.Type.HTTP);
    assertEquals(definitionBasic.getExtensions().get("x-foo"), "basicBar");
    // API Key Authentication
    SecurityScheme definition = openAPI.getComponents().getSecuritySchemes().get("api_key");
    assertNotNull(definition);
    assertEquals(definition.getType(), SecurityScheme.Type.APIKEY);
    SecurityScheme apiKey = definition;
    assertEquals(apiKey.getName(), "api_key");
    assertEquals(apiKey.getIn(), SecurityScheme.In.HEADER);
    assertEquals(apiKey.getDescription(), "api key description");
    assertEquals(apiKey.getExtensions().get("x-foo"), "apiKeyBar");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 3 with Info

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

the class OpenAPIDeserializerTest method testAlmostEmpty.

@Test
public void testAlmostEmpty(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: '3.0.1'\n" + "new: extra";
    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);
    assertTrue(messages.contains("attribute info is missing"));
    assertTrue(messages.contains("attribute paths is missing"));
    assertTrue(messages.contains("attribute new is unexpected"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with Info

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

the class OpenAPIDeserializerTest method testContact.

@Test
public void testContact() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: title\n" + "  bad: bad\n" + "  x-foo: bar\n" + "  description: description\n" + "  termsOfService: tos\n" + "  contact:\n" + "    name: tony\n" + "    url: url\n" + "    email: email\n" + "    invalid: invalid\n" + "    x-fun: true\n" + "  version: version\n" + "paths: {}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertEquals(result.getOpenAPI().getInfo().getTitle(), "title");
    assertEquals(result.getOpenAPI().getInfo().getDescription(), "description");
    assertEquals(result.getOpenAPI().getInfo().getTermsOfService(), "tos");
    assertEquals(result.getOpenAPI().getInfo().getVersion(), "version");
    Contact contact = result.getOpenAPI().getInfo().getContact();
    assertEquals(contact.getName(), "tony");
    assertEquals(contact.getUrl(), "url");
    assertEquals(contact.getEmail(), "email");
    assertTrue(messages.contains("attribute info.bad is unexpected"));
    assertTrue(messages.contains("attribute info.contact.invalid is unexpected"));
    assertEquals(result.getOpenAPI().getInfo().getExtensions().get("x-foo").toString(), "bar");
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Contact(io.swagger.v3.oas.models.info.Contact) Test(org.testng.annotations.Test)

Example 5 with Info

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

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