Search in sources :

Example 36 with StringProperty

use of io.swagger.models.properties.StringProperty in project camel by apache.

the class RestModelConverters method readClass.

public Map<String, Model> readClass(Class clazz) {
    String name = clazz.getName();
    Map<String, Model> resolved = super.read(clazz);
    if (resolved != null) {
        for (Model model : resolved.values()) {
            // enrich with the class name of the model
            model.getVendorExtensions().put("x-className", new StringProperty(name));
        }
        // read any extra using read-all
        Map<String, Model> extra = super.readAll(clazz);
        if (extra != null) {
            for (Map.Entry<String, Model> entry : extra.entrySet()) {
                if (!resolved.containsKey(entry.getKey())) {
                    resolved.put(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    return resolved;
}
Also used : Model(io.swagger.models.Model) StringProperty(io.swagger.models.properties.StringProperty) Map(java.util.Map)

Example 37 with StringProperty

use of io.swagger.models.properties.StringProperty in project camel by apache.

the class RestSwaggerReader method modelTypeAsProperty.

private Property modelTypeAsProperty(String typeName, Swagger swagger) {
    boolean array = typeName.endsWith("[]");
    if (array) {
        typeName = typeName.substring(0, typeName.length() - 2);
    }
    String ref = modelTypeAsRef(typeName, swagger);
    Property prop = ref != null ? new RefProperty(ref) : new StringProperty(typeName);
    if (array) {
        return new ArrayProperty(prop);
    } else {
        return prop;
    }
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) StringProperty(io.swagger.models.properties.StringProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) FloatProperty(io.swagger.models.properties.FloatProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) RefProperty(io.swagger.models.properties.RefProperty)

Example 38 with StringProperty

use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.

the class ModelResolver method resolveProperty.

public Property resolveProperty(JavaType propType, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> next) {
    LOGGER.debug("resolveProperty {}", propType);
    Property property = null;
    if (propType.isContainerType()) {
        JavaType keyType = propType.getKeyType();
        JavaType valueType = propType.getContentType();
        if (keyType != null && valueType != null) {
            property = new MapProperty().additionalProperties(context.resolveProperty(valueType, new Annotation[] {}));
        } else if (valueType != null) {
            Property items = context.resolveProperty(valueType, new Annotation[] {});
            // If property is XmlElement annotated, then use the name provided by annotation | https://github.com/swagger-api/swagger-core/issues/2047
            if (annotations != null && annotations.length > 0) {
                for (Annotation annotation : annotations) {
                    if (annotation instanceof XmlElement) {
                        XmlElement xmlElement = (XmlElement) annotation;
                        if (xmlElement != null && xmlElement.name() != null && !"".equals(xmlElement.name()) && !"##default".equals(xmlElement.name())) {
                            Xml xml = items.getXml() != null ? items.getXml() : new Xml();
                            xml.setName(xmlElement.name());
                            items.setXml(xml);
                        }
                    }
                }
            }
            ArrayProperty arrayProperty = new ArrayProperty().items(items);
            if (_isSetType(propType.getRawClass())) {
                arrayProperty.setUniqueItems(true);
            }
            property = arrayProperty;
        }
    } else {
        property = PrimitiveType.createProperty(propType);
    }
    if (property == null) {
        if (propType.isEnumType()) {
            property = new StringProperty();
            _addEnumProps(propType.getRawClass(), property);
        } else if (_isOptionalType(propType)) {
            property = context.resolveProperty(propType.containedType(0), null);
        } else {
            // complex type
            Model innerModel = context.resolve(propType);
            if (innerModel instanceof ComposedModel) {
                innerModel = ((ComposedModel) innerModel).getChild();
            }
            if (innerModel instanceof ModelImpl) {
                ModelImpl mi = (ModelImpl) innerModel;
                property = new RefProperty(StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName());
            }
        }
    }
    return property;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ArrayProperty(io.swagger.models.properties.ArrayProperty) Xml(io.swagger.models.Xml) ComposedModel(io.swagger.models.ComposedModel) MapProperty(io.swagger.models.properties.MapProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) XmlElement(javax.xml.bind.annotation.XmlElement) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) UUIDProperty(io.swagger.models.properties.UUIDProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Annotation(java.lang.annotation.Annotation) RefProperty(io.swagger.models.properties.RefProperty)

Example 39 with StringProperty

use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.

the class ModelResolver method applyBeanValidatorAnnotations.

protected void applyBeanValidatorAnnotations(Property property, Annotation[] annotations) {
    Map<String, Annotation> annos = new HashMap<String, Annotation>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    if (annos.containsKey("javax.validation.constraints.NotNull")) {
        property.setRequired(true);
    }
    if (annos.containsKey("javax.validation.constraints.Min")) {
        if (property instanceof AbstractNumericProperty) {
            Min min = (Min) annos.get("javax.validation.constraints.Min");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new BigDecimal(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Max")) {
        if (property instanceof AbstractNumericProperty) {
            Max max = (Max) annos.get("javax.validation.constraints.Max");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new BigDecimal(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Size")) {
        Size size = (Size) annos.get("javax.validation.constraints.Size");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new BigDecimal(size.min()));
            ap.setMaximum(new BigDecimal(size.max()));
        } else if (property instanceof StringProperty) {
            StringProperty sp = (StringProperty) property;
            sp.minLength(new Integer(size.min()));
            sp.maxLength(new Integer(size.max()));
        } else if (property instanceof ArrayProperty) {
            ArrayProperty sp = (ArrayProperty) property;
            sp.setMinItems(size.min());
            sp.setMaxItems(size.max());
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
        DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new BigDecimal(min.value()));
            ap.setExclusiveMinimum(!min.inclusive());
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
        DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new BigDecimal(max.value()));
            ap.setExclusiveMaximum(!max.inclusive());
        }
    }
    if (annos.containsKey("javax.validation.constraints.Pattern")) {
        Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
        if (property instanceof StringProperty) {
            StringProperty ap = (StringProperty) property;
            ap.setPattern(pattern.regexp());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) StringProperty(io.swagger.models.properties.StringProperty) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) DecimalMax(javax.validation.constraints.DecimalMax)

Example 40 with StringProperty

use of io.swagger.models.properties.StringProperty in project swagger-core by swagger-api.

the class ModelWithRangesTest method modelWithRangesTest.

@Test(description = "test model with @ApiModelProperty.allowableValues")
public void modelWithRangesTest() {
    final Map<String, Property> properties = ModelConverters.getInstance().read(ModelWithRanges.class).get("ModelWithRanges").getProperties();
    final IntegerProperty inclusiveRange = (IntegerProperty) properties.get("inclusiveRange");
    assertEquals(inclusiveRange.getMinimum(), new BigDecimal(1));
    assertEquals(inclusiveRange.getMaximum(), new BigDecimal(5));
    assertNull(inclusiveRange.getExclusiveMaximum());
    assertNull(inclusiveRange.getExclusiveMinimum());
    final IntegerProperty exclusiveRange = (IntegerProperty) properties.get("exclusiveRange");
    assertEquals(exclusiveRange.getMinimum(), new BigDecimal(1));
    assertEquals(exclusiveRange.getMaximum(), new BigDecimal(5));
    assertEquals(exclusiveRange.getExclusiveMinimum(), Boolean.TRUE);
    assertEquals(exclusiveRange.getExclusiveMaximum(), Boolean.TRUE);
    final IntegerProperty positiveInfinityRange = (IntegerProperty) properties.get("positiveInfinityRange");
    assertEquals(positiveInfinityRange.getMinimum(), new BigDecimal(1.0));
    assertNull(positiveInfinityRange.getMaximum());
    assertNull(positiveInfinityRange.getExclusiveMaximum());
    assertNull(positiveInfinityRange.getExclusiveMinimum());
    final IntegerProperty negativeInfinityRange = (IntegerProperty) properties.get("negativeInfinityRange");
    assertNull(negativeInfinityRange.getMinimum());
    assertEquals(negativeInfinityRange.getMaximum(), new BigDecimal(5.0));
    assertNull(negativeInfinityRange.getExclusiveMaximum());
    assertNull(negativeInfinityRange.getExclusiveMinimum());
    final StringProperty stringValues = (StringProperty) properties.get("stringValues");
    assertEquals(stringValues.getEnum(), Arrays.asList("str1", "str2"));
    final DoubleProperty doubleValues = (DoubleProperty) properties.get("doubleValues");
    assertEquals(doubleValues.getMinimum(), new BigDecimal("1.0"));
    assertEquals(doubleValues.getMaximum(), new BigDecimal("8.0"));
    assertEquals(doubleValues.getExclusiveMaximum(), Boolean.TRUE);
    assertNull(doubleValues.getExclusiveMinimum());
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) StringProperty(io.swagger.models.properties.StringProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) DoubleProperty(io.swagger.models.properties.DoubleProperty) StringProperty(io.swagger.models.properties.StringProperty) Property(io.swagger.models.properties.Property) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

StringProperty (io.swagger.models.properties.StringProperty)60 Test (org.testng.annotations.Test)47 Property (io.swagger.models.properties.Property)29 ArrayProperty (io.swagger.models.properties.ArrayProperty)21 Model (io.swagger.models.Model)20 IntegerProperty (io.swagger.models.properties.IntegerProperty)16 LongProperty (io.swagger.models.properties.LongProperty)16 RefProperty (io.swagger.models.properties.RefProperty)16 DoubleProperty (io.swagger.models.properties.DoubleProperty)14 ModelImpl (io.swagger.models.ModelImpl)9 FloatProperty (io.swagger.models.properties.FloatProperty)9 BooleanProperty (io.swagger.models.properties.BooleanProperty)8 ApiModelProperty (io.swagger.annotations.ApiModelProperty)7 BodyParameter (io.swagger.models.parameters.BodyParameter)7 MapProperty (io.swagger.models.properties.MapProperty)7 Operation (io.swagger.models.Operation)6 Path (io.swagger.models.Path)6 PathParameter (io.swagger.models.parameters.PathParameter)6 ObjectProperty (io.swagger.models.properties.ObjectProperty)6 QueryParameter (io.swagger.models.parameters.QueryParameter)5