use of io.swagger.models.parameters.BodyParameter in project java-chassis by ServiceComb.
the class PendingBodyParameter method createBodyParameter.
public BodyParameter createBodyParameter(String paramName) {
String modelType = ParamUtils.generateBodyParameterName(operationGenerator.getProviderMethod());
RefModel model = toRefModel(modelType);
BodyParameter bodyParameter = new BodyParameter();
bodyParameter.setName(paramName);
bodyParameter.setSchema(model);
return bodyParameter;
}
use of io.swagger.models.parameters.BodyParameter in project java-chassis by ServiceComb.
the class OperationGenerator method replaceBodyBasedParameter.
protected void replaceBodyBasedParameter(Parameter bodyBasedParameter) {
if (ParamUtils.isRealBodyParameter(bodyBasedParameter)) {
return;
}
int idx = swaggerParameters.indexOf(bodyBasedParameter);
String bodyParamName = bodyBasedParameter.getName();
BodyParameter bodyParameter = ((PendingBodyParameter) bodyBasedParameter).createBodyParameter(bodyParamName);
swaggerParameters.set(idx, bodyParameter);
}
use of io.swagger.models.parameters.BodyParameter 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;
}
use of io.swagger.models.parameters.BodyParameter in project swagger-core by swagger-api.
the class ParameterProcessorTest method implicitParameterProcessorTest.
@Test(description = "parse implicit parameters from method")
public void implicitParameterProcessorTest() throws NoSuchMethodException {
final ApiImplicitParams params = getClass().getDeclaredMethod("implicitParametrizedMethod").getAnnotation(ApiImplicitParams.class);
final PathParameter param0 = (PathParameter) ParameterProcessor.applyAnnotations(null, new PathParameter(), String.class, Collections.<Annotation>singletonList(params.value()[0]));
assertNotNull(param0);
assertEquals(param0.getIn(), "path");
assertEquals(param0.getName(), "paramName1");
assertEquals(param0.getDescription(), "paramValue1");
assertNull(param0.getEnum());
assertNotNull(param0.getItems());
final BodyParameter param1 = (BodyParameter) ParameterProcessor.applyAnnotations(null, new BodyParameter(), String.class, Collections.<Annotation>singletonList(params.value()[1]));
assertNotNull(param1);
assertEquals(param1.getIn(), "body");
assertEquals(param1.getName(), "body");
assertEquals(param1.getDescription(), "paramValue2");
assertEquals(param1.getAccess(), "test");
final ModelImpl model = (ModelImpl) param1.getSchema();
assertNotNull(model);
assertEquals(model.getDefaultValue(), "10");
}
use of io.swagger.models.parameters.BodyParameter in project swagger-core by swagger-api.
the class ParameterSerializationTest method deserializeRefBodyParameter.
@Test(description = "it should deserialize a ref BodyParameter")
public void deserializeRefBodyParameter() throws IOException {
final String json = "{\"in\":\"body\",\"required\":false,\"schema\":{\"$ref\":\"#/definitions/Cat\"}}";
final Parameter p = m.readValue(json, Parameter.class);
SerializationMatchers.assertEqualsToJson(p, json);
}
Aggregations