Search in sources :

Example 46 with Swagger

use of io.swagger.models.Swagger 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 Model personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Model 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.setVendorExtension("x-test2", map);
    info.setVendorExtension("x-test", "value");
    final Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").securityDefinition("api-key", new ApiKeyAuthDefinition("key", In.HEADER)).scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel).model("Error", errorModel);
    final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id").deprecated(true);
    get.parameter(new QueryParameter().name("tags").description("tags to filter by").required(false).property(new StringProperty()));
    get.parameter(new PathParameter().name("petId").description("pet to fetch").property(new LongProperty()));
    final Response response = new Response().description("pets returned").schema(new RefProperty().asDefault("Person")).example("application/json", "fun!");
    final Response errorResponse = new Response().description("error response").schema(new RefProperty().asDefault("Error"));
    get.response(200, response).defaultResponse(errorResponse);
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").tag("Pet Operations").operationId("add pet").defaultResponse(errorResponse).parameter(new BodyParameter().description("the pet to add").schema(new RefModel().asDefault("Person")));
    swagger.path("/pets", new Path().get(get).post(post));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) Error(io.swagger.models.Error) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Contact(io.swagger.models.Contact) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Person(io.swagger.models.Person) Test(org.testng.annotations.Test)

Example 47 with Swagger

use of io.swagger.models.Swagger 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 Model 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 Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").securityDefinition("api-key", new ApiKeyAuthDefinition("key", In.HEADER)).scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel);
    final QueryParameter parameter = new QueryParameter().name("id").description("a common get parameter").property(new LongProperty());
    final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id").parameter(new RefParameter("foo"));
    swagger.parameter("foo", parameter).path("/pets", new Path().get(get));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);
    assertEquals(Json.pretty(swagger), Json.pretty(rebuilt));
}
Also used : Path(io.swagger.models.Path) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) QueryParameter(io.swagger.models.parameters.QueryParameter) RefParameter(io.swagger.models.parameters.RefParameter) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) Person(io.swagger.models.Person) Contact(io.swagger.models.Contact) Test(org.testng.annotations.Test)

Example 48 with Swagger

use of io.swagger.models.Swagger in project swagger-core by swagger-api.

the class ParameterSerializationTest method testIssue1765.

@Test(description = "should serialize correctly typed numeric enums")
public void testIssue1765() throws Exception {
    String yaml = "swagger: '2.0'\n" + "paths:\n" + "  /test:\n" + "    get:\n" + "      parameters:\n" + "      - name: \"days\"\n" + "        in: \"path\"\n" + "        required: true\n" + "        type: \"integer\"\n" + "        format: \"int32\"\n" + "        enum:\n" + "        - 1\n" + "        - 2\n" + "        - 3\n" + "        - 4\n" + "        - 5\n" + "      responses:\n" + "        default:\n" + "          description: great";
    Swagger swagger = Yaml.mapper().readValue(yaml, Swagger.class);
    SerializationMatchers.assertEqualsToYaml(swagger, yaml);
}
Also used : Swagger(io.swagger.models.Swagger) Test(org.testng.annotations.Test)

Example 49 with Swagger

use of io.swagger.models.Swagger in project swagger-core by swagger-api.

the class SpecFilterTest method cloneRetainDeperecatedFlags.

@Test(description = "Clone should retain any 'deperecated' flags present on operations")
public void cloneRetainDeperecatedFlags() throws IOException {
    final Swagger swagger = getSwagger("specFiles/deprecatedoperationmodel.json");
    final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
    final Swagger filtered = new SpecFilter().filter(swagger, remover, null, null, null);
    Operation operation = filtered.getPath("/test").getOperations().get(0);
    Boolean deprectedFlag = operation.isDeprecated();
    assertNotNull(deprectedFlag);
    assertEquals(deprectedFlag, Boolean.TRUE);
}
Also used : Swagger(io.swagger.models.Swagger) Operation(io.swagger.models.Operation) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Example 50 with Swagger

use of io.swagger.models.Swagger in project swagger-core by swagger-api.

the class SpecFilterTest method filterAwayGetOperations.

@Test(description = "it should filter away get operations in a resource")
public void filterAwayGetOperations() throws IOException {
    final Swagger swagger = getSwagger("specFiles/petstore.json");
    final NoGetOperationsFilter filter = new NoGetOperationsFilter();
    final Swagger filtered = new SpecFilter().filter(swagger, filter, null, null, null);
    if (filtered.getPaths() != null) {
        for (Map.Entry<String, Path> entry : filtered.getPaths().entrySet()) {
            assertNull(entry.getValue().getGet());
        }
    } else {
        fail("paths should not be null");
    }
}
Also used : Path(io.swagger.models.Path) Swagger(io.swagger.models.Swagger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) SpecFilter(io.swagger.core.filter.SpecFilter) Test(org.testng.annotations.Test)

Aggregations

Swagger (io.swagger.models.Swagger)184 Test (org.testng.annotations.Test)115 Operation (io.swagger.models.Operation)45 Parameter (io.swagger.models.parameters.Parameter)33 QueryParameter (io.swagger.models.parameters.QueryParameter)33 BodyParameter (io.swagger.models.parameters.BodyParameter)29 Path (io.swagger.models.Path)26 PathParameter (io.swagger.models.parameters.PathParameter)25 Reader (io.swagger.jaxrs.Reader)23 SpecFilter (io.swagger.core.filter.SpecFilter)21 Test (org.junit.Test)19 SwaggerParser (io.swagger.parser.SwaggerParser)16 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)16 Response (io.swagger.models.Response)14 SerializableParameter (io.swagger.models.parameters.SerializableParameter)14 HashMap (java.util.HashMap)14 Info (io.swagger.models.Info)12 Map (java.util.Map)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Model (io.swagger.models.Model)10