io.swagger.models.parameters.AbstractSerializableParameter Java Examples

The following examples show how to use io.swagger.models.parameters.AbstractSerializableParameter. 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: OperationsTransformer.java    From spring-openapi with MIT License 6 votes vote down vote up
private void fillParameterInfo(AbstractSerializableParameter<?> oasParameter, Parameter parameter, String parameterName) {
	Class<?> clazz = parameter.getType();
	Annotation[] annotations = parameter.getAnnotations();

	if (clazz.isPrimitive()) {
		setParameterDetails(oasParameter, clazz, annotations);
	} else if (clazz.isArray()) {
		oasParameter.setProperty(parseArraySignatureForParameter(clazz.getComponentType(), annotations));
	} else if (clazz.isAssignableFrom(List.class)) {
		if (!(parameter.getParameterizedType() instanceof ParameterizedType)) {
			throw new IllegalArgumentException(String.format("List [%s] not being parametrized type.", parameterName));
		}
		Class<?> listGenericParameter = (Class<?>) ((ParameterizedType) parameter.getParameterizedType()).getActualTypeArguments()[0];
		oasParameter.setProperty(parseArraySignature(listGenericParameter, null, annotations));
	} else {
		setParameterDetails(oasParameter, clazz, annotations);
	}
}
 
Example #2
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 #3
Source File: DubboReaderExtension.java    From swagger-dubbo 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 ? String.class : 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() == true ? true : cls.isPrimitive());
	}else{
	    //hack: Get the from data model paramter from BodyParameter
	    BodyParameter bp = (BodyParameter)parameter;
	    bp.setIn("formData");
	}
	return parameter;
}
 
Example #4
Source File: JaxrsOperationGenerator.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
protected void fillParameter(Swagger swagger, io.swagger.models.parameters.Parameter parameter, String parameterName,
    Type type, List<Annotation> annotations) {
  super.fillParameter(swagger, parameter, parameterName, type, annotations);

  if (!(parameter instanceof AbstractSerializableParameter)) {
    return;
  }

  if (!(type instanceof Class && ((Class) type).isPrimitive())) {
    return;
  }

  AbstractSerializableParameter<?> serializableParameter = (AbstractSerializableParameter<?>) parameter;
  if (serializableParameter.getDefault() == null && !parameter.getRequired()) {
    serializableParameter.setDefaultValue(String.valueOf(Defaults.defaultValue((Class<?>) type)));
  }
}
 
Example #5
Source File: ParameterProcessor.java    From netty-rest with Apache License 2.0 6 votes vote down vote up
private static void processAllowedValues(AllowableValues allowableValues, AbstractSerializableParameter<?> p) {
    if (allowableValues == null) {
        return;
    }
    Map<PropertyBuilder.PropertyId, Object> args = allowableValues.asPropertyArguments();
    if (args.containsKey(PropertyBuilder.PropertyId.ENUM)) {
        p.setEnum((List<String>) args.get(PropertyBuilder.PropertyId.ENUM));
    } else {
        if (args.containsKey(PropertyBuilder.PropertyId.MINIMUM)) {
            p.setMinimum((BigDecimal) args.get(PropertyBuilder.PropertyId.MINIMUM));
        }
        if (args.containsKey(PropertyBuilder.PropertyId.MAXIMUM)) {
            p.setMaximum((BigDecimal) args.get(PropertyBuilder.PropertyId.MAXIMUM));
        }
        if (args.containsKey(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM)) {
            p.setExclusiveMinimum((Boolean) args.get(PropertyBuilder.PropertyId.EXCLUSIVE_MINIMUM) ? true : null);
        }
        if (args.containsKey(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM)) {
            p.setExclusiveMaximum((Boolean) args.get(PropertyBuilder.PropertyId.EXCLUSIVE_MAXIMUM) ? true : null);
        }
    }
}
 
Example #6
Source File: JaxrsParameterExtensionTest.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterDefaultValue() {
    List<Parameter> parameters = new JaxrsParameterExtension().extractParameters(
            Lists.newArrayList(
                    getTestAnnotation("getWithDefault", QueryParam.class),
                    getTestAnnotation("getWithDefault", DefaultValue.class)
            ),
            String.class,
            Sets.<Type>newHashSet(),
            Lists.<SwaggerExtension>newArrayList().iterator());

    assertFalse(parameters.isEmpty());
    assertEquals(parameters.size(), 1);

    Parameter extracted = parameters.get(0);
    assertEquals(((AbstractSerializableParameter)extracted).getDefaultValue(), "en-US");
}
 
