use of io.swagger.v3.oas.models.parameters.RequestBody in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method componentsResolver.
@Test
public void componentsResolver() throws Exception {
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
final JsonNode rootNode = mapper.readTree(pathFile.getBytes());
final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
final SwaggerParseResult result = deserializer.deserialize(rootNode);
Assert.assertNotNull(result);
final OpenAPI openAPI = result.getOpenAPI();
Assert.assertNotNull(openAPI);
assertEquals(new OpenAPIResolver(openAPI, new ArrayList<>(), null).resolve(), openAPI);
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
// internal url schema
Schema pet = schemas.get("Pet");
Schema category = (Schema) pet.getProperties().get("category");
assertEquals(category.get$ref(), "#/components/schemas/Category");
// remote url schema
Schema user = (Schema) pet.getProperties().get("user");
assertEquals(user.get$ref(), "#/components/schemas/User");
// ArraySchema items
ArraySchema tagsProperty = (ArraySchema) pet.getProperties().get("tags");
assertEquals(tagsProperty.getItems().get$ref(), "#/components/schemas/ExampleSchema");
assertEquals(tagsProperty.getType(), "array");
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ExampleSchema"));
// Schema not
assertEquals(schemas.get("OrderRef").getNot().get$ref(), "#/components/schemas/Category");
// Schema additionalProperties
assertTrue(schemas.get("OrderRef").getAdditionalProperties() instanceof Schema);
Schema additionalProperties = (Schema) schemas.get("OrderRef").getAdditionalProperties();
assertEquals(additionalProperties.get$ref(), "#/components/schemas/User");
// AllOfSchema
ComposedSchema extended = (ComposedSchema) schemas.get("ExtendedErrorModel");
Schema root = (Schema) extended.getAllOf().get(0).getProperties().get("rootCause");
assertEquals(root.get$ref(), "#/components/schemas/Category");
Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
// internal response headers
ApiResponse illegalInput = responses.get("IllegalInput");
assertEquals(illegalInput.getHeaders().get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
// internal response links
assertEquals(illegalInput.getLinks().get("address").get$ref(), "#/components/links/unsubscribe");
// internal url response schema
MediaType generalError = responses.get("GeneralError").getContent().get("application/json");
assertEquals(generalError.getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
Map<String, RequestBody> requestBodies = openAPI.getComponents().getRequestBodies();
// internal url requestBody schema
RequestBody requestBody1 = requestBodies.get("requestBody1");
MediaType xmlMedia = requestBody1.getContent().get("application/json");
assertEquals(xmlMedia.getSchema().get$ref(), "#/components/schemas/Pet");
// internal url requestBody ArraySchema
RequestBody requestBody2 = requestBodies.get("requestBody2");
MediaType jsonMedia = requestBody2.getContent().get("application/json");
ArraySchema items = (ArraySchema) jsonMedia.getSchema();
assertEquals(items.getItems().get$ref(), "#/components/schemas/User");
// internal request body
assertEquals("#/components/requestBodies/requestBody2", requestBodies.get("requestBody3").get$ref());
// remote request body url
assertEquals(requestBodies.get("reference").get$ref(), "#/components/requestBodies/remote_requestBody");
Map<String, Parameter> parameters = openAPI.getComponents().getParameters();
// remote url parameter
assertEquals(parameters.get("remoteParameter").get$ref(), "#/components/parameters/parameter");
// internal Schema Parameter
assertEquals(parameters.get("newParam").getSchema().get$ref(), "#/components/schemas/Tag");
// parameter examples
assertEquals(parameters.get("contentParameter").getExamples().get("cat"), openAPI.getComponents().getExamples().get("cat"));
// parameter content schema
assertEquals(parameters.get("contentParameter").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
// internal Schema header
Map<String, Header> headers = openAPI.getComponents().getHeaders();
// header remote schema ref
assertEquals(headers.get("X-Rate-Limit-Remaining").getSchema().get$ref(), "#/components/schemas/User");
// header examples
assertEquals(headers.get("X-Rate-Limit-Reset").getExamples().get("headerExample").get$ref(), "#/components/examples/dog");
// remote header ref
assertEquals(headers.get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
// header content
assertEquals(headers.get("X-Rate-Limit-Reset").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
Map<String, Example> examples = openAPI.getComponents().getExamples();
// internal url example
Example frogExample = examples.get("frog");
assertEquals(frogExample.get$ref(), "#/components/examples/cat");
// remote example url
assertEquals(examples.get("referenceCat").get$ref(), "#/components/examples/example");
// internal url securityScheme
SecurityScheme scheme = openAPI.getComponents().getSecuritySchemes().get("reference");
assertEquals(scheme.getType(), SecurityScheme.Type.APIKEY);
SecurityScheme remoteScheme = openAPI.getComponents().getSecuritySchemes().get("remote_reference");
assertEquals(remoteScheme.getType(), SecurityScheme.Type.OAUTH2);
Map<String, Link> links = openAPI.getComponents().getLinks();
// internal link
assertEquals(openAPI.getComponents().getLinks().get("referenced").get$ref(), "#/components/links/unsubscribe");
// remote ref link
assertEquals(openAPI.getComponents().getLinks().get("subscribe").get$ref(), "#/components/links/link");
Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
// internal callback reference
assertEquals(callbacks.get("referenced").get$ref(), "#/components/callbacks/failed");
// callback pathItem -> operation ->requestBody
assertEquals(callbacks.get("heartbeat").get("$request.query.heartbeat-url").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody3");
// remote callback ref
assertEquals(callbacks.get("remoteCallback").get$ref(), "#/components/callbacks/callback");
}
use of io.swagger.v3.oas.models.parameters.RequestBody in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method testIncompatibleRefs.
@Test
public void testIncompatibleRefs() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + " /test:\n" + " post:\n" + " responses:\n" + " '200':\n" + " $ref: '#/components/schemas/Schema'\n" + " '400':\n" + " definitions: this is right\n" + " description: Bad Request\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " $ref: '#/components/schemas/Schema'\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/Schema'\n" + " required: true\n" + "info:\n" + " version: ''\n" + " title: ''\n" + "components:\n" + " schemas:\n" + " Schema: {}";
SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, null);
assertNotNull(result.getOpenAPI());
}
use of io.swagger.v3.oas.models.parameters.RequestBody in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineArrayRequestBody.
@Test
public void resolveInlineArrayRequestBody() throws Exception {
OpenAPI openAPI = new OpenAPI();
ObjectSchema addressSchema = new ObjectSchema();
addressSchema.addProperties("street", new StringSchema());
ObjectSchema objectSchema = new ObjectSchema();
objectSchema.addProperties("address", addressSchema);
ArraySchema arraySchema = new ArraySchema();
arraySchema.items(objectSchema);
openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(arraySchema))))));
new InlineModelResolver().flatten(openAPI);
RequestBody body = openAPI.getPaths().get("/hello").getGet().getRequestBody();
Schema schema = body.getContent().get("*/*").getSchema();
assertTrue(schema instanceof ArraySchema);
ArraySchema am = (ArraySchema) schema;
Schema inner = am.getItems();
assertTrue(inner.get$ref() != null);
assertEquals("#/components/schemas/hello_body", inner.get$ref());
Schema inline = openAPI.getComponents().getSchemas().get("hello_body");
assertNotNull(inline);
assertTrue(inline instanceof Schema);
Schema address = (Schema) inline.getProperties().get("address");
assertNotNull(address);
assertEquals("#/components/schemas/hello_address", address.get$ref());
Schema inlineProp = openAPI.getComponents().getSchemas().get("hello_address");
assertNotNull(inlineProp);
assertTrue(inlineProp instanceof Schema);
assertNotNull(inlineProp.getProperties().get("street"));
assertTrue(inlineProp.getProperties().get("street") instanceof StringSchema);
}
use of io.swagger.v3.oas.models.parameters.RequestBody in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testBasicInput.
@Test
public void testBasicInput() {
OpenAPI openAPI = new OpenAPI();
openAPI.setComponents(new Components());
Schema user = new Schema();
user.addProperties("name", new StringSchema());
openAPI.path("/foo/baz", new PathItem().post(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("User")))))));
openAPI.getComponents().addSchemas("User", user);
new InlineModelResolver().flatten(openAPI);
}
use of io.swagger.v3.oas.models.parameters.RequestBody in project swagger-parser by swagger-api.
the class InlineModelResolverTest method resolveInlineRequestBody_maxTwoPathParts.
@Test
public void resolveInlineRequestBody_maxTwoPathParts() 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().flatten(openAPI);
Operation getOperation = openAPI.getPaths().get("/api/cloud/greet/hello").getGet();
RequestBody body = getOperation.getRequestBody();
assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null);
Schema bodySchema = openAPI.getComponents().getSchemas().get("greet_hello_body");
assertTrue(bodySchema instanceof Schema);
assertNotNull(bodySchema.getProperties().get("address"));
}
Aggregations