Java Code Examples for io.swagger.models.properties.StringProperty

The following examples show how to use io.swagger.models.properties.StringProperty. These examples are extracted from open source projects. 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
public static List<String> extractEnum(Parameter p) {
    List<String> enumValues = null;

    if (p instanceof SerializableParameter) {
        SerializableParameter serializableParameter = (SerializableParameter) p;
        if (serializableParameter.getEnum() != null && !serializableParameter.getEnum().isEmpty()) {
            enumValues = serializableParameter.getEnum();
        }

        if (enumValues == null && serializableParameter.getItems() != null) {
            Property items = serializableParameter.getItems();
            if (items instanceof StringProperty) {
                StringProperty stringProperty = (StringProperty) items;
                if (stringProperty.getEnum() != null && !stringProperty.getEnum().isEmpty()) {
                    enumValues = stringProperty.getEnum();
                }
            }
        }
    }

    return enumValues;
}
 
Example 2
Source Project: servicecomb-java-chassis   Source File: ConverterMgr.java    License: Apache License 2.0 6 votes vote down vote up
private static void initPropertyMap() {
  PROPERTY_MAP.put(BooleanProperty.class, TypeFactory.defaultInstance().constructType(Boolean.class));

  PROPERTY_MAP.put(FloatProperty.class, TypeFactory.defaultInstance().constructType(Float.class));
  PROPERTY_MAP.put(DoubleProperty.class, TypeFactory.defaultInstance().constructType(Double.class));
  PROPERTY_MAP.put(DecimalProperty.class, TypeFactory.defaultInstance().constructType(BigDecimal.class));

  PROPERTY_MAP.put(ByteProperty.class, TypeFactory.defaultInstance().constructType(Byte.class));
  PROPERTY_MAP.put(ShortProperty.class, TypeFactory.defaultInstance().constructType(Short.class));
  PROPERTY_MAP.put(IntegerProperty.class, TypeFactory.defaultInstance().constructType(Integer.class));
  PROPERTY_MAP.put(BaseIntegerProperty.class, TypeFactory.defaultInstance().constructType(Integer.class));
  PROPERTY_MAP.put(LongProperty.class, TypeFactory.defaultInstance().constructType(Long.class));

  // stringProperty include enum scenes, not always be string type
  // if convert by StringPropertyConverter, can support enum scenes
  PROPERTY_MAP.put(StringProperty.class, TypeFactory.defaultInstance().constructType(String.class));

  PROPERTY_MAP.put(DateProperty.class, TypeFactory.defaultInstance().constructType(LocalDate.class));
  PROPERTY_MAP.put(DateTimeProperty.class, TypeFactory.defaultInstance().constructType(Date.class));

  PROPERTY_MAP.put(ByteArrayProperty.class, TypeFactory.defaultInstance().constructType(byte[].class));

  PROPERTY_MAP.put(FileProperty.class, TypeFactory.defaultInstance().constructType(Part.class));
}
 
Example 3
Source Project: servicecomb-java-chassis   Source File: ConverterMgr.java    License: Apache License 2.0 6 votes vote down vote up
private static void initConverters() {
  // inner converters
  for (Class<? extends Property> propertyCls : PROPERTY_MAP.keySet()) {
    addInnerConverter(propertyCls);
  }

  converterMap.put(RefProperty.class, new RefPropertyConverter());
  converterMap.put(ArrayProperty.class, new ArrayPropertyConverter());
  converterMap.put(MapProperty.class, new MapPropertyConverter());
  converterMap.put(StringProperty.class, new StringPropertyConverter());
  converterMap.put(ObjectProperty.class, new ObjectPropertyConverter());

  converterMap.put(ModelImpl.class, new ModelImplConverter());
  converterMap.put(RefModel.class, new RefModelConverter());
  converterMap.put(ArrayModel.class, new ArrayModelConverter());
}
 