Example #7
Source File: OpenApiTransformer.java    From spring-openapi with MIT License 5 votes vote down vote up
protected void setParameterDetails(AbstractSerializableParameter<?> oasParameter, Class<?> type, Annotation[] annotations) {
	if (byte.class.equals(type) || short.class.equals(type) || int.class.equals(type) || Byte.class.equals(type) ||
		Short.class.equals(type) || Integer.class.equals(type)) {
		oasParameter.setProperty(new IntegerProperty());
	} else if (long.class.equals(type) || Long.class.equals(type) || BigInteger.class.equals(type)) {
		oasParameter.setProperty(new LongProperty());
	} else if (float.class.equals(type) || Float.class.equals(type)) {
		oasParameter.setProperty(new FloatProperty().vendorExtension("x-type", "System.BigDecimal"));
	} else if (double.class.equals(type) || Double.class.equals(type) || BigDecimal.class.equals(type)) {
		oasParameter.setProperty(new DoubleProperty().vendorExtension("x-type", "System.BigDecimal"));
	} else if (char.class.equals(type) || Character.class.equals(type) || String.class.equals(type)) {
		oasParameter.setProperty(new StringProperty());
	} else if (boolean.class.equals(type) || Boolean.class.equals(type)) {
		oasParameter.setProperty(new BooleanProperty());
	} else if (List.class.equals(type)) {
		oasParameter.setProperty(createArrayProperty(type, null, annotations));
	} else if (LocalDate.class.equals(type) || Date.class.equals(type)) {
		oasParameter.setProperty(new DateProperty());
	} else if (LocalDateTime.class.equals(type) || LocalTime.class.equals(type)) {
		oasParameter.setProperty(new DateTimeProperty());
	}  else if (type.isEnum()) {
		mapEnum(oasParameter, type);
	} else {
		oasParameter.setProperty(createRefProperty(type, null));
	}
	asList(annotations).forEach(annotation -> applyAnnotationDetailsOnParameter(oasParameter, annotation));
}
 
Example #8
Source File: OpenApiTransformer.java    From spring-openapi with MIT License 5 votes vote down vote up
private void mapEnum(AbstractSerializableParameter<?> oasParameter, Class<?> type) {
	if (isNswagCompatibilityMode()) {
		oasParameter.setType(null);
		setRef(oasParameter, type);
	} else {
		oasParameter.setType("string");
		oasParameter.setEnumValue(asList(type.getEnumConstants()));
	}
}
 
Example #9
Source File: OpenApiTransformer.java    From spring-openapi with MIT License 5 votes vote down vote up
protected void applyAnnotationDetailsOnParameter(AbstractSerializableParameter schema, Annotation annotation) {
	if (annotation instanceof DecimalMin) {
		schema.setMinimum(new BigDecimal(((DecimalMin) annotation).value()));
	} else if (annotation instanceof DecimalMax) {
		schema.setMaximum(new BigDecimal(((DecimalMax) annotation).value()));
	} else if (annotation instanceof Min) {
		schema.setMinimum(BigDecimal.valueOf(((Min) annotation).value()));
	} else if (annotation instanceof Max) {
		schema.setMaximum(BigDecimal.valueOf(((Max) annotation).value()));
	}
	if (annotation instanceof Pattern) {
		schema.setPattern(((Pattern) annotation).regexp());
	} else if (annotation instanceof Size) {
		schema.setMinLength(((Size) annotation).min());
		schema.setMaxLength(((Size) annotation).max());
	} else if (annotation instanceof Deprecated) {
		schema.setVendorExtension("x-deprecated", true);
	}
}
 
Example #10
Source File: SpringmvcOperationGenerator.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
protected void initMethodParameterGenerators(Map<String, List<Annotation>> methodAnnotationMap) {
  super.initMethodParameterGenerators(methodAnnotationMap);

  parameterGenerators.stream()
      .filter(pg -> pg.getHttpParameterType() == null)
      .forEach(pg -> {
        pg.setHttpParameterType(HttpParameterType.QUERY);
        pg.setGeneratedParameter(createParameter(HttpParameterType.QUERY));
        SwaggerUtils.setParameterType(swagger,
            pg.getGenericType(),
            (AbstractSerializableParameter<?>) pg.getGeneratedParameter());
      });
}
 
Example #11
Source File: SwaggerUtils.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public static void setParameterType(Swagger swagger, Type type, AbstractSerializableParameter<?> parameter) {
  addDefinitions(swagger, type);
  Property property = ModelConverters.getInstance().readAsProperty(type);

  if (isComplexProperty(property)) {
    // cannot set a simple parameter(header, query, etc.) as complex type
    String msg = String
        .format("not allow complex type for %s parameter, type=%s.", parameter.getIn(), type.getTypeName());
    throw new IllegalStateException(msg);
  }
  parameter.setProperty(property);
}
 
Example #12
Source File: AbstractSerializableParameterExtractor.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
public Object extract(String name, Parameter parameter, MultiMap params) {
    AbstractSerializableParameter abstractSerializableParameter = (AbstractSerializableParameter) parameter;
    if (!params.contains(name)) {
        if (abstractSerializableParameter.getRequired()) {
            throw new IllegalArgumentException("Missing required parameter: " + name);
        } else if (abstractSerializableParameter.getDefaultValue()!=null){
            return abstractSerializableParameter.getDefaultValue();
        } else {
            return null;
        }
    }

    if ((abstractSerializableParameter.getAllowEmptyValue() == null
            || !abstractSerializableParameter.getAllowEmptyValue())
            && StringUtils.isEmpty(params.get(name))) {
        throw new IllegalArgumentException(
                "Empty value is not authorized for parameter: " + name);
    }

    if ("array".equals(abstractSerializableParameter.getType())) {
        if ("multi".equals(abstractSerializableParameter.getCollectionFormat())) {
            return params.getAll(name);
        } else {
            List<String> resultParams = this.splitArrayParam(abstractSerializableParameter,
                    params.get(name));
            if (resultParams != null) {
                return resultParams;
            }
        }
    }

    return params.get(name);
}
 
Example #13
Source File: OpenApiTransformer.java    From spring-openapi with MIT License 4 votes vote down vote up
private void setRef(AbstractSerializableParameter<?> oasParameter, Class<?> type) {
	if (oasParameter instanceof CustomQueryParameter) {
		((CustomQueryParameter) oasParameter).setRef(CommonConstants.COMPONENT_REF_PREFIX + type.getSimpleName());
	}
}