public static Type typeFromString(String type) { final PrimitiveType primitive = PrimitiveType.fromName(type); if (primitive != null) { return primitive.getKeyClass(); } try { return Class.forName(type); } catch (Exception e) { // LOGGER.error(String.format("Failed to resolve '%s' into class", type), e); } return null; }
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; }
private Parameter readImplicitParam(Swagger swagger, ApiImplicitParam param) { PrimitiveType fromType = PrimitiveType.fromName(param.paramType()); final Parameter p = null == fromType ? new FormParameter() : new QueryParameter(); final Type type = ReflectionUtils.typeFromString(param.dataType()); return ParameterProcessor.applyAnnotations(swagger, p, type == null ? String.class : type, Collections.<Annotation> singletonList(param)); }
private Object suggestPrameterValue(Type type, Class<?> cls, String parameter) throws JsonParseException, JsonMappingException, IOException { PrimitiveType fromType = PrimitiveType.fromType(type); if (null != fromType) { DefaultConversionService service = new DefaultConversionService(); boolean actual = service.canConvert(String.class, cls); if (actual) { return service.convert(parameter, cls); } } else { if (null == parameter) return null; try { return Json.mapper().readValue(parameter, cls); } catch (Exception e) { throw new IllegalArgumentException("The parameter value [" + parameter + "] should be json of [" + cls.getName() + "] Type.", e); } } try { return Class.forName(cls.getName()).newInstance(); } catch (Exception e) { e.printStackTrace(); } return null; }