Example 4
protected void fillBodyParameter(Swagger swagger, Parameter parameter, Type type, List<Annotation> annotations) {
  // so strange, for bodyParameter, swagger return a new instance
  // that will cause lost some information
  // so we must merge them
  BodyParameter newBodyParameter = (BodyParameter) io.swagger.util.ParameterProcessor.applyAnnotations(
      swagger, parameter, type, annotations);

  // swagger missed enum data, fix it
  ModelImpl model = SwaggerUtils.getModelImpl(swagger, newBodyParameter);
  if (model != null) {
    Property property = ModelConverters.getInstance().readAsProperty(type);
    if (property instanceof StringProperty) {
      model.setEnum(((StringProperty) property).getEnum());
    }
  }

  mergeBodyParameter((BodyParameter) parameter, newBodyParameter);
}
 
Example 5
Source Project: servicecomb-java-chassis   Source File: ModelResolverExt.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Property resolveProperty(JavaType propType, ModelConverterContext context, Annotation[] annotations,
    Iterator<ModelConverter> next) {
  checkType(propType);

  PropertyCreator creator = propertyCreatorMap.get(propType.getRawClass());
  if (creator != null) {
    return creator.createProperty();
  }

  Property property = super.resolveProperty(propType, context, annotations, next);
  if (StringProperty.class.isInstance(property)) {
    if (StringPropertyConverter.isEnum((StringProperty) property)) {
      setType(propType, property.getVendorExtensions());
    }
  }
  return property;
}
 
Example 6
/**
 * EdgeService cannot recognize the map type form body whose value type is String,
 * so there should be this additional setting.
 * @param parameter the swagger information of the parameter
 * @param type the resolved param type
 * @return the corrected param type
 */
private Type correctFormBodyType(Parameter parameter, Type type) {
  if (null != type || !(parameter instanceof BodyParameter)) {
    return type;
  }
  final BodyParameter bodyParameter = (BodyParameter) parameter;
  if (!(bodyParameter.getSchema() instanceof ModelImpl)) {
    return type;
  }
  final Property additionalProperties = ((ModelImpl) bodyParameter.getSchema()).getAdditionalProperties();
  if (additionalProperties instanceof StringProperty) {
    type = RestObjectMapperFactory.getRestObjectMapper().getTypeFactory()
        .constructMapType(Map.class, String.class, String.class);
  }
  return type;
}
 
Example 7
Source Project: swagger2puml   Source File: PlantUMLCodegen.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param modelObject
 * @param models
 * @param variablName
 * @param classMemberObject
 * @param propObject
 */
private ClassMembers getClassMember(ArrayProperty property, Model modelObject, Map<String, Model> models,
		String variablName) {
	LOGGER.entering(LOGGER.getName(), "getClassMember-ArrayProperty");

	ClassMembers classMemberObject = new ClassMembers();
	Property propObject = property.getItems();

	if (propObject instanceof RefProperty) {
		classMemberObject = getClassMember((RefProperty) propObject, models, modelObject, variablName);
	} else if (propObject instanceof StringProperty) {
		classMemberObject = getClassMember((StringProperty) propObject, variablName);
	}

	LOGGER.exiting(LOGGER.getName(), "getClassMember-ArrayProperty");
	return classMemberObject;
}
 
Example 8
Source Project: endpoints-java   Source File: SwaggerGenerator.java    License: Apache License 2.0 6 votes vote down vote up
private static Property getSwaggerArrayProperty(TypeToken<?> typeToken) {
  Class<?> type = typeToken.getRawType();
  if (type == String.class) {
    return new StringProperty();
  } else if (type == Boolean.class || type == Boolean.TYPE) {
    return new BooleanProperty();
  } else if (type == Integer.class || type == Integer.TYPE) {
    return new IntegerProperty();
  } else if (type == Long.class || type == Long.TYPE) {
    return new LongProperty();
  } else if (type == Float.class || type == Float.TYPE) {
    return new FloatProperty();
  } else if (type == Double.class || type == Double.TYPE) {
    return new DoubleProperty();
  } else if (type == byte[].class) {
    return new ByteArrayProperty();
  } else if (type.isEnum()) {
    return new StringProperty();
  }
  throw new IllegalArgumentException("invalid property type");
}
 
Example 9
Source Project: swagger2markup   Source File: PropertyAdapter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the default value of a property
 *
 * @return the default value of the property
 */
