Search in sources :

Example 76 with Response

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()));
}
Also used : Response(io.swagger.models.Response) RefProperty(io.swagger.models.properties.RefProperty)

Example 77 with Response

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() {

        {
        }
    };
}
Also used : Response(io.swagger.models.Response) StrictExpectations(mockit.StrictExpectations) FullVerifications(mockit.FullVerifications) Test(org.testng.annotations.Test)

Example 78 with Response

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);
            }
        }
    }
}
Also used : Scheme(io.swagger.models.Scheme) Swagger(io.swagger.models.Swagger) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) HashMap(java.util.HashMap) ArrayProperty(io.swagger.models.properties.ArrayProperty) ResourceNode(org.ballerinalang.model.tree.ResourceNode) Model(io.swagger.models.Model) ArrayList(java.util.ArrayList) VariableNode(org.ballerinalang.model.tree.VariableNode) MediaType(javax.ws.rs.core.MediaType) Lists(com.google.common.collect.Lists) Path(io.swagger.models.Path) Locale(java.util.Locale) Map(java.util.Map) Operation(io.swagger.models.Operation) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Set(java.util.Set) Parameter(io.swagger.models.parameters.Parameter) HttpConstants(org.ballerinalang.net.http.HttpConstants) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Response(io.swagger.models.Response) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) IntegerProperty(io.swagger.models.properties.IntegerProperty) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) BooleanProperty(io.swagger.models.properties.BooleanProperty) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) Response(io.swagger.models.Response) HashMap(java.util.HashMap) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 79 with Response

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();
}
Also used : Path(io.swagger.models.Path) Violation(io.syndesis.common.model.Violation) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) Response(io.swagger.models.Response) Model(io.swagger.models.Model) ArrayModel(io.swagger.models.ArrayModel) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) Map(java.util.Map) HttpMethod(io.swagger.models.HttpMethod)

Example 80 with Response

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();
}
Also used : Response(io.swagger.models.Response) HashMap(java.util.HashMap) XmlSchemaHelper.isElement(io.syndesis.server.connector.generator.swagger.util.XmlSchemaHelper.isElement) XmlSchemaHelper.addElement(io.syndesis.server.connector.generator.swagger.util.XmlSchemaHelper.addElement) Element(org.dom4j.Element) Document(org.dom4j.Document) DataShape(io.syndesis.common.model.DataShape)

Aggregations

Response (io.swagger.models.Response)93 Operation (io.swagger.models.Operation)51 Property (io.swagger.models.properties.Property)30 Path (io.swagger.models.Path)29 Swagger (io.swagger.models.Swagger)29 ApiResponse (io.swagger.annotations.ApiResponse)24 Test (org.testng.annotations.Test)23 RefProperty (io.swagger.models.properties.RefProperty)21 ArrayProperty (io.swagger.models.properties.ArrayProperty)20 Test (org.junit.Test)17 MapProperty (io.swagger.models.properties.MapProperty)14 Parameter (io.swagger.models.parameters.Parameter)13 Model (io.swagger.models.Model)12 BodyParameter (io.swagger.models.parameters.BodyParameter)12 StringProperty (io.swagger.models.properties.StringProperty)12 HashMap (java.util.HashMap)12 RefModel (io.swagger.models.RefModel)10 PathParameter (io.swagger.models.parameters.PathParameter)10 IntegerProperty (io.swagger.models.properties.IntegerProperty)10 QueryParameter (io.swagger.models.parameters.QueryParameter)9