Search in sources :

Example 41 with Info

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

the class OpenAPIParserTest method testIssue813.

@Test
public void testIssue813() throws Exception {
    String inputSpec = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"info\": {\n" + "    \"description\": \"This is a sample server Petstore server.  You can find out more about Swagger at <a href=\\\"http://swagger.io\\\">http://swagger.io</a> or on irc.freenode.net, #swagger.  For this sample, you can use the api key \\\"special-key\\\" to test the authorization filters\",\n" + "    \"version\": \"1.0.0\",\n" + "    \"title\": \"Swagger Petstore\",\n" + "    \"termsOfService\": \"http://helloreverb.com/terms/\",\n" + "    \"contact\": {\n" + "      \"email\": \"apiteam@wordnik.com\"\n" + "    },\n" + "    \"license\": {\n" + "      \"name\": \"Apache-2.0\",\n" + "      \"url\": \"http://www.apache.org/licenses/LICENSE-2.0.html\"\n" + "    }\n" + "  },\n" + "  \"host\": \"petstore.swagger.io\",\n" + "  \"basePath\": \"/v2\",\n" + "  \"schemes\": [\n" + "    \"http\"\n" + "  ],\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"post\": {\n" + "        \"tags\": [\n" + "          \"pet\"\n" + "        ],\n" + "        \"summary\": \"Add a new pet to the store\",\n" + "        \"description\": \"\",\n" + "        \"operationId\": \"addPet\",\n" + "        \"consumes\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"produces\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"parameters\": [{\n" + "          \"in\": \"body\",\n" + "          \"name\": \"body\",\n" + "          \"description\": \"Pet object that needs to be added to the store\",\n" + "          \"required\": false,\n" + "          \"schema\": {\n" + "            \"$ref\": \"#/definitions/Pet\"\n" + "          }\n" + "        }],\n" + "        \"responses\": {\n" + "          \"405\": {\n" + "            \"description\": \"Invalid input\"\n" + "          }\n" + "        },\n" + "        \"security\": [{\n" + "          \"petstore_auth\": [\n" + "            \"write:pets\",\n" + "            \"read:pets\"\n" + "          ]\n" + "        }]\n" + "      },\n" + "      \"put\": {\n" + "        \"tags\": [\n" + "          \"pet\"\n" + "        ],\n" + "        \"summary\": \"Update an existing pet\",\n" + "        \"description\": \"\",\n" + "        \"operationId\": \"updatePet\",\n" + "        \"consumes\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"produces\": [\n" + "          \"application/json\",\n" + "          \"application/xml\"\n" + "        ],\n" + "        \"parameters\": [{\n" + "          \"in\": \"body\",\n" + "          \"name\": \"body\",\n" + "          \"description\": \"Pet object that needs to be added to the store\",\n" + "          \"required\": false,\n" + "          \"schema\": {\n" + "            \"$ref\": \"#/definitions/Pet\"\n" + "          }\n" + "        }],\n" + "        \"responses\": {\n" + "          \"405\": {\n" + "            \"description\": \"Validation exception\"\n" + "          },\n" + "          \"404\": {\n" + "            \"description\": \"Pet not found\"\n" + "          },\n" + "          \"400\": {\n" + "            \"description\": \"Invalid ID supplied\"\n" + "          }\n" + "        },\n" + "        \"security\": [{\n" + "          \"petstore_auth\": [\n" + "            \"write:pets\",\n" + "            \"read:pets\"\n" + "          ]\n" + "        }]\n" + "      }\n" + "    },\n" + "    \"securityDefinitions\": {\n" + "      \"api_key\": {\n" + "        \"type\": \"apiKey\",\n" + "        \"name\": \"api_key\",\n" + "        \"in\": \"header\"\n" + "      },\n" + "      \"petstore_auth\": {\n" + "        \"type\": \"oauth2\",\n" + "        \"authorizationUrl\": \"http://petstore.swagger.io/api/oauth/dialog\",\n" + "        \"flow\": \"implicit\",\n" + "        \"scopes\": {\n" + "          \"write:pets\": \"modify pets in your account\",\n" + "          \"read:pets\": \"read your pets\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"definitions\": {\n" + "      \"Pet\": {\n" + "        \"required\": [\n" + "          \"name\",\n" + "          \"photoUrls\"\n" + "        ],\n" + "        \"properties\": {\n" + "          \"id\": {\n" + "            \"type\": \"integer\",\n" + "            \"format\": \"int64\"\n" + "          },\n" + "          \"category\": {\n" + "            \"$ref\": \"#/definitions/Category\"\n" + "          },\n" + "          \"name\": {\n" + "            \"type\": \"string\",\n" + "            \"example\": \"doggie\"\n" + "          },\n" + "          \"photoUrls\": {\n" + "            \"type\": \"array\",\n" + "            \"xml\": {\n" + "              \"name\": \"photoUrl\",\n" + "              \"wrapped\": true\n" + "            },\n" + "            \"items\": {\n" + "              \"type\": \"string\"\n" + "            }\n" + "          },\n" + "          \"tags\": {\n" + "            \"type\": \"array\",\n" + "            \"xml\": {\n" + "              \"name\": \"tag\",\n" + "              \"wrapped\": true\n" + "            },\n" + "            \"items\": {\n" + "              \"$ref\": \"#/definitions/Tag\"\n" + "            }\n" + "          },\n" + "          \"status\": {\n" + "            \"type\": \"string\",\n" + "            \"description\": \"pet status in the store\",\n" + "            \"enum\": [\n" + "              \"available\",\n" + "              \"pending\",\n" + "              \"sold\"\n" + "            ]\n" + "          }\n" + "        },\n" + "        \"xml\": {\n" + "          \"name\": \"Pet\"\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setFlatten(true);
    SwaggerParseResult result = new OpenAPIParser().readContents(inputSpec, null, options);
    assertTrue(result.getOpenAPI() != null);
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Test(org.junit.Test)

Example 42 with Info

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

the class V2ConverterTest method testIssue15.

@Test(description = "Convert extensions everywhere applicable #15")
public void testIssue15() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_15_JSON);
    assertNotNull(oas);
    Info info = oas.getInfo();
    assertNotNull(info.getExtensions().get("x-apis-json"));
    assertNotNull(info.getLicense().getExtensions().get("x-notes"));
    assertNotNull(oas.getExternalDocs().getExtensions().get("x-docs-extension"));
    assertNotNull(oas.getTags().get(0).getExtensions().get("x-tag-extension"));
    assertNotNull(oas.getTags().get(0).getExternalDocs().getExtensions().get("x-tag-docs-extension"));
    PathItem pathItem = oas.getPaths().get("/something");
    assertNotNull(pathItem.getExtensions().get("x-path-item-extension"));
    Operation get = pathItem.getGet();
    assertNotNull(get.getExtensions().get("x-version"));
    assertNotNull(get.getExternalDocs().getExtensions().get("x-operation-docs-extension"));
    assertNotNull(get.getResponses().get("200").getExtensions().get("x-response-extension"));
    ArraySchema schema = (ArraySchema) get.getParameters().get(0).getSchema();
    assertNull(schema.getItems().getExtensions().get(X_EXAMPLE));
    Map<String, SecurityScheme> securitySchemes = oas.getComponents().getSecuritySchemes();
    assertNotNull(securitySchemes);
    assertNotNull(securitySchemes.get("OAuth2Implicit").getExtensions().get("x-auth-extension"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 43 with Info

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

the class V2ConverterTest method testIssue755.

@Test(description = "OpenAPI v2 converter - Conversion of a spec without a info section")
public void testIssue755() throws Exception {
    final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_755_YAML);
    assertNotNull(oas);
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 44 with Info

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

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.addExtension("x-test2", map);
    info.addExtension("x-test", "value");
    final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id").deprecated(true);
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Person")).example("fun")));
    final ApiResponse errorResponse = new ApiResponse().description("error response").addLink("myLink", new Link().description("a link").operationId("theLinkedOperationId").addParameter("userId", "gah")).content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse));
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").addTagsItem("Pet Operations").operationId("add pet").responses(new ApiResponses().addApiResponse("default", errorResponse)).requestBody(new RequestBody().description("the pet to add").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person")))));
    swagger.paths(new Paths().addPathItem("/pets", new PathItem().get(get).post(post)));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    Json.prettyPrint(swagger);
    final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 45 with Info

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

the class SwaggerSerializerTest method writeSpecWithParameterReferences.

@Test(description = "it should write a spec with parameter references")
public void writeSpecWithParameterReferences() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel);
    final QueryParameter parameter = (QueryParameter) new QueryParameter().name("id").description("a common get parameter").schema(new IntegerSchema());
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").operationId("get pet by id").addParametersItem(new Parameter().$ref("#/parameters/Foo"));
    swagger.components(new Components().addParameters("Foo", parameter)).path("/pets", new PathItem().get(get));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
    assertEquals(Json.pretty(rebuilt), Json.pretty(swagger));
}
Also used : QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) Server(io.swagger.v3.oas.models.servers.Server) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) Contact(io.swagger.v3.oas.models.info.Contact) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) 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