Search in sources :

Example 26 with MapProperty

use of io.swagger.models.properties.MapProperty in project incubator-servicecomb-java-chassis by apache.

the class MapPropertyConverter method doConvert.

@Override
public JavaType doConvert(ClassLoader classLoader, String packageName, Swagger swagger, Object property) {
    MapProperty mapProperty = (MapProperty) property;
    Property valueProperty = mapProperty.getAdditionalProperties();
    return findJavaType(classLoader, packageName, swagger, valueProperty);
}
Also used : MapProperty(io.swagger.models.properties.MapProperty) MapProperty(io.swagger.models.properties.MapProperty) Property(io.swagger.models.properties.Property)

Example 27 with MapProperty

use of io.swagger.models.properties.MapProperty in project teiid by teiid.

the class SwaggerMetadataProcessor method buildResponse.

private boolean buildResponse(final MetadataFactory mf, final Swagger swagger, final Procedure procedure, final Response resp) throws TranslatorException {
    PropertyAction pa = new PropertyAction() {

        @Override
        public void execute(String name, String nameInSource, Property property, boolean array) {
            String type = getPropertyType(property, array);
            Column c = mf.addProcedureResultSetColumn(name, type, procedure);
            if (!name.equalsIgnoreCase(nameInSource)) {
                c.setNameInSource(nameInSource);
            }
        }
    };
    Property schema = resp.getSchema();
    if (schema != null) {
        if (isSimple(schema)) {
            boolean array = false;
            if (schema instanceof ArrayProperty) {
                schema = ((ArrayProperty) schema).getItems();
                array = true;
            }
            if (resp.getHeaders() == null || resp.getHeaders().isEmpty()) {
                String type = SwaggerTypeManager.teiidType(schema.getType(), schema.getFormat(), array);
                mf.addProcedureParameter("return", type, ProcedureParameter.Type.ReturnValue, procedure);
            } else {
                Map<String, Property> properties = new LinkedHashMap<String, Property>();
                properties.put("return", schema);
                walkProperties(swagger, properties, null, null, pa);
            }
        } else {
            // since the return is always a collection unwrap the array without any issues.
            if (schema instanceof ArrayProperty) {
                schema = ((ArrayProperty) schema).getItems();
            }
            if (schema instanceof ObjectProperty) {
                walkProperties(swagger, ((ObjectProperty) schema).getProperties(), null, null, pa);
            } else if (schema instanceof RefProperty) {
                String modelName = ((RefProperty) schema).getSimpleRef();
                Model model = swagger.getDefinitions().get(modelName);
                walkProperties(swagger, model.getProperties(), null, null, pa);
            } else if (schema instanceof MapProperty) {
                Property property = ((MapProperty) schema).getAdditionalProperties();
                String type = SwaggerTypeManager.teiidType(property.getType(), property.getFormat(), false);
                Column c = mf.addProcedureResultSetColumn(KEY_NAME, "string", procedure);
                c.setNameInSource(KEY_NAME);
                c = mf.addProcedureResultSetColumn(KEY_VALUE, type, procedure);
                c.setNameInSource(KEY_VALUE);
            } else {
                throw new TranslatorException("File properties are not supported");
            }
        }
    }
    Map<String, Property> headers = resp.getHeaders();
    if (headers != null && !headers.isEmpty()) {
        walkProperties(swagger, headers, null, null, pa);
    }
    return procedure.getResultSet() != null;
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) LinkedHashMap(java.util.LinkedHashMap) RefProperty(io.swagger.models.properties.RefProperty) BaseColumn(org.teiid.metadata.BaseColumn) Column(org.teiid.metadata.Column) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) TranslatorException(org.teiid.translator.TranslatorException) 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 28 with MapProperty

use of io.swagger.models.properties.MapProperty in project teiid by teiid.

the class SwaggerMetadataProcessor method walkProperties.

