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);
}
}
}
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();
}
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());
}
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;
}
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;
}
Aggregations