Search in sources :

Example 86 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project carbon-apimgt by wso2.

the class SequenceGenerator method populateParametersFromOperation.

private static void populateParametersFromOperation(Operation operation, Map<String, Model> definitions, Map<String, String> parameterJsonPathMapping, Map<String, String> queryParameters) {
    List<Parameter> parameters = operation.getParameters();
    for (Parameter parameter : parameters) {
        String name = parameter.getName();
        if (parameter instanceof BodyParameter) {
            Model schema = ((BodyParameter) parameter).getSchema();
            if (schema instanceof RefModel) {
                String $ref = ((RefModel) schema).get$ref();
                if (StringUtils.isNotBlank($ref)) {
                    String defName = $ref.substring("#/definitions/".length());
                    Model model = definitions.get(defName);
                    Example example = ExampleBuilder.fromModel(defName, model, definitions, new HashSet<String>());
                    String jsonExample = Json.pretty(example);
                    try {
                        org.json.JSONObject json = new org.json.JSONObject(jsonExample);
                        SequenceUtils.listJson(json, parameterJsonPathMapping);
                    } catch (JSONException e) {
                        log.error("Error occurred while generating json mapping for the definition: " + defName, e);
                    }
                }
            }
        }
        if (parameter instanceof QueryParameter) {
            String type = ((QueryParameter) parameter).getType();
            queryParameters.put(name, type);
        }
    }
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) JSONException(org.json.JSONException) BodyParameter(io.swagger.models.parameters.BodyParameter) Example(io.swagger.inflector.examples.models.Example) ObjectExample(io.swagger.inflector.examples.models.ObjectExample) ArrayExample(io.swagger.inflector.examples.models.ArrayExample) StringExample(io.swagger.inflector.examples.models.StringExample) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 87 with BodyParameter

use of io.swagger.models.parameters.BodyParameter 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 88 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project syndesis by syndesisio.

the class UnifiedJsonDataShapeGenerator method createJsonSchemaForBodyOf.

private static JsonNode createJsonSchemaForBodyOf(final String specification, final Operation operation) {
    final Optional<BodyParameter> maybeRequestBody = findBodyParameter(operation);
    if (!maybeRequestBody.isPresent()) {
        return null;
    }
    final BodyParameter requestBody = maybeRequestBody.get();
    return createSchemaFromModel(specification, requestBody.getSchema());
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 89 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project syndesis by syndesisio.

the class UnifiedXmlDataShapeGenerator method createRequestBodySchema.

private static Element createRequestBodySchema(final Swagger swagger, final Operation operation, final Map<String, SchemaPrefixAndElement> moreSchemas) {
    final Optional<BodyParameter> bodyParameter = findBodyParameter(operation);
    if (!bodyParameter.isPresent()) {
        return null;
    }
    final BodyParameter body = bodyParameter.get();
    final Model bodySchema = body.getSchema();
    final ModelImpl bodySchemaToUse;
    if (bodySchema instanceof RefModel) {
        bodySchemaToUse = dereference((RefModel) bodySchema, swagger);
    } else if (bodySchema instanceof ArrayModel) {
        final Property items = ((ArrayModel) bodySchema).getItems();
        if (items instanceof RefProperty) {
            bodySchemaToUse = dereference((RefProperty) items, swagger);
        } else {
            bodySchemaToUse = new ModelImpl();
            final String name = nameOrDefault(items, "array");
            bodySchemaToUse.name(name);
            bodySchemaToUse.addProperty(name, items);
        }
    } else {
        bodySchemaToUse = (ModelImpl) bodySchema;
    }
    final String targetNamespace = xmlTargetNamespaceOrNull(bodySchemaToUse);
    final Element schema = newXmlSchema(targetNamespace);
    final Element bodyElement = addElement(schema, "element");
    bodyElement.addAttribute("name", nameOf(bodySchemaToUse));
    final Element complexBody = addElement(bodyElement, "complexType");
    final Element bodySequence = addElement(complexBody, "sequence");
    defineElementPropertiesOf(bodySequence, bodySchemaToUse, swagger, moreSchemas);
    defineAttributePropertiesOf(complexBody, bodySchemaToUse);
    return schema;
}
Also used : RefModel(io.swagger.models.RefModel) 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) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) ArrayModel(io.swagger.models.ArrayModel) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) RefProperty(io.swagger.models.properties.RefProperty)

Example 90 with BodyParameter

use of io.swagger.models.parameters.BodyParameter in project syndesis by syndesisio.

the class UnifiedXmlDataShapeGeneratorShapeValidityTest method specifications.

@Parameters
public static Iterable<Object[]> specifications() {
    final List<String> specifications = Collections.singletonList("/swagger/petstore.swagger.json");
    final List<Object[]> parameters = new ArrayList<>();
    specifications.forEach(specification -> {
        String specificationContent;
        try (InputStream in = UnifiedXmlDataShapeGenerator.class.getResourceAsStream(specification)) {
            specificationContent = IOUtils.toString(in, StandardCharsets.UTF_8);
        } catch (final IOException e) {
            throw new AssertionError("Unable to load swagger specification in path: " + specification, e);
        }
        final SwaggerParser parser = new SwaggerParser();
        final Swagger swagger = parser.parse(specificationContent);
        swagger.getPaths().forEach((path, operations) -> {
            operations.getOperationMap().forEach((method, operation) -> {
                final Optional<BodyParameter> bodyParameter = BaseDataShapeGenerator.findBodyParameter(operation);
                if (!bodyParameter.isPresent()) {
                    // only parameters
                    return;
                }
                parameters.add(new Object[] { specificationContent, swagger, operation, specification });
            });
        });
    });
    return parameters;
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

BodyParameter (io.swagger.models.parameters.BodyParameter)96 Test (org.testng.annotations.Test)42 Parameter (io.swagger.models.parameters.Parameter)34 Model (io.swagger.models.Model)30 Operation (io.swagger.models.Operation)28 QueryParameter (io.swagger.models.parameters.QueryParameter)26 ModelImpl (io.swagger.models.ModelImpl)23 Swagger (io.swagger.models.Swagger)23 PathParameter (io.swagger.models.parameters.PathParameter)23 RefModel (io.swagger.models.RefModel)22 ArrayModel (io.swagger.models.ArrayModel)21 Property (io.swagger.models.properties.Property)21 Path (io.swagger.models.Path)20 RefProperty (io.swagger.models.properties.RefProperty)19 StringProperty (io.swagger.models.properties.StringProperty)18 ArrayProperty (io.swagger.models.properties.ArrayProperty)13 HeaderParameter (io.swagger.models.parameters.HeaderParameter)12 Response (io.swagger.models.Response)10 SerializableParameter (io.swagger.models.parameters.SerializableParameter)10 ArrayList (java.util.ArrayList)9