Java Code Examples for io.swagger.models.parameters.BodyParameter#setSchema()

The following examples show how to use io.swagger.models.parameters.BodyParameter#setSchema() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcReaderExtension.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private Parameter readParam(Swagger swagger, Type type, Class<?> cls, ApiParam param) {
    PrimitiveType fromType = PrimitiveType.fromType(type);
    final Parameter para = null == fromType ? new BodyParameter() : new QueryParameter();
    Parameter parameter = ParameterProcessor.applyAnnotations(swagger, para, type,
        null == param ? new ArrayList<Annotation>()
            : Collections.<Annotation> singletonList(param));
    if (parameter instanceof AbstractSerializableParameter) {
        final AbstractSerializableParameter<?> p = (AbstractSerializableParameter<?>) parameter;
        if (p.getType() == null) {
            p.setType(null == fromType ? "string" : fromType.getCommonName());
        }
        p.setRequired(p.getRequired() || cls.isPrimitive());
    } else {
        //hack: Get the from data model paramter from BodyParameter
        BodyParameter bp = (BodyParameter) parameter;
        bp.setIn("body");
        Property property = ModelConverters.getInstance().readAsProperty(type);
        final Map<PropertyBuilder.PropertyId, Object> args = new EnumMap<PropertyBuilder.PropertyId, Object>(
            PropertyBuilder.PropertyId.class);
        bp.setSchema(PropertyBuilder.toModel(PropertyBuilder.merge(property, args)));
    }
    return parameter;
}
 
Example 2
Source File: AbstractOperationGenerator.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private void mergeBodyParameter(BodyParameter bodyParameter, BodyParameter fromBodyParameter) {
  if (fromBodyParameter.getExamples() != null) {
    bodyParameter.setExamples(fromBodyParameter.getExamples());
  }
  if (fromBodyParameter.getRequired()) {
    bodyParameter.setRequired(true);
  }
  if (StringUtils.isNotEmpty(fromBodyParameter.getDescription())) {
    bodyParameter.setDescription(fromBodyParameter.getDescription());
  }
  if (StringUtils.isNotEmpty(fromBodyParameter.getAccess())) {
    bodyParameter.setAccess(fromBodyParameter.getAccess());
  }
  if (fromBodyParameter.getSchema() != null) {
    bodyParameter.setSchema(fromBodyParameter.getSchema());
  }
}
 
Example 3
Source File: JaxrsReader.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
private Parameter replaceArrayModelForOctetStream(Operation operation, Parameter parameter) {
    if (parameter instanceof BodyParameter
            && operation.getConsumes() != null
            && operation.getConsumes().contains("application/octet-stream")) {
        BodyParameter bodyParam = (BodyParameter) parameter;
        Model schema = bodyParam.getSchema();
        if (schema instanceof ArrayModel) {
            ArrayModel arrayModel = (ArrayModel) schema;
            Property items = arrayModel.getItems();
            if (items != null && items.getFormat() == "byte" && items.getType() == "string") {
                ModelImpl model = new ModelImpl();
                model.setFormat("byte");
                model.setType("string");
                bodyParam.setSchema(model);
            }
        }
    }
    return parameter;
}
 
Example 4
Source File: OperationsTransformer.java    From spring-openapi with MIT License 5 votes vote down vote up
private io.swagger.models.parameters.Parameter createStandardRequestBody(Method method, ParameterNamePair requestBodyParameter, ModelImpl content,
																		 Property property) {
	BodyParameter requestBody = new BodyParameter();
	requestBody.setSchema(content);
	requestBody.setRequired(true);
	requestBody.setName(resolveRequestBodyName(property, requestBodyParameter.getName()));
	requestBody.setDescription(requestBody.getName());

	requestBodyInterceptors.forEach(interceptor ->
											interceptor.intercept(method, requestBodyParameter.getParameter(), requestBodyParameter.getName(), requestBody)
	);

	return requestBody;
}
 
Example 5
Source File: ReplaceDefinitionsProcessor.java    From yang2swagger with Eclipse Public License 1.0 5 votes vote down vote up
private void fixParameter(Parameter p, Map<String, String> replacements) {
    if(!(p instanceof BodyParameter)) return;
    BodyParameter bp = (BodyParameter) p;
    if(!(bp.getSchema() instanceof RefModel)) return;
    RefModel ref = (RefModel) bp.getSchema();
    if(replacements.containsKey(ref.getSimpleRef())) {
        String replacement = replacements.get(ref.getSimpleRef());
        bp.setDescription(bp.getDescription().replace(ref.getSimpleRef(), replacement));
        bp.setSchema(new RefModel(replacement));
    }

}
 
