use of io.swagger.models.Path in project camel by apache.
the class RestSwaggerReader method doParseVerbs.
private void doParseVerbs(Swagger swagger, RestDefinition rest, String camelContextId, List<VerbDefinition> verbs, String pathAsTag) {
// used during gathering of apis
List<Path> paths = new ArrayList<>();
String basePath = rest.getPath();
for (VerbDefinition verb : verbs) {
// check if the Verb Definition must be excluded from documentation
Boolean apiDocs;
if (verb.getApiDocs() != null) {
apiDocs = verb.getApiDocs();
} else {
// fallback to option on rest
apiDocs = rest.getApiDocs();
}
if (apiDocs != null && !apiDocs) {
continue;
}
// the method must be in lower case
String method = verb.asVerb().toLowerCase(Locale.US);
// operation path is a key
String opPath = SwaggerHelper.buildUrl(basePath, verb.getUri());
Operation op = new Operation();
if (ObjectHelper.isNotEmpty(pathAsTag)) {
// group in the same tag
op.addTag(pathAsTag);
}
final String routeId = verb.getRouteId();
final String operationId = Optional.ofNullable(rest.getId()).orElse(routeId);
op.operationId(operationId);
// add id as vendor extensions
op.getVendorExtensions().put("x-camelContextId", camelContextId);
op.getVendorExtensions().put("x-routeId", routeId);
Path path = swagger.getPath(opPath);
if (path == null) {
path = new Path();
paths.add(path);
}
path = path.set(method, op);
String consumes = verb.getConsumes() != null ? verb.getConsumes() : rest.getConsumes();
if (consumes != null) {
String[] parts = consumes.split(",");
for (String part : parts) {
op.addConsumes(part);
}
}
String produces = verb.getProduces() != null ? verb.getProduces() : rest.getProduces();
if (produces != null) {
String[] parts = produces.split(",");
for (String part : parts) {
op.addProduces(part);
}
}
if (verb.getDescriptionText() != null) {
op.summary(verb.getDescriptionText());
}
for (RestOperationParamDefinition param : verb.getParams()) {
Parameter parameter = null;
if (param.getType().equals(RestParamType.body)) {
parameter = new BodyParameter();
} else if (param.getType().equals(RestParamType.formData)) {
parameter = new FormParameter();
} else if (param.getType().equals(RestParamType.header)) {
parameter = new HeaderParameter();
} else if (param.getType().equals(RestParamType.path)) {
parameter = new PathParameter();
} else if (param.getType().equals(RestParamType.query)) {
parameter = new QueryParameter();
}
if (parameter != null) {
parameter.setName(param.getName());
parameter.setDescription(param.getDescription());
parameter.setRequired(param.getRequired());
// set type on parameter
if (parameter instanceof SerializableParameter) {
SerializableParameter serializableParameter = (SerializableParameter) parameter;
if (param.getDataType() != null) {
serializableParameter.setType(param.getDataType());
if (param.getDataType().equalsIgnoreCase("array")) {
if (param.getArrayType() != null) {
if (param.getArrayType().equalsIgnoreCase("string")) {
serializableParameter.setItems(new StringProperty());
}
if (param.getArrayType().equalsIgnoreCase("int") || param.getArrayType().equalsIgnoreCase("integer")) {
serializableParameter.setItems(new IntegerProperty());
}
if (param.getArrayType().equalsIgnoreCase("long")) {
serializableParameter.setItems(new LongProperty());
}
if (param.getArrayType().equalsIgnoreCase("float")) {
serializableParameter.setItems(new FloatProperty());
}
if (param.getArrayType().equalsIgnoreCase("double")) {
serializableParameter.setItems(new DoubleProperty());
}
if (param.getArrayType().equalsIgnoreCase("boolean")) {
serializableParameter.setItems(new BooleanProperty());
}
}
}
}
if (param.getCollectionFormat() != null) {
serializableParameter.setCollectionFormat(param.getCollectionFormat().name());
}
if (param.getAllowableValues() != null && !param.getAllowableValues().isEmpty()) {
serializableParameter.setEnum(param.getAllowableValues());
}
}
// set default value on parameter
if (parameter instanceof AbstractSerializableParameter) {
AbstractSerializableParameter qp = (AbstractSerializableParameter) parameter;
if (param.getDefaultValue() != null) {
qp.setDefaultValue(param.getDefaultValue());
}
}
// set schema on body parameter
if (parameter instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) parameter;
if (verb.getType() != null) {
if (verb.getType().endsWith("[]")) {
String typeName = verb.getType();
typeName = typeName.substring(0, typeName.length() - 2);
Property prop = modelTypeAsProperty(typeName, swagger);
if (prop != null) {
ArrayModel arrayModel = new ArrayModel();
arrayModel.setItems(prop);
bp.setSchema(arrayModel);
}
} else {
String ref = modelTypeAsRef(verb.getType(), swagger);
if (ref != null) {
bp.setSchema(new RefModel(ref));
}
}
}
}
op.addParameter(parameter);
}
}
// if we have an out type then set that as response message
if (verb.getOutType() != null) {
Response response = new Response();
Property prop = modelTypeAsProperty(verb.getOutType(), swagger);
response.setSchema(prop);
response.setDescription("Output type");
op.addResponse("200", response);
}
// enrich with configured response messages from the rest-dsl
doParseResponseMessages(swagger, verb, op);
// add path
swagger.path(opPath, path);
}
}
use of io.swagger.models.Path in project camel by apache.
the class SwaggerRestProducerFactory method getSwaggerOperation.
private Operation getSwaggerOperation(Swagger swagger, String verb, String path) {
// path may include base path so skip that
String basePath = swagger.getBasePath();
if (basePath != null && path.startsWith(basePath)) {
path = path.substring(basePath.length());
}
Path modelPath = swagger.getPath(path);
if (modelPath == null) {
return null;
}
// get,put,post,head,delete,patch,options
Operation op = null;
if ("get".equals(verb)) {
op = modelPath.getGet();
} else if ("put".equals(verb)) {
op = modelPath.getPut();
} else if ("post".equals(verb)) {
op = modelPath.getPost();
} else if ("head".equals(verb)) {
op = modelPath.getHead();
} else if ("delete".equals(verb)) {
op = modelPath.getDelete();
} else if ("patch".equals(verb)) {
op = modelPath.getPatch();
} else if ("options".equals(verb)) {
op = modelPath.getOptions();
}
return op;
}
use of io.swagger.models.Path in project camel by apache.
the class RestSwaggerEndpoint method createProducer.
@Override
public Producer createProducer() throws Exception {
final CamelContext camelContext = getCamelContext();
final Swagger swagger = loadSpecificationFrom(camelContext, specificationUri);
final Map<String, Path> paths = swagger.getPaths();
for (final Entry<String, Path> pathEntry : paths.entrySet()) {
final Path path = pathEntry.getValue();
final Optional<Entry<HttpMethod, Operation>> maybeOperationEntry = path.getOperationMap().entrySet().stream().filter(operationEntry -> operationId.equals(operationEntry.getValue().getOperationId())).findAny();
if (maybeOperationEntry.isPresent()) {
final Entry<HttpMethod, Operation> operationEntry = maybeOperationEntry.get();
final String uriTemplate = pathEntry.getKey();
final HttpMethod httpMethod = operationEntry.getKey();
final String method = httpMethod.name();
final Operation operation = operationEntry.getValue();
return createProducerFor(swagger, operation, method, uriTemplate);
}
}
final String supportedOperations = paths.values().stream().flatMap(p -> p.getOperations().stream()).map(Operation::getOperationId).collect(Collectors.joining(", "));
throw new IllegalArgumentException("The specified operation with ID: `" + operationId + "` cannot be found in the Swagger specification loaded from `" + specificationUri + "`. Operations defined in the specification are: " + supportedOperations);
}
use of io.swagger.models.Path in project swagger-core by swagger-api.
the class SpecFilter method removeBrokenReferenceDefinitions.
private Swagger removeBrokenReferenceDefinitions(Swagger swagger) {
if (swagger.getDefinitions() == null || swagger.getDefinitions().isEmpty())
return swagger;
Set<String> referencedDefinitions = new TreeSet<String>();
if (swagger.getResponses() != null) {
for (Response response : swagger.getResponses().values()) {
String propertyRef = getPropertyRef(response.getSchema());
if (propertyRef != null) {
referencedDefinitions.add(propertyRef);
}
}
}
if (swagger.getParameters() != null) {
for (Parameter p : swagger.getParameters().values()) {
if (p instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) p;
Set<String> modelRef = getModelRef(bp.getSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
}
}
}
}
if (swagger.getPaths() != null) {
for (Path path : swagger.getPaths().values()) {
if (path.getParameters() != null) {
for (Parameter p : path.getParameters()) {
if (p instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) p;
Set<String> modelRef = getModelRef(bp.getSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
}
}
}
}
if (path.getOperations() != null) {
for (Operation op : path.getOperations()) {
if (op.getResponses() != null) {
for (Response response : op.getResponses().values()) {
String propertyRef = getPropertyRef(response.getSchema());
if (propertyRef != null) {
referencedDefinitions.add(propertyRef);
}
}
}
if (op.getParameters() != null) {
for (Parameter p : op.getParameters()) {
if (p instanceof BodyParameter) {
BodyParameter bp = (BodyParameter) p;
Set<String> modelRef = getModelRef(bp.getSchema());
if (modelRef != null) {
referencedDefinitions.addAll(modelRef);
}
}
}
}
}
}
}
}
if (swagger.getDefinitions() != null) {
Set<String> nestedReferencedDefinitions = new TreeSet<String>();
for (String ref : referencedDefinitions) {
locateReferencedDefinitions(ref, nestedReferencedDefinitions, swagger);
}
referencedDefinitions.addAll(nestedReferencedDefinitions);
swagger.getDefinitions().keySet().retainAll(referencedDefinitions);
}
return swagger;
}
use of io.swagger.models.Path in project swagger-core by swagger-api.
the class ReaderTest method readerTest1.
@Test
public void readerTest1() {
final Swagger swagger = new Swagger();
Reader.read(swagger, Collections.<Class<?>>singleton(ResourceWithAnnotations.class));
Assert.assertNotNull(swagger);
final Info info = swagger.getInfo();
Assert.assertNotNull(info);
Assert.assertEquals(info.getTitle(), "Test title");
Assert.assertEquals(info.getDescription(), "Test description");
Assert.assertEquals(info.getVersion(), "1.0.0");
Assert.assertEquals(info.getTermsOfService(), "link_to_terms");
Assert.assertEquals(info.getContact().getName(), "Author");
Assert.assertEquals(info.getContact().getEmail(), "author@mail");
Assert.assertEquals(info.getContact().getUrl(), "site");
Assert.assertEquals(info.getLicense().getName(), "License");
Assert.assertEquals(info.getLicense().getUrl(), "license_url");
Assert.assertEquals(info.getVendorExtensions().get("x-ext1_prop1"), "ext1_val1");
Assert.assertEquals(info.getVendorExtensions().get("x-ext1_prop2"), "x-ext1_val2");
Assert.assertEquals(swagger.getHost(), "host");
Assert.assertEquals(swagger.getBasePath(), "/api");
Assert.assertNotNull(swagger.getPath("/resources/testMethod3"));
Assert.assertNotNull(swagger.getDefinitions().get("SampleData"));
Assert.assertEquals(swagger.getExternalDocs().getDescription(), "docs");
Assert.assertEquals(swagger.getExternalDocs().getUrl(), "url_to_docs");
Path path = swagger.getPath("/resources/testMethod3");
Assert.assertNotNull(path);
Operation get = path.getGet();
Assert.assertNotNull(get);
Assert.assertEquals("value", get.getVendorExtensions().get("x-name"));
}
Aggregations