Search in sources :

Example 11 with BooleanProperty

use of io.swagger.models.properties.BooleanProperty in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method addResourceParameters.

/**
 * Creates parameters in the swagger operation using the parameters in the ballerina resource definition.
 * @param resource The ballerina resource definition.
 * @param operationAdaptor The swagger operation.
 */
private void addResourceParameters(ResourceNode resource, OperationAdaptor operationAdaptor) {
    if (!"get".equalsIgnoreCase(operationAdaptor.getHttpOperation())) {
        // Creating request body - required.
        ModelImpl messageModel = new ModelImpl();
        messageModel.setType("object");
        Map<String, Model> definitions = new HashMap<>();
        if (!definitions.containsKey("Request")) {
            definitions.put("Request", messageModel);
            this.swaggerDefinition.setDefinitions(definitions);
        }
        // Creating "Request rq" parameter
        BodyParameter messageParameter = new BodyParameter();
        messageParameter.setName(resource.getParameters().get(0).getName().getValue());
        RefModel refModel = new RefModel();
        refModel.setReference("Request");
        messageParameter.setSchema(refModel);
        operationAdaptor.getOperation().addParameter(messageParameter);
    }
    for (int i = 2; i < resource.getParameters().size(); i++) {
        VariableNode parameterDef = resource.getParameters().get(i);
        String typeName = parameterDef.getTypeNode().toString().toLowerCase(Locale.getDefault());
        PathParameter pathParameter = new PathParameter();
        // Set in value
        pathParameter.setIn("path");
        // Set parameter name
        String parameterName = parameterDef.getName().getValue();
        pathParameter.setName(parameterName);
        // Set type
        if (typeName.contains("[]")) {
            pathParameter.setType("array");
            switch(typeName.replace("[]", "").trim()) {
                case "string":
                    pathParameter.items(new StringProperty());
                    break;
                case "int":
                    pathParameter.items(new IntegerProperty());
                    break;
                case "boolean":
                    pathParameter.items(new BooleanProperty());
                    break;
                default:
                    break;
            }
        } else if ("int".equals(typeName)) {
            pathParameter.setType("integer");
        } else {
            pathParameter.setType(typeName);
        }
        // Note: 'format' to be added using annotations, hence skipped here.
        operationAdaptor.getOperation().addParameter(pathParameter);
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) StringProperty(io.swagger.models.properties.StringProperty) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) VariableNode(org.ballerinalang.model.tree.VariableNode) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ModelImpl(io.swagger.models.ModelImpl)

Example 12 with BooleanProperty

use of io.swagger.models.properties.BooleanProperty in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method createHeadersModel.

/**
 * Creates headers definitions for swagger response.
 *
 * @param annotationAttributeValue The annotation attribute value which has the headers.
 * @param response                 The swagger response.
 */
private void createHeadersModel(AnnAttributeValue annotationAttributeValue, Response response) {
    if (null != annotationAttributeValue) {
        AnnAttributeValue[] headersValueArray = annotationAttributeValue.getAttributeValueArray();
        for (AnnAttributeValue headersValue : headersValueArray) {
            AnnAttachmentInfo headerAnnotationAttachment = headersValue.getAnnotationAttachmentValue();
            Map<String, AnnAttributeValue> headerAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(headerAnnotationAttachment);
            Map<String, Property> headers = new HashMap<>();
            if (null != headerAnnAttributeValueMap.get("name") && null != headerAnnAttributeValueMap.get("headerType")) {
                String headerName = headerAnnAttributeValueMap.get("name").getStringValue();
                String type = headerAnnAttributeValueMap.get("headerType").getStringValue();
                Property property = null;
                if ("string".equals(type)) {
                    property = new StringProperty();
                } else if ("number".equals(type) || "integer".equals(type)) {
                    property = new IntegerProperty();
                } else if ("boolean".equals(type)) {
                    property = new BooleanProperty();
                } else if ("array".equals(type)) {
                    property = new ArrayProperty();
                }
                if (null != property) {
                    if (null != headerAnnAttributeValueMap.get("description")) {
                        property.setDescription(headerAnnAttributeValueMap.get("description").getStringValue());
                    }
                    headers.put(headerName, property);
                }
            }
            response.setHeaders(headers);
        }
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) StringProperty(io.swagger.models.properties.StringProperty) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty)

Example 13 with BooleanProperty

use of io.swagger.models.properties.BooleanProperty in project herd by FINRAOS.

the class DefinitionGenerator method getPropertyFromType.

/**
 * Gets a property from the given fieldType. This method may be called recursively.
 *
 * @param fieldType the field type class.
 *
 * @return the property.
 * @throws MojoExecutionException if any problems were encountered.
 */