public Optional<Object> getDefaultValue() {
    if (property instanceof BooleanProperty) {
        BooleanProperty booleanProperty = (BooleanProperty) property;
        return Optional.ofNullable(booleanProperty.getDefault());
    } else if (property instanceof StringProperty) {
        StringProperty stringProperty = (StringProperty) property;
        return Optional.ofNullable(stringProperty.getDefault());
    } else if (property instanceof DoubleProperty) {
        DoubleProperty doubleProperty = (DoubleProperty) property;
        return Optional.ofNullable(doubleProperty.getDefault());
    } else if (property instanceof FloatProperty) {
        FloatProperty floatProperty = (FloatProperty) property;
        return Optional.ofNullable(floatProperty.getDefault());
    } else if (property instanceof IntegerProperty) {
        IntegerProperty integerProperty = (IntegerProperty) property;
        return Optional.ofNullable(integerProperty.getDefault());
    } else if (property instanceof LongProperty) {
        LongProperty longProperty = (LongProperty) property;
        return Optional.ofNullable(longProperty.getDefault());
    } else if (property instanceof UUIDProperty) {
        UUIDProperty uuidProperty = (UUIDProperty) property;
        return Optional.ofNullable(uuidProperty.getDefault());
    }
    return Optional.empty();
}
 
Example 10
Source Project: assertj-swagger   Source File: PropertyValidator.java    License: Apache License 2.0 6 votes vote down vote up
void validateProperty(Property actualProperty, Property expectedProperty, String message) {
    if (expectedProperty == null || !isAssertionEnabled(SwaggerAssertionType.PROPERTIES)) {
        return;
    }

    // TODO Validate Property schema
    if (shouldValidateRefProperty(expectedProperty)) {
        validateBasicPropertyFeatures(actualProperty, expectedProperty, message);
        // TODO improve validation by verifying property based on RefProperty type
    } else if (shouldValidateArrayProperty(expectedProperty)) {
        validateBasicPropertyFeatures(actualProperty, expectedProperty, message);
        // TODO improve validation by verifying property based on ArrayProperty type
    } else if (shouldValidateByteArrayProperty(expectedProperty)) {
        validateBasicPropertyFeatures(actualProperty, expectedProperty, message);
        // TODO improve validation by verifying property based on ByteArrayProperty type
    } else if (shouldValidateStringProperty(expectedProperty)) {
        StringProperty expectedStringProperty = (StringProperty) expectedProperty;
        validateBasicPropertyFeatures(actualProperty, expectedProperty, message);
        if (isPropertyOfEnumType(actualProperty)) {
            StringProperty actualStringProperty = (StringProperty) actualProperty;
            validateEnumPropertyFeatures(actualStringProperty, expectedStringProperty);
        }
    } else {
        validateBasicPropertyFeatures(actualProperty, expectedProperty, message);
    }
}
 
Example 11
private Property readAsPropertyIfPrimitive(Type type) {
    if (com.github.kongchen.swagger.docgen.util.TypeUtils.isPrimitive(type)) {
        return ModelConverters.getInstance().readAsProperty(type);
    } else {
        String msg = String.format("Non-primitive type: %s used as request/path/cookie parameter", type);
        log.warn(msg);

        // fallback to string if type is simple wrapper for String to support legacy code
        JavaType ct = constructType(type);
        if (isSimpleWrapperForString(ct.getRawClass())) {
            log.warn(String.format("Non-primitive type: %s used as string for request/path/cookie parameter", type));
            return new StringProperty();
        }
    }
    return null;
}
 
Example 12
Source Project: swagger-maven-plugin   Source File: JaxrsReader.java    License: Apache License 2.0 6 votes vote down vote up
private void handleJsonTypeInfo(Type responseClassType, Map<String, Model> modelMap) {
    if (responseClassType instanceof ParameterizedType) {
        Type[] actualTypes = ((ParameterizedType) responseClassType).getActualTypeArguments();
        for (Type type : actualTypes) {
            handleJsonTypeInfo(type, modelMap);
        }
    } else if (responseClassType instanceof Class<?>){
        Class<?> responseClass = ((Class<?>) responseClassType);
        if (responseClass.isArray()) {
            responseClass = responseClass.getComponentType();
        }

        JsonTypeInfo typeInfo = responseClass.getAnnotation(JsonTypeInfo.class);
        if (typeInfo == null || StringUtils.isEmpty(typeInfo.property()) || typeInfo.include().equals(As.EXISTING_PROPERTY)) {
            return;
        }

        Map<String, Property> properties = modelMap.get(responseClass.getSimpleName()).getProperties();
        if (properties != null && !properties.containsKey(typeInfo.property())) {
            properties.put(typeInfo.property(), new StringProperty());
        }
    }
}
 