private void walkProperties(final Swagger swagger, Set<Property> parents, final Map<String, Property> properties, final String namePrefix, final String nisPrefix, final PropertyAction pa) {
    if (properties == null) {
        return;
    }
    final PropertyVisitor visitor = new PropertyVisitor() {

        @Override
        public void visit(String name, Property property) {
            pa.execute(fqn(namePrefix, name), nis(nisPrefix, name, false), property, false);
        }

        @Override
        public void visit(String name, ArrayProperty property) {
            if (isSimple(property)) {
                // the array type defined in the type of the property
                pa.execute(fqn(namePrefix, name), nis(nisPrefix, name, false), property.getItems(), true);
            } else {
                // if Object or Ref, array does not matter as return is already a resultset.
                Property items = property.getItems();
                parents.add(property);
                if (items instanceof ObjectProperty) {
                    String modelName = ((ObjectProperty) items).getName();
                    walkProperties(swagger, parents, ((ObjectProperty) items).getProperties(), fqn(fqn(namePrefix, name), modelName), nis(nis(nisPrefix, name, true), modelName, false), pa);
                } else if (items instanceof RefProperty) {
                    String modelName = ((RefProperty) items).getSimpleRef();
                    Model model = swagger.getDefinitions().get(modelName);
                    walkProperties(swagger, parents, model.getProperties(), fqn(fqn(namePrefix, name), modelName), nis(nis(nisPrefix, name, true), modelName, false), pa);
                } else {
                    walkProperties(swagger, parents, properties, fqn(namePrefix, name), nis(nisPrefix, name, true), pa);
                }
                parents.remove(property);
            }
        }

        @Override
        public void visit(String name, FileProperty property) {
        // TODO:
        }

        @Override
        public void visit(String name, MapProperty property) {
        // TODO:
        }

        @Override
        public void visit(String name, ObjectProperty property) {
            parents.add(property);
            walkProperties(swagger, parents, property.getProperties(), fqn(namePrefix, name), nis(nisPrefix, name, false), pa);
            parents.remove(property);
        }

        @Override
        public void visit(String name, RefProperty property) {
            parents.add(property);
            Model model = swagger.getDefinitions().get(property.getSimpleRef());
            walkProperties(swagger, parents, model.getProperties(), fqn(namePrefix, name), nis(nisPrefix, name, false), pa);
            parents.remove(property);
        }
    };
    for (Entry<String, Property> p : properties.entrySet()) {
        if (parents.contains(p.getValue())) {
            // TODO: could be an error condition
            continue;
        }
        visitor.accept(p.getKey(), p.getValue());
    }
}
Also used : ObjectProperty(io.swagger.models.properties.ObjectProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) FileProperty(io.swagger.models.properties.FileProperty) MapProperty(io.swagger.models.properties.MapProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) 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) RefProperty(io.swagger.models.properties.RefProperty)

Example 29 with MapProperty

use of io.swagger.models.properties.MapProperty in project swagger-parser by swagger-api.

the class ExternalRefProcessor method processRefToExternalDefinition.