Example 6
Source File: SwaggerUtils.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * This method will generate a sample request body for the request.
 *
 * @param method        Rest resource method.
 * @param operation     Swagger operation object.
 * @param parameterList list of parameters.
 */
private static void generateSampleRequestPayload(String method, Operation operation,
                                                 List<AxisResourceParameter> parameterList) {
    // GET method does not have a body
    if (!"GET".equals(method)) {
        BodyParameter bodyParameter = new BodyParameter();
        bodyParameter.description("Sample Payload");
        bodyParameter.name("payload");
        bodyParameter.setRequired(false);

        ModelImpl modelschema = new ModelImpl();
        modelschema.setType("object");
        Map<String, Property> propertyMap = new HashMap<>(1);
        ObjectProperty objectProperty = new ObjectProperty();
        objectProperty.name("payload");

        Map<String, Property> payloadProperties = new HashMap<>();
        for (AxisResourceParameter resourceParameter : parameterList) {
            switch (resourceParameter.getParameterDataType()) {
                case SwaggerProcessorConstants.INTEGER:
                    payloadProperties.put(resourceParameter.getParameterName(), new IntegerProperty());
                    break;
                case SwaggerProcessorConstants.NUMBER:
                    payloadProperties.put(resourceParameter.getParameterName(), new DoubleProperty());
                    break;
                case SwaggerProcessorConstants.BOOLEAN:
                    payloadProperties.put(resourceParameter.getParameterName(), new BooleanProperty());
                    break;
                default:
                    payloadProperties.put(resourceParameter.getParameterName(), new StringProperty());
                    break;
            }
        }

        objectProperty.setProperties(payloadProperties);
        propertyMap.put("payload", objectProperty);
        modelschema.setProperties(propertyMap);
        bodyParameter.setSchema(modelschema);
        operation.addParameter(bodyParameter);
    }
}
 
Example 7
Source File: PojoOperationGenerator.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
private void wrapParametersToBody(List<ParameterGenerator> bodyFields) {
  String simpleRef = MethodUtils.findSwaggerMethodName(method) + "Body";

  bodyModel = new ModelImpl();
  bodyModel.setType(ModelImpl.OBJECT);
  for (ParameterGenerator parameterGenerator : bodyFields) {
    // to collect all information by swagger mechanism
    // must have a parameter type
    // but all these parameters will be wrap to be one body parameter, their parameter type must be null
    // so we first set to be BODY, after collected, set back to be null
    parameterGenerator.setHttpParameterType(HttpParameterType.BODY);
    scanMethodParameter(parameterGenerator);

    Property property = ModelConverters.getInstance().readAsProperty(parameterGenerator.getGenericType());
    property.setDescription(parameterGenerator.getGeneratedParameter().getDescription());
    bodyModel.addProperty(parameterGenerator.getParameterName(), property);

    parameterGenerator.setHttpParameterType(null);
  }
  swagger.addDefinition(simpleRef, bodyModel);

  SwaggerGeneratorFeature feature = swaggerGenerator.getSwaggerGeneratorFeature();
  // bodyFields.size() > 1 is no reason, just because old version do this......
  // if not care for this, then can just delete all logic about EXT_JAVA_CLASS/EXT_JAVA_INTF
  if (feature.isExtJavaClassInVendor()
      && bodyFields.size() > 1
      && StringUtils.isNotEmpty(feature.getPackageName())) {
    bodyModel.getVendorExtensions().put(SwaggerConst.EXT_JAVA_CLASS, feature.getPackageName() + "." + simpleRef);
  }

  RefModel refModel = new RefModel();
  refModel.setReference("#/definitions/" + simpleRef);

  bodyParameter = new BodyParameter();
  bodyParameter.name(simpleRef);
  bodyParameter.setSchema(refModel);
  bodyParameter.setName(parameterGenerators.size() == 1 ? parameterGenerators.get(0).getParameterName() : simpleRef);

  List<ParameterGenerator> newParameterGenerators = new ArrayList<>();
  newParameterGenerators.add(new ParameterGenerator(
      bodyParameter.getName(),
      Collections.emptyList(),
      null,
      HttpParameterType.BODY,
      bodyParameter));
  parameterGenerators.stream().filter(p -> p.getHttpParameterType() != null)
      .forEach(p -> newParameterGenerators.add(p));
  parameterGenerators = newParameterGenerators;
}
 
Example 8
Source File: PayloadWrapperProcessor.java    From yang2swagger with Eclipse Public License 1.0 4 votes vote down vote up
private void wrap(String propertyName, BodyParameter param) {
    RefModel m = (RefModel) param.getSchema();
    String wrapperName = wrap(propertyName, m.getSimpleRef());
    param.setSchema(new RefModel(wrapperName));
}