Example 13
Source Project: spring-openapi   Source File: OpenApiTransformer.java    License: 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 14
Source Project: spring-openapi   Source File: OpenApiTransformer.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("squid:S1192") // better in-place defined for better readability
protected Property createStringProperty(String format, Annotation[] annotations) {
	StringProperty schema = new StringProperty();
	if (StringUtils.isNotBlank(format)) {
		schema.setFormat(format);
	}
	if (annotations != null) {
		asList(annotations).forEach(annotation -> applyStringAnnotationDetails(schema, annotation));
	}
	return schema;
}
 
Example 15
Source Project: spring-openapi   Source File: OpenApiTransformer.java    License: MIT License 5 votes vote down vote up
protected void applyStringAnnotationDetails(StringProperty schema, Annotation annotation) {
	if (annotation instanceof Pattern) {
		schema.pattern(((Pattern) annotation).regexp());
	} else if (annotation instanceof Size) {
		schema.minLength(((Size) annotation).min());
		schema.maxLength(((Size) annotation).max());
	} else if (annotation instanceof Deprecated) {
		schema.setVendorExtension("x-deprecated", true);
	}
}
 
Example 16
Source Project: spring-openapi   Source File: OpenApiTransformer.java    License: MIT License 5 votes vote down vote up
protected Property createProperty(Class<?> elementTypeSignature) {
	if (byte.class.equals(elementTypeSignature) || short.class.equals(elementTypeSignature) || int.class.equals(elementTypeSignature)
		|| long.class.equals(elementTypeSignature) || Byte.class.equals(elementTypeSignature) || Short.class.equals(elementTypeSignature)
		|| Integer.class.equals(elementTypeSignature) || Long.class.equals(elementTypeSignature) || BigInteger.class.equals(elementTypeSignature)) {
		return new IntegerProperty();
	} else if (float.class.equals(elementTypeSignature) || Float.class.equals(elementTypeSignature)) {
		return new FloatProperty().vendorExtension("x-type", "System.BigDecimal");
	} else if (double.class.equals(elementTypeSignature) || Double.class.equals(elementTypeSignature) || BigDecimal.class.equals(elementTypeSignature)) {
		return new DoubleProperty().vendorExtension("x-type", "System.BigDecimal");
	} else if (List.class.equals(elementTypeSignature)) {
		throw new IllegalArgumentException("Nested List types are not supported"
										   + elementTypeSignature.getName()
		);
	} else {
		if (char.class.equals(elementTypeSignature) || Character.class.equals(elementTypeSignature) || String.class.equals(elementTypeSignature)
			|| LocalDate.class.equals(elementTypeSignature) || Date.class.equals(elementTypeSignature)
			|| LocalDateTime.class.equals(elementTypeSignature) || LocalTime.class.equals(elementTypeSignature)) {
			return new StringProperty();
		} else if (elementTypeSignature.isEnum()) {
			StringProperty property = new StringProperty();
			property.setEnum(Arrays.stream(elementTypeSignature.getEnumConstants()).map(Object::toString).collect(toList()));
			return property;
		} else if (boolean.class.equals(elementTypeSignature) || Boolean.class.equals(elementTypeSignature)) {
			return new BooleanProperty();
		}
	}
	return null;
}
 
Example 17
Source Project: spring-openapi   Source File: OperationsTransformer.java    License: MIT License 5 votes vote down vote up
private Map<String, Property> createHeaderResponse(com.github.jrcodeza.Header[] headers) {
	Map<String, Property> responseHeaders = new HashMap<>();
	for (com.github.jrcodeza.Header headerAnnotation : headers) {
		StringProperty header = new StringProperty();
		header.setDescription(headerAnnotation.description());
		responseHeaders.put(headerAnnotation.name(), header);
	}
	return responseHeaders;
}
 
Example 18
Source Project: yshopmall   Source File: SwaggerUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取Swagger支持的类型
 *
 * @return {@link Map<String,  AbstractProperty >}
 */
private static Map<String, AbstractProperty> getPropMap() {
    Map<String, AbstractProperty> map = new HashMap<>(8);
    map.put("integer", new IntegerProperty());
    map.put("int", new IntegerProperty());
    map.put("long", new LongProperty());
    map.put("string", new StringProperty());
    map.put("object", new ObjectProperty());
    map.put("array", new ArrayProperty());
    map.put("boolean", new BooleanProperty());
    map.put("date", new DateTimeProperty());
    return map;
}
 
Example 19
Source Project: TypeScript-Microservices   Source File: NancyFXServerCodegen.java    License: MIT License 5 votes vote down vote up
private static Predicate<Property> timeProperty() {
    return new Predicate<Property>() {
        @Override
        public boolean apply(Property property) {
            return property instanceof StringProperty && "time".equalsIgnoreCase(property.getFormat());
        }
    };
}
 
Example 20
Source Project: TypeScript-Microservices   Source File: TizenClientCodegen.java    License: MIT License 5 votes vote down vote up
@Override
public String toDefaultValue(Property p) {
    if (p instanceof StringProperty) {
        return "std::string()";
    } else if (p instanceof BooleanProperty) {
        return "bool(false)";
    } else if (p instanceof DoubleProperty) {
        return "double(0)";
    } else if (p instanceof FloatProperty) {
        return "float(0)";
    } else if (p instanceof IntegerProperty) {
        return "int(0)";
    } else if (p instanceof LongProperty) {
        return "long(0)";
    } else if (p instanceof DecimalProperty) {
        return "long(0)";
    } else if (p instanceof MapProperty) {
        return "new std::map()";
    } else if (p instanceof ArrayProperty) {
        return "new std::list()";
    }
    // else
    if (p instanceof RefProperty) {
        RefProperty rp = (RefProperty) p;
        return "new " + toModelName(rp.getSimpleRef()) + "()";
    }
    return "null";
}
 
Example 21
Source Project: TypeScript-Microservices   Source File: XmlExampleGenerator.java    License: MIT License 5 votes vote down vote up
/**
* Get the example string value for the given Property.
*
* If an example value was not provided in the specification, a default will be generated.
*
* @param property Property to get example string for
*
* @return Example String
*/
protected String getExample(Property property) {
    if (property.getExample() != null) {
        return property.getExample().toString();
    } else if (property instanceof DateTimeProperty) {
        return "2000-01-23T04:56:07.000Z";
    } else if (property instanceof DateProperty) {
        return "2000-01-23";
    } else if (property instanceof BooleanProperty) {
        return "true";
    } else if (property instanceof LongProperty) {
        return "123456789";
    } else if (property instanceof DoubleProperty) { // derived from DecimalProperty so make sure this is first
        return "3.149";
    }  else if (property instanceof DecimalProperty) {
        return "1.3579";
    } else if (property instanceof PasswordProperty) {
        return "********";
    } else if (property instanceof UUIDProperty) {
        return "046b6c7f-0b8a-43b9-b35d-6489e6daee91";
    // do these last in case the specific types above are derived from these classes
    } else if (property instanceof StringProperty) {
        return "aeiou";
    } else if (property instanceof BaseIntegerProperty) {
        return "123";
    } else if (property instanceof AbstractNumericProperty) {
        return "1.23";
    }
    LOGGER.warn("default example value not implemented for " + property);
    return "";
}
 
Example 22
Source Project: dorado   Source File: DefaultParameterExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Property enforcePrimitive(Property in, int level) {
	if (in instanceof RefProperty) {
		return new StringProperty();
	}
	if (in instanceof ArrayProperty) {
		if (level == 0) {
			final ArrayProperty array = (ArrayProperty) in;
			array.setItems(enforcePrimitive(array.getItems(), level + 1));
		} else {
			return new StringProperty();
		}
	}
	return in;
}
 
Example 23
@Override
public JavaType doConvert(Swagger swagger, Object property) {
  StringProperty stringProperty = (StringProperty) property;

  List<String> enums = stringProperty.getEnum();
  if (!isEnum(enums)) {
    return ConverterMgr.findJavaType(stringProperty.getType(), stringProperty.getFormat());
  }

  return OBJECT_JAVA_TYPE;
}
 