private Property getPropertyFromType(Class<?> fieldType) throws MojoExecutionException {
    Property property;
    if (String.class.isAssignableFrom(fieldType)) {
        property = new StringProperty();
    } else if (Integer.class.isAssignableFrom(fieldType) || int.class.isAssignableFrom(fieldType)) {
        property = new IntegerProperty();
    } else if (Long.class.isAssignableFrom(fieldType) || long.class.isAssignableFrom(fieldType)) {
        property = new LongProperty();
    } else if (BigDecimal.class.isAssignableFrom(fieldType)) {
        property = new DecimalProperty();
    } else if (XMLGregorianCalendar.class.isAssignableFrom(fieldType)) {
        property = new DateTimeProperty();
    } else if (Boolean.class.isAssignableFrom(fieldType) || boolean.class.isAssignableFrom(fieldType)) {
        property = new BooleanProperty();
    } else if (Collection.class.isAssignableFrom(fieldType)) {
        property = new ArrayProperty(new StringProperty());
    } else if (fieldType.getAnnotation(XmlEnum.class) != null) {
        /*
             * Enums are a string property which have enum constants
             */
        List<String> enums = new ArrayList<>();
        for (Enum<?> anEnum : (Enum<?>[]) fieldType.getEnumConstants()) {
            enums.add(anEnum.name());
        }
        property = new StringProperty()._enum(enums);
    } else /*
         * Recursively process complex objects which is a XmlType
         */
    if (fieldType.getAnnotation(XmlType.class) != null) {
        processDefinitionClass(fieldType);
        property = new RefProperty(fieldType.getAnnotation(XmlType.class).name());
    } else {
        // Default to a string property in other cases.
        property = new StringProperty();
    }
    log.debug("Field type \"" + fieldType.getName() + "\" is a property type \"" + property.getType() + "\".");
    return property;
}
Also used : XmlEnum(javax.xml.bind.annotation.XmlEnum) IntegerProperty(io.swagger.models.properties.IntegerProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) StringProperty(io.swagger.models.properties.StringProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) BigDecimal(java.math.BigDecimal) XmlType(javax.xml.bind.annotation.XmlType) RefProperty(io.swagger.models.properties.RefProperty) LongProperty(io.swagger.models.properties.LongProperty) ArrayList(java.util.ArrayList) List(java.util.List) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) LongProperty(io.swagger.models.properties.LongProperty) RefProperty(io.swagger.models.properties.RefProperty) DecimalProperty(io.swagger.models.properties.DecimalProperty) Property(io.swagger.models.properties.Property)

Example 14 with BooleanProperty

use of io.swagger.models.properties.BooleanProperty in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method createHeadersModel.

/**
 * Creates headers definitions for swagger response.
 * @param annotationAttributeValue The annotation attribute value which has the headers.
 * @param response The swagger response.
 */
private void createHeadersModel(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Response response) {
    if (null != annotationAttributeValue) {
        List<? extends AnnotationAttachmentAttributeValueNode> headersValueArray = annotationAttributeValue.getValueArray();
        for (AnnotationAttachmentAttributeValueNode headersValue : headersValueArray) {
            if (headersValue instanceof AnnotationAttachmentNode) {
                AnnotationAttachmentNode headerAnnotationAttachment = (AnnotationAttachmentNode) headersValue;
                Map<String, Property> headers = new HashMap<>();
                Map<String, AnnotationAttachmentAttributeValueNode> headersAttributes = this.listToMap(headerAnnotationAttachment);
                if (headersAttributes.containsKey("name") && headersAttributes.containsKey("headerType")) {
                    String headerName = this.getStringLiteralValue(headersAttributes.get("name"));
                    String type = this.getStringLiteralValue(headersAttributes.get("headerType"));
                    Property property = null;
                    if ("string".equals(type)) {
                        property = new StringProperty();
                    } else if ("number".equals(type) || "integer".equals(type)) {
                        property = new IntegerProperty();
                    } else if ("boolean".equals(type)) {
                        property = new BooleanProperty();
                    } else if ("array".equals(type)) {
                        property = new ArrayProperty();
                    }
                    if (null != property) {
                        if (headersAttributes.containsKey("description")) {
                            property.setDescription(this.getStringLiteralValue(headersAttributes.get("description")));
                        }
                        headers.put(headerName, property);
                    }
                }
                response.setHeaders(headers);
            }
        }
    }
}
Also used : IntegerProperty(io.swagger.models.properties.IntegerProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) BooleanProperty(io.swagger.models.properties.BooleanProperty) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) StringProperty(io.swagger.models.properties.StringProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

BooleanProperty (io.swagger.models.properties.BooleanProperty)14 Property (io.swagger.models.properties.Property)8 StringProperty (io.swagger.models.properties.StringProperty)8 Test (org.testng.annotations.Test)8 ArrayProperty (io.swagger.models.properties.ArrayProperty)7 IntegerProperty (io.swagger.models.properties.IntegerProperty)7 LongProperty (io.swagger.models.properties.LongProperty)4 RefProperty (io.swagger.models.properties.RefProperty)4 DoubleProperty (io.swagger.models.properties.DoubleProperty)3 FloatProperty (io.swagger.models.properties.FloatProperty)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Model (io.swagger.models.Model)2 RefModel (io.swagger.models.RefModel)2 Response (io.swagger.models.Response)2 BodyParameter (io.swagger.models.parameters.BodyParameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2 DateTimeProperty (io.swagger.models.properties.DateTimeProperty)2 DecimalProperty (io.swagger.models.properties.DecimalProperty)2 BigDecimal (java.math.BigDecimal)2