public String processRefToExternalDefinition(String $ref, RefFormat refFormat) {
    String renamedRef = cache.getRenamedRef($ref);
    if (renamedRef != null) {
        return renamedRef;
    }
    final Model model = cache.loadRef($ref, refFormat, Model.class);
    if (model == null) {
        // stop!  There's a problem.  retain the original ref
        LOGGER.warn("unable to load model reference from `" + $ref + "`.  It may not be available " + "or the reference isn't a valid model schema");
        return $ref;
    }
    String newRef;
    Map<String, Model> definitions = swagger.getDefinitions();
    if (definitions == null) {
        definitions = new LinkedHashMap<>();
    }
    final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
    String tryName = null;
    Model existingModel = definitions.get(possiblyConflictingDefinitionName);
    if (existingModel != null) {
        LOGGER.debug("A model for " + existingModel + " already exists");
        if (existingModel instanceof RefModel) {
            // use the new model
            existingModel = null;
        } else {
            // We add a number at the end of the definition name
            int i = 2;
            for (String name : definitions.keySet()) {
                if (name.equals(possiblyConflictingDefinitionName)) {
                    tryName = possiblyConflictingDefinitionName + "_" + i;
                    existingModel = definitions.get(tryName);
                    i++;
                }
            }
        }
    }
    if (StringUtils.isNotBlank(tryName)) {
        newRef = tryName;
    } else {
        newRef = possiblyConflictingDefinitionName;
    }
    cache.putRenamedRef($ref, newRef);
    if (existingModel == null) {
        // don't overwrite existing model reference
        swagger.addDefinition(newRef, model);
        cache.addReferencedKey(newRef);
        String file = $ref.split("#/")[0];
        if (model instanceof RefModel) {
            RefModel refModel = (RefModel) model;
            if (isAnExternalRefFormat(refModel.getRefFormat())) {
                refModel.set$ref(processRefToExternalDefinition(refModel.get$ref(), refModel.getRefFormat()));
            } else {
                processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
            }
        }
        if (model instanceof ComposedModel) {
            ComposedModel composedModel = (ComposedModel) model;
            List<Model> listOfAllOF = composedModel.getAllOf();
            for (Model allOfModel : listOfAllOF) {
                if (allOfModel instanceof RefModel) {
                    RefModel refModel = (RefModel) allOfModel;
                    if (isAnExternalRefFormat(refModel.getRefFormat())) {
                        String joinedRef = join(file, refModel.get$ref());
                        refModel.set$ref(processRefToExternalDefinition(joinedRef, refModel.getRefFormat()));
                    } else {
                        processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
                    }
                } else if (allOfModel instanceof ModelImpl) {
                    // Loop through additional properties of allOf and recursively call this method;
                    processProperties(allOfModel.getProperties(), file);
                }
            }
        }
        // Loop the properties and recursively call this method;
        processProperties(model.getProperties(), file);
        if (model instanceof ModelImpl) {
            ModelImpl modelImpl = (ModelImpl) model;
            Property additionalProperties = modelImpl.getAdditionalProperties();
            if (additionalProperties != null) {
                if (additionalProperties instanceof RefProperty) {
                    processRefProperty(((RefProperty) additionalProperties), file);
                } else if (additionalProperties instanceof ArrayProperty) {
                    ArrayProperty arrayProp = (ArrayProperty) additionalProperties;
                    if (arrayProp.getItems() instanceof RefProperty) {
                        processRefProperty((RefProperty) arrayProp.getItems(), file);
                    }
                } else if (additionalProperties instanceof MapProperty) {
                    MapProperty mapProp = (MapProperty) additionalProperties;
                    if (mapProp.getAdditionalProperties() instanceof RefProperty) {
                        processRefProperty((RefProperty) mapProp.getAdditionalProperties(), file);
                    } else if (mapProp.getAdditionalProperties() instanceof ArrayProperty && ((ArrayProperty) mapProp.getAdditionalProperties()).getItems() instanceof RefProperty) {
                        processRefProperty((RefProperty) ((ArrayProperty) mapProp.getAdditionalProperties()).getItems(), file);
                    }
                }
            }
        }
        if (model instanceof ArrayModel && ((ArrayModel) model).getItems() instanceof RefProperty) {
            processRefProperty((RefProperty) ((ArrayModel) model).getItems(), file);
        }
    }
    return newRef;
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) RefProperty(io.swagger.models.properties.RefProperty) MapProperty(io.swagger.models.properties.MapProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) Property(io.swagger.models.properties.Property)

Example 30 with MapProperty

use of io.swagger.models.properties.MapProperty in project swagger-parser by swagger-api.

the class PropertyProcessorTest method testProcessMapProperty_AdditionalPropertiesIsRefProperty.

@Test
public void testProcessMapProperty_AdditionalPropertiesIsRefProperty() throws Exception {
    expectCreationOfExternalRefProcessor();
    final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
    final RefProperty refProperty = new RefProperty(ref);
    MapProperty mapProperty = new MapProperty();
    mapProperty.setAdditionalProperties(refProperty);
    expectCallToExternalRefProcessor(ref, RefFormat.URL, "bar");
    new PropertyProcessor(cache, swagger).processProperty(mapProperty);
    new FullVerifications() {

        {
        }
    };
    assertEquals(((RefProperty) mapProperty.getAdditionalProperties()).get$ref(), "#/definitions/bar");
}
Also used : MapProperty(io.swagger.models.properties.MapProperty) FullVerifications(mockit.FullVerifications) RefProperty(io.swagger.models.properties.RefProperty) Test(org.testng.annotations.Test)

Aggregations

MapProperty (io.swagger.models.properties.MapProperty)32 Property (io.swagger.models.properties.Property)26 ArrayProperty (io.swagger.models.properties.ArrayProperty)22 RefProperty (io.swagger.models.properties.RefProperty)16 Test (org.testng.annotations.Test)15 StringProperty (io.swagger.models.properties.StringProperty)13 Model (io.swagger.models.Model)12 IntegerProperty (io.swagger.models.properties.IntegerProperty)12 ObjectProperty (io.swagger.models.properties.ObjectProperty)8 RefModel (io.swagger.models.RefModel)7 ModelImpl (io.swagger.models.ModelImpl)6 JavaType (com.fasterxml.jackson.databind.JavaType)5 ApiModelProperty (io.swagger.annotations.ApiModelProperty)5 FileProperty (io.swagger.models.properties.FileProperty)5 LongProperty (io.swagger.models.properties.LongProperty)5 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)4 ComposedModel (io.swagger.models.ComposedModel)4 Operation (io.swagger.models.Operation)4 Response (io.swagger.models.Response)4 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)4