All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.models.reader.ApiDeclarationParser Maven / Gradle / Ivy

There is a newer version: 1.0.71
Show newest version
package io.swagger.models.reader;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.swagger.models.apideclaration.Api;
import io.swagger.models.apideclaration.ApiDeclaration;
import io.swagger.models.apideclaration.Model;
import io.swagger.models.apideclaration.ModelProperty;
import io.swagger.models.apideclaration.Operation;
import io.swagger.models.apideclaration.Parameter;
import io.swagger.report.Message;
import io.swagger.report.MessageBuilder;
import io.swagger.report.Severity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiDeclarationParser extends SwaggerParser {

    public ApiDeclaration read(String json, MessageBuilder messages) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.setSerializationInclusion(Include.NON_NULL);

        try {
            Map m = mapper.readValue(json, Map.class);
            ApiDeclaration api = new ApiDeclaration();

            String apiVersion = readString(m.get("apiVersion"));
            if (apiVersion != null) {
                api.setApiVersion(apiVersion);
            } else {
                messages.append(new Message("ApiDeclaration.apiVersion", "apiVersion is missing", Severity.RECOMMENDED));
            }

            String swaggerVersion = readString(m.get("swaggerVersion"));
            if (swaggerVersion != null) {
                api.setSwaggerVersion(swaggerVersion);
            } else {
                messages.append(new Message("ApiDeclaration.swaggerVersion", "swaggerVersion is missing", Severity.ERROR));
            }

            String basePath = readString(m.get("basePath"));
            if (basePath != null) {
                api.setBasePath(basePath);
            } else {
                messages.append(new Message("ApiDeclaration.basePath", "basePath is missing", Severity.ERROR));
            }

            String resourcePath = readString(m.get("resourcePath"));
            if (resourcePath != null) {
                api.setResourcePath(resourcePath);
            } else {
                messages.append(new Message("ApiDeclaration.resourcePath", "resourcePath is missing", Severity.ERROR));
            }

            String produces = readString(m.get("produces"));
            Object apis = m.get("apis");
            if (apis != null) {
                List o = readApis((List>) apis, messages);
                if (o.size() > 0) {
                    api.setApis(o);
                }
            }

            Object models = m.get("models");
            if (models != null) {
                Map modelMap = readModels((Map) models, messages);
                api.setModels(modelMap);
            }

            return api;
        } catch (Exception e) {
            messages.append(new Message("ApiDeclaration", "invalid json", Severity.ERROR));
            return null;
        }
    }

    Map readModels(Map o, MessageBuilder messages) {
        Map output = new HashMap();

        for (String modelName : o.keySet()) {
            Model model = new Model();
            Map value = (Map) o.get(modelName);
            String id = readString(value.get("id"));
            model.setId(id);

            String description = readString(value.get("description"));
            model.setDescription(description);


            String discriminator = readString(value.get("discriminator"));
            model.setDiscriminator(discriminator);

            Object properties = value.get("properties");
            if (properties != null) {
            }
            output.put(modelName, model);
        }

        return output;
    }

    List readProperties(Map properties, MessageBuilder messages) {
        List output = new ArrayList();

        for (String key : properties.keySet()) {
            Map value = (Map) properties.get(key);
            ModelProperty prop = new ModelProperty();

            String type = readString(value.get("type"));
            prop.setType(type);

            String qualifiedType = readString(value.get("qualifiedType"));

            output.add(prop);
        }

        return output;
    }

    List readApis(List> om, MessageBuilder messages) {
        List output = new ArrayList();
        for (Map o : om) {
            Api op = new Api();
            String path = readString(o.get("path"));
            if (path != null) {
                op.setPath(path);
            } else {
                messages.append(new Message("ApiDeclaration.apis", "path is missing", Severity.ERROR));
            }

            Object operations = o.get("operations");

            if (operations instanceof List) {
                List ops = readOperations((List>) operations, messages);
                op.setOperations(ops);
            }

            output.add(op);
        }
        return output;
    }

    List readOperations(List> ops, MessageBuilder messages) {
        return new ArrayList();
    }

    List readParameters(List> o, MessageBuilder messages) {
        List output = new ArrayList();
        for (Map p : o) {
            Parameter param = readParameter(p, messages);
            if (param != null) {
                output.add(param);
            }
        }
        return output;
    }

    Parameter readParameter(Map o, MessageBuilder messages) {
        Parameter param = new Parameter();

        String name = readString(o.get("name"));
        if (name != null) {
            param.setName(name);
        } else {
            messages.append(new Message("ApiDeclaration.apis.operations.parameters.name", "missing name", Severity.ERROR));
        }
        String description = readString(o.get("description"));
        param.setDescription(description);

        Boolean required = readBoolean(o.get("required"));
        param.setRequired(required);

        String type = readString(o.get("type"));
        param.setType(type);

        Boolean allowMultiple = readBoolean(o.get("allowMultiple"));
        param.setAllowMultiple(allowMultiple);

        return param;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy