Search in sources :

Example 11 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testDeserializeBinaryString.

@Test
public void testDeserializeBinaryString() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  title: foo\n" + "  version: ''\n" + "paths:\n" + "  /test:\n" + "    post:\n" + "      responses:\n" + "        '200':\n" + "          description: ok\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: string\n" + "              format: binary";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
    assertTrue(resolved.getPaths().get("/test").getPost().getRequestBody().getContent().get("application/json").getSchema() instanceof BinarySchema);
}
Also used : BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 12 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testDeserializeDateTimeString.

@Test
public void testDeserializeDateTimeString() {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + "  version: 0.0.0\n" + "  title: My Title\n" + "paths:\n" + "  /persons:\n" + "    get:\n" + "      description: a test\n" + "      responses:\n" + "        '200':\n" + "          description: Successful response\n" + "          content:\n" + "            '*/*':\n" + "              schema:\n" + "                type: object\n" + "                properties:\n" + "                  dateTime:\n" + "                    $ref: '#/components/schemas/DateTimeString'\n" + "components:\n" + "  schemas:\n" + "    DateTimeString:\n" + "      type: string\n" + "      format: date-time\n" + "      default: 2019-01-01T00:00:00Z\n" + "      enum:\n" + "        - null\n" + "        - Nunh uh\n" + "        - 2019-01-01T00:00:00Z\n" + "        - 2018-02-02T23:59:59.999-05:00\n" + "        - 2017-03-03T11:22:33+06:00\n" + "        - 2016-04-04T22:33:44.555Z\n" + "";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
    Schema dateTimeModel = resolved.getComponents().getSchemas().get("DateTimeString");
    assertTrue(dateTimeModel instanceof DateTimeSchema);
    List<OffsetDateTime> dateTimeValues = dateTimeModel.getEnum();
    assertEquals(dateTimeValues.size(), 5);
    assertEquals(dateTimeValues.get(0), null);
    assertEquals(dateTimeValues.get(1), OffsetDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC));
    assertEquals(dateTimeValues.get(2), OffsetDateTime.of(2018, 2, 2, 23, 59, 59, 999000000, ZoneOffset.ofHours(-5)));
    assertEquals(dateTimeValues.get(3), OffsetDateTime.of(2017, 3, 3, 11, 22, 33, 0, ZoneOffset.ofHours(6)));
    assertEquals(dateTimeValues.get(4), OffsetDateTime.of(2016, 4, 4, 22, 33, 44, 555000000, ZoneOffset.UTC));
    assertEquals(dateTimeModel.getDefault(), OffsetDateTime.of(2019, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC));
    assertEquals(result.getMessages(), Arrays.asList("attribute components.schemas.DateTimeString.enum=`Nunh uh` is not of type `date-time`"));
}
Also used : DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) OffsetDateTime(java.time.OffsetDateTime) 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) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 13 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testYamlArrayResponseRemoteRefs.

@Test(description = "resolve array response remote refs in yaml")
public void testYamlArrayResponseRemoteRefs() {
    final OpenAPI swagger = new OpenAPI();
    swagger.path("/fun", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new Schema().$ref(replacePort(REMOTE_REF_YAML))))))))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200");
    final ArraySchema array = (ArraySchema) response.getContent().get("*/*").getSchema();
    assertNotNull(array.getItems());
    assertEquals(array.getItems().get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Content(io.swagger.v3.oas.models.media.Content) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) 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) MediaType(io.swagger.v3.oas.models.media.MediaType) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 14 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testOperationParameterRemoteRefs.

@Test(description = "resolve operation parameter remote refs")
public void testOperationParameterRemoteRefs() {
    final OpenAPI swagger = new OpenAPI();
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(new Parameter().$ref("#/components/parameters/SampleParameter"));
    swagger.path("/fun", new PathItem().get(new Operation().parameters(parameters)));
    swagger.components(new Components().addParameters("SampleParameter", new QueryParameter().name("skip").schema(new IntegerSchema())));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final List<Parameter> params = swagger.getPaths().get("/fun").getGet().getParameters();
    assertEquals(params.size(), 1);
    final Parameter param = params.get(0);
    assertEquals(param.getName(), "skip");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 15 with OpenAPIResolver

use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testMapRemoteModelProperty.

private void testMapRemoteModelProperty(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.components(new Components().addSchemas("Sample", new Schema().addProperties("remoteRef", new Schema().additionalProperties(new Schema().$ref(remoteRef)))));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
    assertTrue(prop.getAdditionalProperties() != null);
    assertEquals(((Schema) prop.getAdditionalProperties()).get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
Also used : Components(io.swagger.v3.oas.models.Components) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) 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) OpenAPIResolver(io.swagger.v3.parser.OpenAPIResolver) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Aggregations

OpenAPIResolver (io.swagger.v3.parser.OpenAPIResolver)21 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Test (org.testng.annotations.Test)16 StringSchema (io.swagger.v3.oas.models.media.StringSchema)15 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)14 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)13 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)13 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)13 Schema (io.swagger.v3.oas.models.media.Schema)13 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)10 Operation (io.swagger.v3.oas.models.Operation)8 PathItem (io.swagger.v3.oas.models.PathItem)8 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)8 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)8 Components (io.swagger.v3.oas.models.Components)7 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)7 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)6 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5