use of io.swagger.models.parameters.AbstractSerializableParameter 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.parameters.AbstractSerializableParameter in project syndesis by syndesisio.
the class UnifiedXmlDataShapeGenerator method createParametersSchema.
private static Element createParametersSchema(final Operation operation) {
final List<Parameter> operationParameters = operation.getParameters();
final List<AbstractSerializableParameter<?>> serializableParameters = //
operationParameters.stream().filter(//
PARAM_CLASS::isInstance).map(//
PARAM_CLASS::cast).collect(Collectors.toList());
if (serializableParameters.isEmpty()) {
return null;
}
final Element schema = newXmlSchema(SYNDESIS_PARAMETERS_NS);
final Element parameters = addElement(schema, "element");
parameters.addAttribute("name", "parameters");
final Element complex = addElement(parameters, "complexType");
final Element sequence = addElement(complex, "sequence");
for (final AbstractSerializableParameter<?> serializableParameter : serializableParameters) {
final String type = toXsdType(serializableParameter.getType());
final String name = trimToNull(serializableParameter.getName());
if ("file".equals(type)) {
// 'file' type is not allowed in JSON schema
continue;
}
final Element element = addElement(sequence, "element");
element.addAttribute("name", name);
if (type != null) {
element.addAttribute("type", type);
}
final Object defaultValue = serializableParameter.getDefault();
if (defaultValue != null) {
element.addAttribute("default", String.valueOf(defaultValue));
}
addEnumsTo(element, serializableParameter);
}
return schema;
}
use of io.swagger.models.parameters.AbstractSerializableParameter in project cxf by apache.
the class JaxRs2Extension method applyBeanValidatorAnnotations.
/**
* This is essentially a duplicate of {@link io.swagger.jackson.ModelResolver.applyBeanValidatorAnnotations}.
*
* @param parameter
* @param annotations
*/
private void applyBeanValidatorAnnotations(final Parameter parameter, final List<Annotation> annotations) {
Map<String, Annotation> annos = new HashMap<>();
if (annotations != null) {
for (Annotation annotation : annotations) {
annos.put(annotation.annotationType().getName(), annotation);
}
}
if (annos.containsKey(NotNull.class.getName())) {
parameter.setRequired(true);
}
if (parameter instanceof AbstractSerializableParameter) {
AbstractSerializableParameter<?> serializable = (AbstractSerializableParameter<?>) parameter;
if (annos.containsKey(Min.class.getName())) {
Min min = (Min) annos.get(Min.class.getName());
serializable.setMinimum(BigDecimal.valueOf(min.value()));
}
if (annos.containsKey(Max.class.getName())) {
Max max = (Max) annos.get(Max.class.getName());
serializable.setMaximum(BigDecimal.valueOf(max.value()));
}
if (annos.containsKey(Size.class.getName())) {
Size size = (Size) annos.get(Size.class.getName());
serializable.setMinimum(BigDecimal.valueOf(size.min()));
serializable.setMaximum(BigDecimal.valueOf(size.max()));
serializable.setMinItems(size.min());
serializable.setMaxItems(size.max());
}
if (annos.containsKey(DecimalMin.class.getName())) {
DecimalMin min = (DecimalMin) annos.get(DecimalMin.class.getName());
if (min.inclusive()) {
serializable.setMinimum(BigDecimal.valueOf(Double.valueOf(min.value())));
} else {
serializable.setExclusiveMinimum(!min.inclusive());
}
}
if (annos.containsKey(DecimalMax.class.getName())) {
DecimalMax max = (DecimalMax) annos.get(DecimalMax.class.getName());
if (max.inclusive()) {
serializable.setMaximum(BigDecimal.valueOf(Double.valueOf(max.value())));
} else {
serializable.setExclusiveMaximum(!max.inclusive());
}
}
if (annos.containsKey(Pattern.class.getName())) {
Pattern pattern = (Pattern) annos.get(Pattern.class.getName());
serializable.setPattern(pattern.regexp());
}
}
}
use of io.swagger.models.parameters.AbstractSerializableParameter in project swagger-parser by swagger-api.
the class SwaggerConverter method convert.
private Schema convert(SerializableParameter sp) {
Schema schema;
if ("file".equals(sp.getType())) {
schema = new FileSchema();
} else if ("array".equals(sp.getType())) {
ArraySchema as = new ArraySchema();
if (sp.getItems() != null) {
as.setItems(convert(sp.getItems()));
}
schema = as;
} else {
PrimitiveType ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), sp.getFormat());
if (ptype != null) {
schema = ptype.createProperty();
} else {
ptype = PrimitiveType.fromTypeAndFormat(sp.getType(), null);
if (ptype != null) {
schema = ptype.createProperty();
schema.setFormat(sp.getFormat());
} else {
schema = new Schema();
schema.setType(sp.getType());
schema.setFormat(sp.getFormat());
}
}
}
schema.setDescription(sp.getDescription());
schema.setReadOnly(sp.isReadOnly());
schema.setEnum(sp.getEnum());
if (sp.getMaxItems() != null) {
schema.setMaxItems(sp.getMaxItems());
}
if (sp.getMinItems() != null) {
schema.setMinItems(sp.getMinItems());
}
if (sp.isUniqueItems() != null) {
schema.setUniqueItems(sp.isUniqueItems());
}
schema.setMaximum(sp.getMaximum());
schema.setExclusiveMaximum(sp.isExclusiveMaximum());
schema.setMinimum(sp.getMinimum());
schema.setExclusiveMinimum(sp.isExclusiveMinimum());
schema.setMinLength(sp.getMinLength());
schema.setMaxLength(sp.getMaxLength());
schema.setName(sp.getName());
if (sp.getVendorExtensions() != null) {
Object exampleExtension = sp.getVendorExtensions().get("x-example");
if (exampleExtension != null) {
schema.setExample(exampleExtension);
}
Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
if (nullableExtension != null) {
schema.setNullable((Boolean) nullableExtension);
}
schema.setExtensions(convert(sp.getVendorExtensions()));
}
if (sp.getMultipleOf() != null) {
schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
}
schema.setPattern(sp.getPattern());
if (sp instanceof AbstractSerializableParameter) {
AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
schema.setDefault(ap.getDefault());
}
return schema;
}
use of io.swagger.models.parameters.AbstractSerializableParameter in project swagger-core by swagger-api.
the class ParameterProcessor method applyAnnotations.
public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, Type type, List<Annotation> annotations) {
final AnnotationsHelper helper = new AnnotationsHelper(annotations, type);
if (helper.isContext()) {
return null;
}
final ParamWrapper<?> param = helper.getApiParam();
if (param.isHidden()) {
return null;
}
final String defaultValue = helper.getDefaultValue();
if (parameter instanceof AbstractSerializableParameter) {
final AbstractSerializableParameter<?> p = (AbstractSerializableParameter<?>) parameter;
if (param.isRequired()) {
p.setRequired(true);
}
if (param.getReadOnly()) {
p.readOnly(param.getReadOnly());
}
if (param.getAllowEmptyValue()) {
p.allowEmptyValue(param.getAllowEmptyValue());
}
if (StringUtils.isNotEmpty(param.getName())) {
p.setName(param.getName());
}
if (StringUtils.isNotEmpty(param.getDescription())) {
p.setDescription(param.getDescription());
}
if (StringUtils.isNotEmpty(param.getExample())) {
p.setExample(param.getExample());
}
if (StringUtils.isNotEmpty(param.getAccess())) {
p.setAccess(param.getAccess());
}
if (StringUtils.isNoneEmpty(param.getCollectionFormat())) {
p.setCollectionFormat(param.getCollectionFormat());
}
if (StringUtils.isNotEmpty(param.getDataType())) {
if ("java.io.File".equalsIgnoreCase(param.getDataType())) {
p.setProperty(new FileProperty());
} else if ("long".equalsIgnoreCase(param.getDataType())) {
p.setProperty(new LongProperty());
} else {
p.setType(param.getDataType());
}
}
if (helper.getMin() != null) {
p.setMinimum(helper.getMin());
if (helper.isMinExclusive()) {
p.setExclusiveMinimum(true);
}
}
if (helper.getMax() != null) {
p.setMaximum(helper.getMax());
if (helper.isMaxExclusive()) {
p.setExclusiveMaximum(true);
}
}
if (helper.getMinItems() != null) {
p.setMinItems(helper.getMinItems());
}
if (helper.getMaxItems() != null) {
p.setMaxItems(helper.getMaxItems());
}
if (helper.getMinLength() != null) {
p.setMinLength(helper.getMinLength());
}
if (helper.getMaxLength() != null) {
p.setMaxLength(helper.getMaxLength());
}
if (helper.getPattern() != null) {
p.setPattern(helper.getPattern());
}
if (helper.isRequired() != null) {
p.setRequired(true);
}
if (helper.getType() != null) {
p.setType(helper.getType());
}
if (helper.getFormat() != null) {
p.setFormat(helper.getFormat());
}
AllowableValues allowableValues = AllowableValuesUtils.create(param.getAllowableValues());
if (p.getItems() != null || param.isAllowMultiple()) {
if (p.getItems() == null) {
// Convert to array
final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
args.put(PropertyBuilder.PropertyId.DEFAULT, p.getDefaultValue());
p.setDefaultValue(null);
args.put(PropertyBuilder.PropertyId.ENUM, p.getEnum());
p.setEnum(null);
args.put(PropertyBuilder.PropertyId.MINIMUM, p.getMinimum());
p.setMinimum(null);
args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM, p.isExclusiveMinimum());
p.setExclusiveMinimum(null);
args.put(PropertyBuilder.PropertyId.MAXIMUM, p.getMaximum());
p.setMaximum(null);
args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM, p.isExclusiveMaximum());
args.put(PropertyBuilder.PropertyId.MIN_LENGTH, p.getMinLength());
p.setMinLength(null);
args.put(PropertyBuilder.PropertyId.MAX_LENGTH, p.getMaxLength());
p.setMaxLength(null);
args.put(PropertyBuilder.PropertyId.PATTERN, p.getPattern());
p.setPattern(null);
args.put(PropertyBuilder.PropertyId.EXAMPLE, p.getExample());
p.setExclusiveMaximum(null);
Property items = PropertyBuilder.build(p.getType(), p.getFormat(), args);
p.type(ArrayProperty.TYPE).format(null).items(items);
}
final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
if (StringUtils.isNotEmpty(defaultValue)) {
args.put(PropertyBuilder.PropertyId.DEFAULT, defaultValue);
}
if (helper.getMin() != null) {
args.put(PropertyBuilder.PropertyId.MINIMUM, helper.getMin());
if (helper.isMinExclusive()) {
args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM, true);
}
}
if (helper.getMax() != null) {
args.put(PropertyBuilder.PropertyId.MAXIMUM, helper.getMax());
if (helper.isMaxExclusive()) {
args.put(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM, true);
}
}
if (helper.getMinLength() != null) {
args.put(PropertyBuilder.PropertyId.MIN_LENGTH, helper.getMinLength());
}
if (helper.getMaxLength() != null) {
args.put(PropertyBuilder.PropertyId.MAX_LENGTH, helper.getMaxLength());
}
if (helper.getPattern() != null) {
args.put(PropertyBuilder.PropertyId.PATTERN, helper.getPattern());
}
//Overwrite Bean validation values with allowable values if present
if (allowableValues != null) {
args.putAll(allowableValues.asPropertyArguments());
}
PropertyBuilder.merge(p.getItems(), args);
} else {
if (StringUtils.isNotEmpty(defaultValue)) {
p.setDefaultValue(defaultValue);
}
//Overwrite Bean validation values with allowable values if present
if (allowableValues != null) {
processAllowedValues(allowableValues, p);
}
// else {
// processJsr303Annotations(helper, p);
// }
}
} else {
// must be a body param
BodyParameter bp = new BodyParameter();
if (helper.getApiParam() != null) {
ParamWrapper<?> pw = helper.getApiParam();
if (pw instanceof ApiParamWrapper) {
ApiParamWrapper apiParam = (ApiParamWrapper) pw;
Example example = apiParam.getExamples();
if (example != null && example.value() != null) {
for (ExampleProperty ex : example.value()) {
String mediaType = ex.mediaType();
String value = ex.value();
if (!mediaType.isEmpty() && !value.isEmpty()) {
bp.example(mediaType.trim(), value.trim());
}
}
}
} else if (pw instanceof ApiImplicitParamWrapper) {
ApiImplicitParamWrapper apiParam = (ApiImplicitParamWrapper) pw;
Example example = apiParam.getExamples();
if (example != null && example.value() != null) {
for (ExampleProperty ex : example.value()) {
String mediaType = ex.mediaType();
String value = ex.value();
if (!mediaType.isEmpty() && !value.isEmpty()) {
bp.example(mediaType.trim(), value.trim());
}
}
}
}
}
bp.setRequired(param.isRequired());
bp.setName(StringUtils.isNotEmpty(param.getName()) ? param.getName() : "body");
if (StringUtils.isNotEmpty(param.getDescription())) {
bp.setDescription(param.getDescription());
}
if (StringUtils.isNotEmpty(param.getAccess())) {
bp.setAccess(param.getAccess());
}
final Property property = ModelConverters.getInstance().readAsProperty(type);
if (property != null) {
final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(PropertyBuilder.PropertyId.class);
if (StringUtils.isNotEmpty(defaultValue)) {
args.put(PropertyBuilder.PropertyId.DEFAULT, defaultValue);
}
bp.setSchema(PropertyBuilder.toModel(PropertyBuilder.merge(property, args)));
for (Map.Entry<String, Model> entry : ModelConverters.getInstance().readAll(type).entrySet()) {
swagger.addDefinition(entry.getKey(), entry.getValue());
}
}
parameter = bp;
}
return parameter;
}
Aggregations