use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testInlineResponseModelWithTitle.
@Test
public void testInlineResponseModelWithTitle() throws Exception {
OpenAPI openAPI = new OpenAPI();
String responseTitle = "GetBarResponse";
StringSchema stringSchema1 = new StringSchema();
ObjectSchema objectSchema1 = new ObjectSchema();
objectSchema1.setTitle(responseTitle);
objectSchema1.addProperties("name", stringSchema1);
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-foo", "bar");
MediaType mediaType2 = new MediaType();
mediaType2.setSchema(objectSchema2);
Content content2 = new Content();
content2.addMediaType("*/*", mediaType2);
ApiResponse response2 = new ApiResponse();
response2.setDescription("it works!");
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);
assertTrue(response.getContent().get("*/*").getSchema().get$ref() != null);
Schema model = openAPI.getComponents().getSchemas().get(responseTitle);
assertTrue(model.getProperties().size() == 1);
assertNotNull(model.getProperties().get("name"));
assertTrue(model.getProperties().get("name") instanceof StringSchema);
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineArrayResponseWithTitle.
@Test
public void resolveInlineArrayResponseWithTitle() throws Exception {
OpenAPI openAPI = new OpenAPI();
ApiResponse apiResponse = new ApiResponse();
apiResponse.addExtension("x-foo", "bar");
apiResponse.description("it works!");
Map<String, Schema> properties = new HashMap<>();
properties.put("name", new StringSchema());
apiResponse.content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new ObjectSchema().title("FooBar").properties(properties)))));
openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", apiResponse))));
new InlineModelResolver().flatten(openAPI);
ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
assertNotNull(response);
assertNotNull(response.getContent().get("*/*").getSchema());
Schema responseProperty = response.getContent().get("*/*").getSchema();
// no need to flatten more
assertTrue(responseProperty instanceof ArraySchema);
ArraySchema ap = (ArraySchema) responseProperty;
Schema p = ap.getItems();
assertNotNull(p);
assertEquals(p.get$ref(), "#/components/schemas/" + "FooBar");
Schema inline = openAPI.getComponents().getSchemas().get("FooBar");
assertNotNull(inline);
assertTrue(inline instanceof Schema);
assertNotNull(inline.getProperties().get("name"));
assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testSchemaPropertiesBeingPassedToFlattenedModel.
@Test(description = "https://github.com/swagger-api/swagger-parser/issues/1200")
public void testSchemaPropertiesBeingPassedToFlattenedModel() {
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
Schema address = new ObjectSchema();
address.setDeprecated(false);
address.setDescription("My address");
address.setExclusiveMaximum(true);
address.setExclusiveMinimum(true);
address.setFormat("format");
address.setMinLength(Integer.getInteger("10"));
address.setMaximum(BigDecimal.valueOf(50));
address.setMaxItems(Integer.getInteger("1"));
address.setMaxLength(Integer.getInteger("100"));
address.setMaxProperties(Integer.getInteger("1"));
address.setMinimum(BigDecimal.ZERO);
address.setMinItems(Integer.getInteger("0"));
address.setMinLength(Integer.getInteger("10"));
address.setMinProperties(Integer.getInteger("0"));
address.setMultipleOf(BigDecimal.valueOf(2));
address.setName("Address");
address.setNullable(true);
address.setPattern("%dd");
address.setReadOnly(false);
address.setTitle("my address");
address.setUniqueItems(true);
address.setWriteOnly(false);
address.addProperties("city", new StringSchema());
Schema user = new ObjectSchema();
user.setTitle("InnerUserTitle");
user.setDefault("default");
user.setReadOnly(false);
user.setDescription("user description");
user.setName("user name");
user.addProperties("address", address);
openAPI.getComponents().addSchemas("User", user);
new InlineModelResolver(true, true).flatten(openAPI);
Schema model = openAPI.getComponents().getSchemas().get("User");
assertTrue(model instanceof ObjectSchema);
Schema userAddress = openAPI.getComponents().getSchemas().get("MyAddress");
assertNotNull(userAddress);
assertEquals(userAddress.getDeprecated(), Boolean.FALSE);
assertEquals(userAddress.getDescription(), "My address");
assertEquals(userAddress.getExclusiveMaximum(), Boolean.TRUE);
assertEquals(userAddress.getExclusiveMinimum(), Boolean.TRUE);
assertEquals(userAddress.getFormat(), "format");
assertEquals(userAddress.getMaximum(), BigDecimal.valueOf(50));
assertEquals(userAddress.getMaxItems(), Integer.getInteger("1"));
assertEquals(userAddress.getMaxLength(), Integer.getInteger("100"));
assertEquals(userAddress.getMaxProperties(), Integer.getInteger("1"));
assertEquals(userAddress.getMinimum(), BigDecimal.ZERO);
assertEquals(userAddress.getMinItems(), Integer.getInteger("1"));
assertEquals(userAddress.getMinLength(), Integer.getInteger("100"));
assertEquals(userAddress.getMinProperties(), Integer.getInteger("0"));
assertEquals(userAddress.getMultipleOf(), BigDecimal.valueOf(2));
assertEquals(userAddress.getName(), "Address");
assertEquals(userAddress.getNullable(), Boolean.TRUE);
assertEquals(userAddress.getPattern(), "%dd");
assertEquals(userAddress.getReadOnly(), Boolean.FALSE);
assertEquals(userAddress.getTitle(), "my address");
assertEquals(userAddress.getUniqueItems(), Boolean.TRUE);
assertEquals(userAddress.getWriteOnly(), Boolean.FALSE);
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineRequestBody_stripsDotsFromPath.
@Test
public void resolveInlineRequestBody_stripsDotsFromPath() throws Exception {
OpenAPI openAPI = new OpenAPI();
ObjectSchema objectSchema = new ObjectSchema();
objectSchema.addProperties("street", new StringSchema());
Schema schema = new Schema();
schema.addProperties("address", objectSchema);
schema.addProperties("name", new StringSchema());
MediaType mediaType = new MediaType();
mediaType.setSchema(schema);
Content content = new Content();
content.addMediaType("*/*", mediaType);
RequestBody requestBody = new RequestBody();
requestBody.setContent(content);
Operation operation = new Operation();
operation.setRequestBody(requestBody);
PathItem pathItem = new PathItem();
pathItem.setGet(operation);
openAPI.path("/api/Cloud.Greet.Hello", pathItem);
new InlineModelResolver(true, true).flatten(openAPI);
Operation getOperation = openAPI.getPaths().get("/api/Cloud.Greet.Hello").getGet();
RequestBody body = getOperation.getRequestBody();
assertEquals("use dot as common word separator: as it occurs frequently on OData services", "#/components/schemas/ApiCloudGreetHelloBody", body.getContent().get("*/*").getSchema().get$ref());
Schema bodySchema = openAPI.getComponents().getSchemas().get("ApiCloudGreetHelloBody");
assertTrue(bodySchema instanceof Schema);
assertNotNull(bodySchema.getProperties().get("address"));
}
use of io.swagger.v3.oas.models.media.StringSchema in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testInlineMapResponseWithObjectSchema.
@Test
public void testInlineMapResponseWithObjectSchema() throws Exception {
OpenAPI openAPI = new OpenAPI();
Schema schema = new Schema();
schema.setAdditionalProperties(new ObjectSchema().addProperties("name", new StringSchema()));
schema.addExtension("x-ext", "ext-prop");
ApiResponse apiResponse = new ApiResponse().description("it works!").content(new Content().addMediaType("*/*", new MediaType().schema(schema)));
apiResponse.addExtension("x-foo", "bar");
ApiResponses apiResponses = new 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);
assertEquals(1, property.getExtensions().size());
assertEquals("ext-prop", property.getExtensions().get("x-ext"));
assertTrue(openAPI.getComponents().getSchemas().size() == 1);
Schema inline = openAPI.getComponents().getSchemas().get("inline_response_map200");
assertTrue(inline instanceof Schema);
assertNotNull(inline.getProperties().get("name"));
assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
Aggregations