Search in sources :

Example 6 with AbstractSerializableParameter

use of io.swagger.models.parameters.AbstractSerializableParameter in project teiid by teiid.

the class SwaggerMetadataProcessor method addProcedureParameters.

private void addProcedureParameters(final MetadataFactory mf, final Swagger swagger, final Procedure procedure, final Operation operation) throws TranslatorException {
    for (final Parameter parameter : operation.getParameters()) {
        if (parameter instanceof BodyParameter) {
            PropertyAction pa = new PropertyAction() {

                @Override
                public void execute(String name, String nameInSource, Property property, boolean array) {
                    String type = getPropertyType(property, array);
                    if (procedure.getParameterByName(nameInSource) == null) {
                        ProcedureParameter param = mf.addProcedureParameter(name, type, Type.In, procedure);
                        param.setProperty(PARAMETER_TYPE, parameter.getIn());
                        if (property != null && !property.getRequired()) {
                            param.setProperty(BaseColumn.DEFAULT_HANDLING, BaseColumn.OMIT_DEFAULT);
                        }
                        param.setNullType(NullType.No_Nulls);
                        param.setAnnotation(property != null ? property.getDescription() : null);
                        if (!name.equalsIgnoreCase(nameInSource)) {
                            param.setNameInSource(nameInSource);
                        }
                    }
                }
            };
            Model model = ((BodyParameter) parameter).getSchema();
            if (model instanceof RefModel) {
                RefModel refModel = (RefModel) model;
                if (refModel.getProperties() != null) {
                    walkProperties(swagger, refModel.getProperties(), null, null, pa);
                } else if (refModel.getReference() != null) {
                    Model m = swagger.getDefinitions().get(refModel.getSimpleRef());
                    walkProperties(swagger, m.getProperties(), null, null, pa);
                }
            } else {
                if ((model instanceof ModelImpl) && model.getProperties() != null) {
                    walkProperties(swagger, model.getProperties(), null, null, pa);
                } else {
                    ProcedureParameter p = mf.addProcedureParameter(parameter.getName(), DataTypeManager.DefaultDataTypes.CLOB, Type.In, procedure);
                    p.setProperty(PARAMETER_TYPE, parameter.getIn());
                    p.setNullType(NullType.No_Nulls);
                    p.setAnnotation(parameter.getDescription());
                }
            }
        } else {
            String name = parameter.getName();
            ProcedureParameter pp = null;
            String type = null;
            Object defaultValue = null;
            String collectionFormat = null;
            if (parameter instanceof AbstractSerializableParameter) {
                AbstractSerializableParameter p = (AbstractSerializableParameter) parameter;
                type = p.getType();
                if (p.getType().equalsIgnoreCase("array")) {
                    Property ap = p.getItems();
                    type = ap.getType();
                }
                type = SwaggerTypeManager.teiidType(type, p.getFormat(), p.getItems() != null);
                defaultValue = p.getDefaultValue();
                collectionFormat = p.getCollectionFormat();
            } else {
                // $NON-NLS-1$
                throw new MetadataException("Unknown property type" + parameter.getClass().getName());
            }
            pp = mf.addProcedureParameter(name, type, Type.In, procedure);
            pp.setProperty(PARAMETER_TYPE, parameter.getIn());
            boolean required = parameter.getRequired();
            if (!required) {
                pp.setProperty(BaseColumn.DEFAULT_HANDLING, BaseColumn.OMIT_DEFAULT);
            }
            pp.setNullType(NullType.No_Nulls);
            pp.setAnnotation(parameter.getDescription());
            if (defaultValue != null) {
                pp.setDefaultValue(defaultValue.toString());
            }
            if (collectionFormat != null) {
                pp.setProperty(COLLECION_FORMAT, collectionFormat);
            }
            // extended properties
            for (Entry<String, Object> extension : parameter.getVendorExtensions().entrySet()) {
                pp.setProperty(extension.getKey(), extension.getValue().toString());
            }
        }
    }
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) MetadataException(org.teiid.metadata.MetadataException) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter) ProcedureParameter(org.teiid.metadata.ProcedureParameter) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) TranslatorProperty(org.teiid.translator.TranslatorProperty) ExtensionMetadataProperty(org.teiid.metadata.ExtensionMetadataProperty) ObjectProperty(io.swagger.models.properties.ObjectProperty) FileProperty(io.swagger.models.properties.FileProperty)

Example 7 with AbstractSerializableParameter

use of io.swagger.models.parameters.AbstractSerializableParameter in project swagger-parser by swagger-api.

the class SwaggerConverter method convert.

public Parameter convert(io.swagger.models.parameters.Parameter v2Parameter) {
    Parameter v3Parameter = new Parameter();
    if (StringUtils.isNotBlank(v2Parameter.getDescription())) {
        v3Parameter.setDescription(v2Parameter.getDescription());
    }
    if (v2Parameter instanceof SerializableParameter) {
        v3Parameter.setAllowEmptyValue(((SerializableParameter) v2Parameter).getAllowEmptyValue());
    }
    v3Parameter.setIn(v2Parameter.getIn());
    v3Parameter.setName(v2Parameter.getName());
    Object exampleExtension = v2Parameter.getVendorExtensions().get("x-example");
    if (exampleExtension != null) {
        v3Parameter.setExample(exampleExtension);
    }
    Schema schema = null;
    if (v2Parameter instanceof RefParameter) {
        RefParameter ref = (RefParameter) v2Parameter;
        if (ref.get$ref().indexOf("#/parameters") == 0) {
            String updatedRef = "#/components/";
            if (components.getRequestBodies() != null && components.getRequestBodies().get(ref.getSimpleRef()) != null) {
                updatedRef += "requestBodies";
            } else if (components.getSchemas() != null && components.getSchemas().get("formData_" + ref.getSimpleRef()) != null) {
                updatedRef += "schemas";
            } else {
                updatedRef += "parameters";
            }
            updatedRef += ref.get$ref().substring("#/parameters".length());
            ref.set$ref(updatedRef);
        }
        v3Parameter.set$ref(ref.get$ref());
    } else if (v2Parameter instanceof SerializableParameter) {
        SerializableParameter sp = (SerializableParameter) v2Parameter;
        if ("array".equals(sp.getType())) {
            ArraySchema a = new ArraySchema();
            // TODO: convert arrays to proper template format
            String cf = sp.getCollectionFormat();
            if (StringUtils.isEmpty(cf)) {
                cf = "csv";
            }
            switch(cf) {
                case "ssv":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.SPACEDELIMITED);
                    }
                    break;
                case "pipes":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle((Parameter.StyleEnum.PIPEDELIMITED));
                    }
                    break;
                case "tsv":
                    break;
                case "multi":
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.FORM);
                        v3Parameter.setExplode(true);
                    }
                    break;
                case "csv":
                default:
                    if ("query".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.FORM);
                        v3Parameter.setExplode(false);
                    } else if ("header".equals(v2Parameter.getIn()) || "path".equals(v2Parameter.getIn())) {
                        v3Parameter.setStyle(Parameter.StyleEnum.SIMPLE);
                        v3Parameter.setExplode(false);
                    }
            }
            Property items = sp.getItems();
            Schema itemsSchema = convert(items);
            a.setItems(itemsSchema);
            if (sp.getMaxItems() != null) {
                a.setMaxItems(sp.getMaxItems());
            }
            if (sp.getMinItems() != null) {
                a.setMinItems(sp.getMinItems());
            }
            if (sp.isUniqueItems() != null) {
                a.setUniqueItems(sp.isUniqueItems());
            }
            schema = a;
        } else {
            schema = SchemaTypeUtil.createSchema(sp.getType(), sp.getFormat());
            schema.setType(sp.getType());
            schema.setFormat(sp.getFormat());
            schema.setMaximum(sp.getMaximum());
            schema.setExclusiveMaximum(sp.isExclusiveMaximum());
            schema.setMinimum(sp.getMinimum());
            schema.setExclusiveMinimum(sp.isExclusiveMinimum());
            schema.setMinLength(sp.getMinLength());
            schema.setMaxLength(sp.getMaxLength());
            if (sp.getMultipleOf() != null) {
                schema.setMultipleOf(new BigDecimal(sp.getMultipleOf().toString()));
            }
            schema.setPattern(sp.getPattern());
        }
        if (sp.getEnum() != null) {
            for (String e : sp.getEnum()) {
                switch(sp.getType() == null ? SchemaTypeUtil.OBJECT_TYPE : sp.getType()) {
                    case SchemaTypeUtil.INTEGER_TYPE:
                        schema.addEnumItemObject(Integer.parseInt(e));
                        break;
                    case SchemaTypeUtil.NUMBER_TYPE:
                        schema.addEnumItemObject(new BigDecimal(e));
                        break;
                    case SchemaTypeUtil.BOOLEAN_TYPE:
                        schema.addEnumItemObject(Boolean.valueOf(e));
                        break;
                    default:
                        schema.addEnumItemObject(e);
                        break;
                }
            }
        }
        if (sp.getVendorExtensions() != null) {
            Object nullableExtension = sp.getVendorExtensions().get("x-nullable");
            if (nullableExtension != null) {
                schema.setNullable((Boolean) nullableExtension);
            }
        }
        schema.setExtensions(convert(sp.getVendorExtensions()));
        if (sp instanceof AbstractSerializableParameter) {
            AbstractSerializableParameter ap = (AbstractSerializableParameter) sp;
            schema.setDefault(ap.getDefault());
        }
    }
    if (v2Parameter.getRequired()) {
        v3Parameter.setRequired(v2Parameter.getRequired());
    }
    v3Parameter.setSchema(schema);
    v3Parameter.setExtensions(convert(v2Parameter.getVendorExtensions()));
    return v3Parameter;
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) Parameter(io.swagger.v3.oas.models.parameters.Parameter) SerializableParameter(io.swagger.models.parameters.SerializableParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) RefParameter(io.swagger.models.parameters.RefParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) BigDecimal(java.math.BigDecimal)

Example 8 with AbstractSerializableParameter