Example 24
Source Project: servicecomb-java-chassis   Source File: PropertyAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getEnum() {
  if (property instanceof StringProperty) {
    return ((StringProperty) property).getEnum();
  }

  return Collections.emptyList();
}
 
Example 25
Source Project: swagger2puml   Source File: PlantUMLCodegen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param stringProperty
 * @param models
 * @param modelObject
 * @param variablName
 * @return
 */
private ClassMembers getClassMember(StringProperty stringProperty, String variablName) {
	LOGGER.entering(LOGGER.getName(), "getClassMember-StringProperty");

	ClassMembers classMemberObject = new ClassMembers();
	classMemberObject.setDataType(getDataType(stringProperty.getType(), true));
	classMemberObject.setName(variablName);

	LOGGER.exiting(LOGGER.getName(), "getClassMember-StringProperty");
	return classMemberObject;
}
 
Example 26
Source Project: swagger-diff   Source File: ModelDiff.java    License: Apache License 2.0 5 votes vote down vote up
private List<String> enumValues(Property prop) {
    if (prop instanceof StringProperty && ((StringProperty) prop).getEnum() != null) {
        return ((StringProperty) prop).getEnum();
    } else {
        return new ArrayList<>();
    }
}
 
Example 27
Source Project: swagger-diff   Source File: ParameterDiff.java    License: Apache License 2.0 5 votes vote down vote up
private Property mapToProperty(Parameter rightPara) {
    Property prop = new StringProperty();
    prop.setAccess(rightPara.getAccess());
    prop.setAllowEmptyValue(rightPara.getAllowEmptyValue());
    prop.setDescription(rightPara.getDescription());
    prop.setName(rightPara.getName());
    prop.setReadOnly(rightPara.isReadOnly());
    prop.setRequired(rightPara.getRequired());
    return prop;
}
 
Example 28
Source Project: Web-API   Source File: SwaggerDefinition.java    License: MIT License 5 votes vote down vote up
private static Response constructResponse(int status, String error) {
    Property statusProp = new IntegerProperty()
            .description("The status code of the error (also provided in the HTTP header)");
    statusProp.setExample(status);

    Property errorProp = new StringProperty()
            .description("The error message describing the error");
    errorProp.setExample((Object)error);

    return new Response()
            .description(error)
            .schema(new ObjectProperty()
                    .property("status", statusProp)
                    .property("error", errorProp));
}
 
Example 29
Source Project: swagger2markup   Source File: PropertyAdapter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Generate a default example value for property.
 *
 * @param property         property
 * @param markupDocBuilder doc builder
 * @return a generated example for the property
 */
public static Object generateExample(Property property, MarkupDocBuilder markupDocBuilder) {

    if (property.getType() == null) {
        return "untyped";
    }

    switch (property.getType()) {
        case "integer":
            return ExamplesUtil.generateIntegerExample(property instanceof IntegerProperty ? ((IntegerProperty) property).getEnum() : null);
        case "number":
            return 0.0;
        case "boolean":
            return true;
        case "string":
            return ExamplesUtil.generateStringExample(property.getFormat(), property instanceof StringProperty ? ((StringProperty) property).getEnum() : null);
        case "ref":
            if (property instanceof RefProperty) {
                if (logger.isDebugEnabled()) logger.debug("generateExample RefProperty for " + property.getName());
                return markupDocBuilder.copy(false).crossReference(((RefProperty) property).getSimpleRef()).toString();
            } else {
                if (logger.isDebugEnabled()) logger.debug("generateExample for ref not RefProperty");
            }
        case "array":
            if (property instanceof ArrayProperty) {
                return generateArrayExample((ArrayProperty) property, markupDocBuilder);
            }
        default:
            return property.getType();
    }
}
 
Example 30
Source Project: swagger2markup   Source File: PropertyAdapter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the minLength of a property
 *
 * @return the minLength of the property
 */
public Optional<Integer> getMinlength() {
    if (property instanceof StringProperty) {
        StringProperty stringProperty = (StringProperty) property;
        return Optional.ofNullable(stringProperty.getMinLength());
    } else if (property instanceof UUIDProperty) {
        UUIDProperty uuidProperty = (UUIDProperty) property;
        return Optional.ofNullable(uuidProperty.getMinLength());
    }
    return Optional.empty();
}