use of io.swagger.models.Response in project swagger-parser by swagger-api.
the class SwaggerParserTest method assertResponse.
private void assertResponse(Swagger swagger, Map<String, Response> responsesMap, String responseCode, String expectedDescription, String expectedSchemaRef) {
final Response response = responsesMap.get(responseCode);
final RefProperty schema = (RefProperty) response.getSchema();
assertEquals(response.getDescription(), expectedDescription);
assertEquals(schema.getClass(), RefProperty.class);
assertEquals(schema.get$ref(), expectedSchemaRef);
assertTrue(swagger.getDefinitions().containsKey(schema.getSimpleRef()));
}
use of io.swagger.models.Response in project swagger-parser by swagger-api.
the class ResponseProcessorTest method testProcessResponse.
@Test
public void testProcessResponse(@Injectable final Model responseSchema, @Injectable final Property responseHeader) throws Exception {
new StrictExpectations() {
{
new ModelProcessor(cache, swagger);
times = 1;
result = modelProcessor;
modelProcessor.processModel(responseSchema);
times = 1;
}
};
Response response = new Response();
response.setResponseSchema(responseSchema);
response.addHeader("foo", responseHeader);
new ResponseProcessor(cache, swagger).processResponse(response);
new FullVerifications() {
{
}
};
}
use of io.swagger.models.Response in project ballerina by ballerina-lang.
the class SwaggerResourceMapper method parseResponsesAnnotationAttachment.
/**
* Parses the 'Responses' annotation attachment and build swagger operation.
* @param resource The ballerina resource definition.
* @param op The swagger operation.
*/
private void parseResponsesAnnotationAttachment(ResourceNode resource, Operation op) {
Optional<? extends AnnotationAttachmentNode> responsesAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "Responses".equals(a.getAnnotationName().getValue())).findFirst();
if (responsesAnnotation.isPresent()) {
Map<String, AnnotationAttachmentAttributeValueNode> responsesAttributes = this.listToMap(responsesAnnotation.get());
if (responsesAttributes.containsKey("value")) {
List<? extends AnnotationAttachmentAttributeValueNode> responsesValues = responsesAttributes.get("value").getValueArray();
if (responsesValues.size() > 0) {
Map<String, Response> responses = new HashMap<>();
for (AnnotationAttachmentAttributeValueNode responsesValue : responsesValues) {
AnnotationAttachmentNode responseAnnotationAttachment = (AnnotationAttachmentNode) responsesValue.getValue();
Map<String, AnnotationAttachmentAttributeValueNode> responseAttributes = this.listToMap(responseAnnotationAttachment);
if (responseAttributes.containsKey("code")) {
String code = this.getStringLiteralValue(responseAttributes.get("code"));
Response response = new Response();
if (responseAttributes.containsKey("description")) {
response.setDescription(this.getStringLiteralValue(responseAttributes.get("description")));
}
// TODO: Parse 'response' attribute for $.paths./resource-path.responses[*]["code"].schema
this.createHeadersModel(responseAttributes.get("headers"), response);
responses.put(code, response);
}
}
op.setResponses(responses);
}
}
}
}
use of io.swagger.models.Response in project syndesis by syndesisio.
the class SyndesisSwaggerValidationRules method validateResponses.
/**
* Check if a request/response JSON schema is present
*/
@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.StdCyclomaticComplexity", "PMD.ModifiedCyclomaticComplexity" })
private SwaggerModelInfo validateResponses(final SwaggerModelInfo swaggerModelInfo) {
if (swaggerModelInfo.getModel() == null) {
return swaggerModelInfo;
}
final SwaggerModelInfo.Builder withWarnings = new SwaggerModelInfo.Builder().createFrom(swaggerModelInfo);
for (final Map.Entry<String, Path> pathEntry : notNull(swaggerModelInfo.getModel().getPaths()).entrySet()) {
for (final Map.Entry<HttpMethod, Operation> operationEntry : notNull(pathEntry.getValue().getOperationMap()).entrySet()) {
// Check requests
for (final Parameter parameter : notNull(operationEntry.getValue().getParameters())) {
if (!(parameter instanceof BodyParameter)) {
continue;
}
final BodyParameter bodyParameter = (BodyParameter) parameter;
final Model schema = bodyParameter.getSchema();
if (schemaIsNotSpecified(schema)) {
final String message = "Operation " + operationEntry.getKey() + " " + pathEntry.getKey() + " does not provide a schema for the body parameter";
withWarnings.addWarning(//
new Violation.Builder().property(//
"").error(//
"missing-parameter-schema").message(//
message).build());
}
}
// Check responses
for (final Map.Entry<String, Response> responseEntry : notNull(operationEntry.getValue().getResponses()).entrySet()) {
if (responseEntry.getKey().charAt(0) != '2') {
// check only correct responses
continue;
}
if (responseEntry.getValue().getSchema() == null) {
final String message = "Operation " + operationEntry.getKey() + " " + pathEntry.getKey() + " does not provide a response schema for code " + responseEntry.getKey();
withWarnings.addWarning(//
new Violation.Builder().property(//
"").error(//
"missing-response-schema").message(//
message).build());
}
}
// Assume that operations without 2xx responses do not provide a
// response
}
}
return withWarnings.build();
}
use of io.swagger.models.Response in project syndesis by syndesisio.
the class UnifiedXmlDataShapeGenerator method createShapeFromResponse.
@Override
public DataShape createShapeFromResponse(final String specification, final Swagger swagger, final Operation operation) {
final Optional<Response> maybeResponse = findResponse(operation);
if (!maybeResponse.isPresent()) {
return DATA_SHAPE_NONE;
}
final Document document = DocumentHelper.createDocument();
final Element schemaSet = document.addElement("d:SchemaSet", SCHEMA_SET_NS);
schemaSet.addNamespace(XmlSchemaHelper.XML_SCHEMA_PREFIX, XmlSchemaHelper.XML_SCHEMA_NS);
final Map<String, SchemaPrefixAndElement> moreSchemas = new HashMap<>();
final Element bodySchema = createResponseBodySchema(swagger, operation, moreSchemas);
if (bodySchema == null) {
return DATA_SHAPE_NONE;
}
schemaSet.add(bodySchema.detach());
if (!moreSchemas.isEmpty()) {
final Element additionalSchemas = schemaSet.addElement("d:AdditionalSchemas");
moreSchemas.values().forEach(e -> additionalSchemas.add(e.schema.detach()));
}
final String xmlSchemaSet = serialize(document);
return //
new DataShape.Builder().name(//
"Response").description(//
"API response payload").kind(//
DataShapeKinds.XML_SCHEMA).specification(//
xmlSchemaSet).build();
}
Aggregations