use of io.swagger.models.parameters.AbstractSerializableParameter in project syndesis by syndesisio.

the class UnifiedJsonDataShapeGenerator method createJsonSchemaForParametersOf.

private static JsonNode createJsonSchemaForParametersOf(final Operation operation) {
    final List<Parameter> operationParameters = operation.getParameters();
    final List<AbstractSerializableParameter<?>> serializableParameters = // 
    operationParameters.stream().filter(// 
    PARAM_CLASS::isInstance).map(// 
    PARAM_CLASS::cast).collect(Collectors.toList());
    if (serializableParameters.isEmpty()) {
        return null;
    }
    final ObjectNode schema = newJsonObjectSchema();
    final ObjectNode properties = schema.putObject("properties");
    final ObjectNode parameters = properties.putObject("parameters");
    parameters.put("type", "object");
    final ObjectNode parametersProperties = parameters.putObject("properties");
    for (final AbstractSerializableParameter<?> serializableParameter : serializableParameters) {
        final String type = serializableParameter.getType();
        final String name = trimToNull(serializableParameter.getName());
        final String description = trimToNull(serializableParameter.getDescription());
        if ("file".equals(type)) {
            // 'file' type is not allowed in JSON schema
            continue;
        }
        final ObjectNode parameterParameter = parametersProperties.putObject(name);
        if (type != null) {
            parameterParameter.put("type", type);
        }
        if (name != null) {
            parameterParameter.put("title", name);
        }
        if (description != null) {
            parameterParameter.put("description", description);
        }
        final Object defaultValue = serializableParameter.getDefault();
        if (defaultValue != null) {
            parameterParameter.put("default", String.valueOf(defaultValue));
        }
        addEnumsTo(parameterParameter, serializableParameter);
    }
    return schema;
}
Also used : AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Parameter(io.swagger.models.parameters.Parameter)

Example 9 with AbstractSerializableParameter

use of io.swagger.models.parameters.AbstractSerializableParameter in project java-chassis by ServiceComb.

the class AbstractOperationGenerator method fillParameter.

protected void fillParameter(Swagger swagger, Parameter parameter, String parameterName, JavaType type, List<Annotation> annotations) {
    for (Annotation annotation : annotations) {
        ParameterProcessor<Parameter, Annotation> processor = findParameterProcessors(annotation.annotationType());
        if (processor != null) {
            processor.fillParameter(swagger, swaggerOperation, parameter, type, annotation);
        }
    }
    if (type == null) {
        return;
    }
    ParameterProcessor<Parameter, Annotation> processor = findParameterProcessors(type);
    if (processor != null) {
        processor.fillParameter(swagger, swaggerOperation, parameter, type, null);
    }
    if (parameter instanceof AbstractSerializableParameter) {
        io.swagger.util.ParameterProcessor.applyAnnotations(swagger, parameter, type, annotations);
        annotations.stream().forEach(annotation -> {
            if (NOT_NULL_ANNOTATIONS.contains(annotation.annotationType().getSimpleName())) {
                parameter.setRequired(true);
            }
        });
        return;
    }
    fillBodyParameter(swagger, parameter, type, annotations);
}
Also used : AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) SwaggerGeneratorUtils.isContextParameter(org.apache.servicecomb.swagger.generator.SwaggerGeneratorUtils.isContextParameter) Annotation(java.lang.annotation.Annotation)

Example 10 with AbstractSerializableParameter

use of io.swagger.models.parameters.AbstractSerializableParameter in project java-chassis by ServiceComb.

the class EnumPostProcessor method processParameterDescription.

private void processParameterDescription(ParameterGenerator parameterGenerator) {
    JavaType genericType = parameterGenerator.getGenericType();
    Annotation[] annotations = parameterGenerator.getAnnotations().toArray(new Annotation[0]);
    String description = generateDescription(genericType, annotations);
    if (description != null) {
        AbstractSerializableParameter<?> parameter = (AbstractSerializableParameter<?>) parameterGenerator.getGeneratedParameter();
        parameter.setDescription(description);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) AbstractSerializableParameter(io.swagger.models.parameters.AbstractSerializableParameter) Annotation(java.lang.annotation.Annotation)

Aggregations

AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)10 BodyParameter (io.swagger.models.parameters.BodyParameter)7 Parameter (io.swagger.models.parameters.Parameter)5 ArrayProperty (io.swagger.models.properties.ArrayProperty)3 Property (io.swagger.models.properties.Property)3 Annotation (java.lang.annotation.Annotation)3 Model (io.swagger.models.Model)2 RefModel (io.swagger.models.RefModel)2 FormParameter (io.swagger.models.parameters.FormParameter)2 HeaderParameter (io.swagger.models.parameters.HeaderParameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 SerializableParameter (io.swagger.models.parameters.SerializableParameter)2 FileProperty (io.swagger.models.properties.FileProperty)2 LongProperty (io.swagger.models.properties.LongProperty)2 RefProperty (io.swagger.models.properties.RefProperty)2 StringProperty (io.swagger.models.properties.StringProperty)2 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)2 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)2 FileSchema (io.swagger.v3.oas.models.media.FileSchema)2