Search in sources :

Example 11 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testInlineResponseModel.

@Test
public void testInlineResponseModel() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    StringSchema stringSchema1 = new StringSchema();
    ObjectSchema objectSchema1 = new ObjectSchema();
    objectSchema1.addProperties("name", stringSchema1);
    objectSchema1.addExtension("x-ext", "ext-prop");
    MediaType mediaType1 = new MediaType();
    mediaType1.setSchema(objectSchema1);
    Content content1 = new Content();
    content1.addMediaType("*/*", mediaType1);
    ApiResponse response1 = new ApiResponse();
    response1.setDescription("it works!");
    response1.setContent(content1);
    ApiResponses responses1 = new ApiResponses();
    responses1.addApiResponse("200", response1);
    Operation operation1 = new Operation();
    operation1.setResponses(responses1);
    PathItem pathItem1 = new PathItem();
    pathItem1.setGet(operation1);
    openAPI.path("/foo/bar", pathItem1);
    StringSchema stringSchema2 = new StringSchema();
    ObjectSchema objectSchema2 = new ObjectSchema();
    objectSchema2.addProperties("name", stringSchema2);
    objectSchema2.addExtension("x-ext", "ext-prop");
    MediaType mediaType2 = new MediaType();
    mediaType2.setSchema(objectSchema2);
    Content content2 = new Content();
    content2.addMediaType("*/*", mediaType2);
    ApiResponse response2 = new ApiResponse();
    response2.setDescription("it works!");
    response2.addExtension("x-foo", "bar");
    response2.setContent(content2);
    ApiResponses responses2 = new ApiResponses();
    responses2.addApiResponse("200", response2);
    Operation operation2 = new Operation();
    operation2.setResponses(responses2);
    PathItem pathItem2 = new PathItem();
    pathItem2.setGet(operation2);
    openAPI.path("/foo/baz", pathItem2);
    new InlineModelResolver().flatten(openAPI);
    Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses();
    ApiResponse response = responses.get("200");
    assertNotNull(response);
    Schema schema = response.getContent().get("*/*").getSchema();
    assertTrue(schema.get$ref() != null);
    assertEquals(1, schema.getExtensions().size());
    assertEquals("ext-prop", schema.getExtensions().get("x-ext"));
    Schema model = openAPI.getComponents().getSchemas().get("inline_response_200");
    assertTrue(model.getProperties().size() == 1);
    assertNotNull(model.getProperties().get("name"));
    assertTrue(model.getProperties().get("name") instanceof StringSchema);
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 12 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method propertyNameMixup.

@Test
public void propertyNameMixup() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("simple.yaml", null, parseOptions);
    assertEquals(((StringSchema) openAPI.getComponents().getSchemas().get("Manufacturer").getProperties().get("name")).getExample(), "ACME Corporation");
    Schema schema = openAPI.getPaths().get("/inventory").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
    assertEquals(((ObjectSchema) ((ArraySchema) schema).getItems().getProperties().get("manufacturer")).getProperties().get("name").getExample(), "ACME Corporation");
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 13 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testSharedSwaggerParametersTest.

@Test(description = "resolve top-level parameters")
public void testSharedSwaggerParametersTest() {
    final OpenAPI swagger = new OpenAPI();
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(0, new Parameter().$ref("username"));
    swagger.path("/fun", new PathItem().get(new Operation().parameters(parameters).responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("ok!")))));
    swagger.components(new Components().addParameters("username", new QueryParameter().name("username").schema(new StringSchema())));
    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    assertTrue(resolved.getComponents().getParameters().size() == 1);
    assertTrue(resolved.getPaths().get("/fun").getGet().getParameters().size() == 1);
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) 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) StringSchema(io.swagger.v3.oas.models.media.StringSchema) 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 StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testInlineMapResponse.

@Test
public void testInlineMapResponse() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    Schema schema = new Schema();
    schema.setAdditionalProperties(new StringSchema());
    schema.addExtension("x-ext", "ext-prop");
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.description("it works!");
    MediaType mediaType = new MediaType();
    mediaType.setSchema(schema);
    Content content = new Content();
    content.addMediaType("*/*", mediaType);
    apiResponse.setContent(content);
    apiResponse.addExtension("x-foo", "bar");
    ApiResponses apiResponses = new ApiResponses();
    apiResponses.addApiResponse("200", apiResponse);
    openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(apiResponses)));
    new InlineModelResolver().flatten(openAPI);
    ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
    Schema property = response.getContent().get("*/*").getSchema();
    assertTrue(property.getAdditionalProperties() != null);
    assertTrue(openAPI.getComponents().getSchemas() == null);
    assertEquals(1, property.getExtensions().size());
    assertEquals("ext-prop", property.getExtensions().get("x-ext"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Test(org.testng.annotations.Test)

Example 15 with StringSchema

use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue1164.

@Test(description = "OpenAPI v2 converter - uses specialized schema subclasses where available")
public void testIssue1164() throws Exception {
    final OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_1164_YAML);
    assertNotNull(oas);
    assertNotNull(oas.getPaths());
    assertNotNull(oas.getPaths().get("/foo"));
    assertNotNull(oas.getPaths().get("/foo").getGet());
    assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody());
    assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody().getContent());
    assertNotNull(oas.getPaths().get("/foo").getGet().getRequestBody().getContent().get("multipart/form-data"));
    Schema formSchema = oas.getPaths().get("/foo").getGet().getRequestBody().getContent().get("multipart/form-data").getSchema();
    assertNotNull(formSchema);
    assertNotNull(formSchema.getProperties());
    assertEquals(4, formSchema.getProperties().size());
    assertTrue(formSchema.getProperties().get("first") instanceof StringSchema);
    assertTrue(formSchema.getProperties().get("second") instanceof BooleanSchema);
    assertTrue(formSchema.getProperties().get("third") instanceof StringSchema);
    StringSchema third = (StringSchema) formSchema.getProperties().get("third");
    assertNotNull(third.getFormat());
    assertTrue("password".equals(third.getFormat()));
    assertTrue(formSchema.getProperties().get("fourth") instanceof BooleanSchema);
    Schema fourth = (Schema) formSchema.getProperties().get("fourth");
    assertNotNull(fourth.getType());
    assertNotNull(fourth.getFormat());
    assertTrue("completely-custom".equals(fourth.getFormat()));
}
Also used : BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

StringSchema (io.swagger.v3.oas.models.media.StringSchema)88 Test (org.testng.annotations.Test)79 Schema (io.swagger.v3.oas.models.media.Schema)69 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)53 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)52 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)47 OpenAPI (io.swagger.v3.oas.models.OpenAPI)40 PathItem (io.swagger.v3.oas.models.PathItem)22 Operation (io.swagger.v3.oas.models.Operation)21 MediaType (io.swagger.v3.oas.models.media.MediaType)21 Content (io.swagger.v3.oas.models.media.Content)20 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)19 MapSchema (io.swagger.v3.oas.models.media.MapSchema)19 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)19 DateSchema (io.swagger.v3.oas.models.media.DateSchema)17 Components (io.swagger.v3.oas.models.Components)15 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)15 Parameter (io.swagger.v3.oas.models.parameters.Parameter)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)15 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)14