Search in sources :

Example 26 with BodyParameter

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;
}
Also used : RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 27 with 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);
}
Also used : PendingBodyParameter(io.servicecomb.swagger.extend.parameter.PendingBodyParameter) PendingBodyParameter(io.servicecomb.swagger.extend.parameter.PendingBodyParameter) BodyParameter(io.swagger.models.parameters.BodyParameter)

Example 28 with 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;
}
Also used : ExampleProperty(io.swagger.annotations.ExampleProperty) BodyParameter(io.swagger.models.parameters.BodyParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) FileProperty(io.swagger.models.properties.FileProperty) LongProperty(io.swagger.models.properties.LongProperty) Example(io.swagger.annotations.Example) Model(io.swagger.models.Model) EnumMap(java.util.EnumMap) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) ExampleProperty(io.swagger.annotations.ExampleProperty) FileProperty(io.swagger.models.properties.FileProperty) Map(java.util.Map) EnumMap(java.util.EnumMap) PropertyBuilder(io.swagger.models.properties.PropertyBuilder)

Example 29 with BodyParameter

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");
}
Also used : ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) BodyParameter(io.swagger.models.parameters.BodyParameter) ModelImpl(io.swagger.models.ModelImpl) PathParameter(io.swagger.models.parameters.PathParameter) Annotation(java.lang.annotation.Annotation) Test(org.testng.annotations.Test)

Example 30 with BodyParameter

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);
}
Also used : BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) Test(org.testng.annotations.Test)

Aggregations

BodyParameter (io.swagger.models.parameters.BodyParameter)45 Test (org.testng.annotations.Test)34 Operation (io.swagger.models.Operation)20 Parameter (io.swagger.models.parameters.Parameter)16 ArrayModel (io.swagger.models.ArrayModel)15 Model (io.swagger.models.Model)14 PathParameter (io.swagger.models.parameters.PathParameter)14 Path (io.swagger.models.Path)13 QueryParameter (io.swagger.models.parameters.QueryParameter)13 StringProperty (io.swagger.models.properties.StringProperty)12 Swagger (io.swagger.models.Swagger)11 RefProperty (io.swagger.models.properties.RefProperty)11 Property (io.swagger.models.properties.Property)10 RefModel (io.swagger.models.RefModel)7 HeaderParameter (io.swagger.models.parameters.HeaderParameter)7 ModelImpl (io.swagger.models.ModelImpl)6 SerializableParameter (io.swagger.models.parameters.SerializableParameter)6 ArrayProperty (io.swagger.models.properties.ArrayProperty)5 PendingBodyParameter (io.servicecomb.swagger.extend.parameter.PendingBodyParameter)4 Response (io.swagger.models.Response)4