use of io.swagger.models.properties.DoubleProperty 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.properties.DoubleProperty in project camel by apache.
the class RestSwaggerReader method doParseResponseMessages.
private void doParseResponseMessages(Swagger swagger, VerbDefinition verb, Operation op) {
for (RestOperationResponseMsgDefinition msg : verb.getResponseMsgs()) {
Response response = null;
if (op.getResponses() != null) {
response = op.getResponses().get(msg.getCode());
}
if (response == null) {
response = new Response();
}
if (ObjectHelper.isNotEmpty(msg.getResponseModel())) {
Property prop = modelTypeAsProperty(msg.getResponseModel(), swagger);
response.setSchema(prop);
}
response.setDescription(msg.getMessage());
// add headers
if (msg.getHeaders() != null) {
for (RestOperationResponseHeaderDefinition header : msg.getHeaders()) {
String name = header.getName();
String type = header.getDataType();
if ("string".equals(type)) {
StringProperty sp = new StringProperty();
sp.setName(name);
sp.setDescription(header.getDescription());
if (header.getAllowableValues() != null) {
sp.setEnum(header.getAllowableValues());
}
response.addHeader(name, sp);
} else if ("int".equals(type) || "integer".equals(type)) {
IntegerProperty ip = new IntegerProperty();
ip.setName(name);
ip.setDescription(header.getDescription());
List<Integer> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Integer>();
for (String text : header.getAllowableValues()) {
values.add(Integer.valueOf(text));
}
ip.setEnum(values);
}
response.addHeader(name, ip);
} else if ("long".equals(type)) {
LongProperty lp = new LongProperty();
lp.setName(name);
lp.setDescription(header.getDescription());
List<Long> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Long>();
for (String text : header.getAllowableValues()) {
values.add(Long.valueOf(text));
}
lp.setEnum(values);
}
response.addHeader(name, lp);
} else if ("float".equals(type)) {
FloatProperty lp = new FloatProperty();
lp.setName(name);
lp.setDescription(header.getDescription());
List<Float> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Float>();
for (String text : header.getAllowableValues()) {
values.add(Float.valueOf(text));
}
lp.setEnum(values);
}
response.addHeader(name, lp);
} else if ("double".equals(type)) {
DoubleProperty dp = new DoubleProperty();
dp.setName(name);
dp.setDescription(header.getDescription());
List<Double> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Double>();
for (String text : header.getAllowableValues()) {
values.add(Double.valueOf(text));
}
dp.setEnum(values);
}
response.addHeader(name, dp);
} else if ("boolean".equals(type)) {
BooleanProperty bp = new BooleanProperty();
bp.setName(name);
bp.setDescription(header.getDescription());
response.addHeader(name, bp);
} else if ("array".equals(type)) {
ArrayProperty ap = new ArrayProperty();
ap.setName(name);
ap.setDescription(header.getDescription());
if (header.getArrayType() != null) {
if (header.getArrayType().equalsIgnoreCase("string")) {
ap.setItems(new StringProperty());
}
if (header.getArrayType().equalsIgnoreCase("int") || header.getArrayType().equalsIgnoreCase("integer")) {
ap.setItems(new IntegerProperty());
}
if (header.getArrayType().equalsIgnoreCase("long")) {
ap.setItems(new LongProperty());
}
if (header.getArrayType().equalsIgnoreCase("float")) {
ap.setItems(new FloatProperty());
}
if (header.getArrayType().equalsIgnoreCase("double")) {
ap.setItems(new DoubleProperty());
}
if (header.getArrayType().equalsIgnoreCase("boolean")) {
ap.setItems(new BooleanProperty());
}
}
response.addHeader(name, ap);
}
}
}
op.addResponse(msg.getCode(), response);
}
}
use of io.swagger.models.properties.DoubleProperty in project swagger-core by swagger-api.
the class ModelWithRangesTest method modelWithRangesTest.
@Test(description = "test model with @ApiModelProperty.allowableValues")
public void modelWithRangesTest() {
final Map<String, Property> properties = ModelConverters.getInstance().read(ModelWithRanges.class).get("ModelWithRanges").getProperties();
final IntegerProperty inclusiveRange = (IntegerProperty) properties.get("inclusiveRange");
assertEquals(inclusiveRange.getMinimum(), new BigDecimal(1));
assertEquals(inclusiveRange.getMaximum(), new BigDecimal(5));
assertNull(inclusiveRange.getExclusiveMaximum());
assertNull(inclusiveRange.getExclusiveMinimum());
final IntegerProperty exclusiveRange = (IntegerProperty) properties.get("exclusiveRange");
assertEquals(exclusiveRange.getMinimum(), new BigDecimal(1));
assertEquals(exclusiveRange.getMaximum(), new BigDecimal(5));
assertEquals(exclusiveRange.getExclusiveMinimum(), Boolean.TRUE);
assertEquals(exclusiveRange.getExclusiveMaximum(), Boolean.TRUE);
final IntegerProperty positiveInfinityRange = (IntegerProperty) properties.get("positiveInfinityRange");
assertEquals(positiveInfinityRange.getMinimum(), new BigDecimal(1.0));
assertNull(positiveInfinityRange.getMaximum());
assertNull(positiveInfinityRange.getExclusiveMaximum());
assertNull(positiveInfinityRange.getExclusiveMinimum());
final IntegerProperty negativeInfinityRange = (IntegerProperty) properties.get("negativeInfinityRange");
assertNull(negativeInfinityRange.getMinimum());
assertEquals(negativeInfinityRange.getMaximum(), new BigDecimal(5.0));
assertNull(negativeInfinityRange.getExclusiveMaximum());
assertNull(negativeInfinityRange.getExclusiveMinimum());
final StringProperty stringValues = (StringProperty) properties.get("stringValues");
assertEquals(stringValues.getEnum(), Arrays.asList("str1", "str2"));
final DoubleProperty doubleValues = (DoubleProperty) properties.get("doubleValues");
assertEquals(doubleValues.getMinimum(), new BigDecimal("1.0"));
assertEquals(doubleValues.getMaximum(), new BigDecimal("8.0"));
assertEquals(doubleValues.getExclusiveMaximum(), Boolean.TRUE);
assertNull(doubleValues.getExclusiveMinimum());
}
use of io.swagger.models.properties.DoubleProperty in project swagger-core by swagger-api.
the class ParameterProcessorTest method beanValidationArrayParametrizedMethodTest.
@Test
public void beanValidationArrayParametrizedMethodTest() throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod("beanValidationArrayParametrizedMethod", List.class, List.class, List.class, String.class);
final Type[] genericParameterTypes = method.getGenericParameterTypes();
final Annotation[][] paramAnnotations = method.getParameterAnnotations();
//First param - items specified
HeaderParameter headerParam1 = new HeaderParameter().type(ArrayProperty.TYPE).items(new LongProperty());
HeaderParameter param1 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam1, genericParameterTypes[0], Arrays.asList(paramAnnotations[0]));
assertNotNull(param1);
assertEquals((int) param1.getMinItems(), 5);
assertEquals((int) param1.getMaxItems(), 10);
Property items1 = param1.getItems();
assertTrue(items1 instanceof LongProperty);
LongProperty longItems = (LongProperty) items1;
assertEquals(longItems.getMinimum(), new BigDecimal(5));
assertNull(longItems.getExclusiveMinimum());
assertEquals(longItems.getMaximum(), new BigDecimal(10));
assertNull(longItems.getExclusiveMaximum());
//Second param - items specified
HeaderParameter headerParam2 = new HeaderParameter().type(ArrayProperty.TYPE).items(new DoubleProperty());
HeaderParameter param2 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam2, genericParameterTypes[1], Arrays.asList(paramAnnotations[1]));
assertNotNull(param2);
assertEquals((int) param2.getMinItems(), 5);
assertEquals((int) param2.getMaxItems(), 10);
Property items2 = param2.getItems();
assertTrue(items2 instanceof DoubleProperty);
DoubleProperty doubleItems = (DoubleProperty) items2;
assertEquals(doubleItems.getMinimum(), new BigDecimal(5.5));
assertTrue(doubleItems.getExclusiveMinimum());
assertEquals(doubleItems.getMaximum(), new BigDecimal(10.5));
assertTrue(doubleItems.getExclusiveMaximum());
//Third param - items specified
HeaderParameter headerParam3 = new HeaderParameter().type(ArrayProperty.TYPE).items(new StringProperty());
HeaderParameter param3 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam3, genericParameterTypes[2], Arrays.asList(paramAnnotations[2]));
assertNotNull(param3);
assertEquals((int) param3.getMinItems(), 5);
assertEquals((int) param3.getMaxItems(), 10);
Property items3 = param3.getItems();
assertTrue(items3 instanceof StringProperty);
StringProperty stringItems = (StringProperty) items3;
assertEquals(stringItems.getPattern(), TEST_PATTERN_REGXP);
//Fourth param - items specified
HeaderParameter headerParam4 = new HeaderParameter().type(StringProperty.TYPE);
HeaderParameter param4 = (HeaderParameter) ParameterProcessor.applyAnnotations(null, headerParam4, genericParameterTypes[3], Arrays.asList(paramAnnotations[3]));
assertNotNull(param4);
assertEquals(param4.getType(), ArrayProperty.TYPE);
assertEquals((int) param4.getMinItems(), 5);
assertEquals((int) param4.getMaxItems(), 10);
Property items4 = param4.getItems();
assertTrue(items4 instanceof StringProperty);
}
use of io.swagger.models.properties.DoubleProperty in project swagger-core by swagger-api.
the class BeanValidatorTest method readBeanValidatorTest.
@Test(description = "read bean validations")
public void readBeanValidatorTest() {
final Map<String, Model> schemas = ModelConverters.getInstance().readAll(BeanValidationsModel.class);
final Model model = schemas.get("BeanValidationsModel");
final Map<String, Property> properties = model.getProperties();
final IntegerProperty age = (IntegerProperty) properties.get("age");
Assert.assertEquals(age.getMinimum(), new BigDecimal(13.0));
Assert.assertEquals(age.getMaximum(), new BigDecimal(99.0));
final StringProperty password = (StringProperty) properties.get("password");
Assert.assertEquals((int) password.getMinLength(), 6);
Assert.assertEquals((int) password.getMaxLength(), 20);
final StringProperty email = (StringProperty) properties.get("email");
Assert.assertEquals((String) email.getPattern(), "(.+?)@(.+?)");
final DoubleProperty minBalance = (DoubleProperty) properties.get("minBalance");
Assert.assertTrue(minBalance.getExclusiveMinimum());
final DoubleProperty maxBalance = (DoubleProperty) properties.get("maxBalance");
Assert.assertTrue(maxBalance.getExclusiveMaximum());
final ArrayProperty items = (ArrayProperty) properties.get("items");
Assert.assertEquals((int) items.getMinItems(), 2);
Assert.assertEquals((int) items.getMaxItems(), 10);
}
Aggregations