Search in sources :

Example 36 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class SecurityDefinitionTest method createModelWithSecurityRequirements.

@Test(description = "it should create a model with security requirements")
public void createModelWithSecurityRequirements() 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 OpenAPI oas = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    oas.schemaRequirement("githubAccessCode", new SecurityScheme().flows(new OAuthFlows().authorizationCode(new OAuthFlow().scopes(new Scopes().addString("user:email", "Grants read access to a user’s email addresses.")))));
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id");
    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("*/*", new MediaType().schema(new Schema().$ref("Person"))));
    final ApiResponse errorResponse = new ApiResponse().description("error response").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse)).addSecurityItem(new SecurityRequirement().addList("internal_oauth2", "user:email")).addSecurityItem(new SecurityRequirement().addList("api_key"));
    oas.path("/pets", new PathItem().get(get));
    final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
    SerializationMatchers.assertEqualsToJson(oas, json);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) 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) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 37 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class ModelSerializerTest method serializeArrayModel.

@Test(description = "it should serialize an array model")
public void serializeArrayModel() throws IOException {
    final ArraySchema model = new ArraySchema();
    model.setItems(new Schema().$ref("Pet"));
    assertEquals(m.writeValueAsString(model), "{\"type\":\"array\",\"items\":{\"$ref\":\"#/components/schemas/Pet\"}}");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 38 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class ModelSerializerTest method deserializeArrayModel.

@Test(description = "it should deserialize an array model")
public void deserializeArrayModel() throws IOException {
    final String json = "{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/Pet\"}}";
    final Schema p = m.readValue(json, Schema.class);
    assertTrue(p instanceof ArraySchema);
    assertEquals(m.writeValueAsString(p), json);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Test(org.testng.annotations.Test)

Example 39 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class SchemaSerializationTest method serializeRefSchema3_1.

@Test
public void serializeRefSchema3_1() {
    OpenAPI openAPI = new OpenAPI().components(new Components().addSchemas("Pet", new Schema().addProperties("id", new Schema().type("integer")).addProperties("name", new Schema().type("string")).addProperties("tag", new Schema().type("string"))).addSchemas("AnotherPet", new Schema().title("Another Pet").description("Another Pet for petstore referencing Pet schema").$ref("#/components/schemas/Pet").addProperties("category", new Schema().type("string")).addProperties("photoUrl", new Schema().type("string"))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.0.1\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      properties:\n" + "        id: {}\n" + "        name: {}\n" + "        tag: {}\n" + "    AnotherPet:\n" + "      title: Another Pet\n" + "      properties:\n" + "        category: {}\n" + "        photoUrl: {}\n" + "      description: Another Pet for petstore referencing Pet schema\n" + "      $ref: '#/components/schemas/Pet'");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.1\n" + "components:\n" + "  schemas:\n" + "    Pet:\n" + "      properties:\n" + "        id:\n" + "          type: integer\n" + "        name:\n" + "          type: string\n" + "        tag:\n" + "          type: string\n" + "    AnotherPet:\n" + "      $ref: '#/components/schemas/Pet'");
}
Also used : Components(io.swagger.v3.oas.models.Components) Schema(io.swagger.v3.oas.models.media.Schema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 40 with Pet

use of io.swagger.v3.jaxrs2.resources.model.Pet in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testDiscriminatorSerialization.

@Test
public void testDiscriminatorSerialization() {
    Schema<String> propertySchema1 = new StringSchema();
    propertySchema1.addType(propertySchema1.getType());
    Schema<String> propertySchema2 = new StringSchema();
    propertySchema2.addType(propertySchema2.getType());
    Discriminator discriminator = new Discriminator().propertyName("type");
    discriminator.addExtension("x-otherName", "discriminationType");
    Schema schema = new ObjectSchema().addProperties("name", propertySchema1).addProperties("type", propertySchema1).discriminator(discriminator);
    schema.addType(schema.getType());
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("pet", schema));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + "  schemas:\n" + "    pet:\n" + "      properties:\n" + "        name:\n" + "          type: string\n" + "        type:\n" + "          type: string\n" + "      discriminator:\n" + "        propertyName: type\n" + "        x-otherName: discriminationType\n" + "      type: object");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"pet\" : {\n" + "        \"properties\" : {\n" + "          \"name\" : {\n" + "            \"type\" : \"string\"\n" + "          },\n" + "          \"type\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        },\n" + "        \"discriminator\" : {\n" + "          \"propertyName\" : \"type\",\n" + "          \"x-otherName\" : \"discriminationType\"\n" + "        },\n" + "        \"type\" : \"object\"\n" + "      }\n" + "    }\n" + "  }\n" + "}");
    openAPI.openapi("3.0.3");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + "  schemas:\n" + "    pet:\n" + "      properties:\n" + "        name:\n" + "          type: string\n" + "        type:\n" + "          type: string\n" + "      discriminator:\n" + "        propertyName: type\n" + "      type: object");
    SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + "  \"openapi\" : \"3.0.3\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"pet\" : {\n" + "        \"properties\" : {\n" + "          \"name\" : {\n" + "            \"type\" : \"string\"\n" + "          },\n" + "          \"type\" : {\n" + "            \"type\" : \"string\"\n" + "          }\n" + "        },\n" + "        \"discriminator\" : {\n" + "          \"propertyName\" : \"type\"\n" + "        },\n" + "        \"type\" : \"object\"\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Discriminator(io.swagger.v3.oas.models.media.Discriminator) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)69 OpenAPI (io.swagger.v3.oas.models.OpenAPI)60 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)37 Schema (io.swagger.v3.oas.models.media.Schema)32 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)28 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)28 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)24 StringSchema (io.swagger.v3.oas.models.media.StringSchema)24 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)20 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 PathItem (io.swagger.v3.oas.models.PathItem)16 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)15 MapSchema (io.swagger.v3.oas.models.media.MapSchema)12 DateSchema (io.swagger.v3.oas.models.media.DateSchema)10 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)10 ArrayList (java.util.ArrayList)10 Operation (io.swagger.v3.oas.models.Operation)9 Paths (io.swagger.v3.oas.models.Paths)9 Parameter (io.swagger.v3.oas.models.parameters.Parameter)8