Search in sources :

Example 1 with CodegenModel

use of io.swagger.codegen.CodegenModel in project okta-sdk-java by okta.

the class AbstractOktaJavaClientCodegen method fromModel.

@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
    CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
    // super add these imports, and we don't want that dependency
    codegenModel.imports.remove("ApiModel");
    if (model.getVendorExtensions().containsKey("x-baseType")) {
        String baseType = (String) model.getVendorExtensions().get("x-baseType");
        codegenModel.vendorExtensions.put("baseType", toModelName(baseType));
        codegenModel.imports.add(toModelName(baseType));
    }
    Collection<CodegenOperation> operations = (Collection<CodegenOperation>) codegenModel.vendorExtensions.get("operations");
    if (operations != null) {
        operations.forEach(op -> {
            if (op.returnType != null) {
                codegenModel.imports.add(op.returnType);
            }
            if (op.allParams != null) {
                op.allParams.stream().filter(param -> needToImport(param.dataType)).forEach(param -> codegenModel.imports.add(param.dataType));
            }
        });
    }
    // force alias == false (likely only relevant for Lists, but something changed in swagger 2.2.3 to require this)
    codegenModel.isAlias = false;
    String parent = (String) model.getVendorExtensions().get("x-okta-parent");
    if (StringUtils.isNotEmpty(parent)) {
        codegenModel.parent = toApiName(parent.substring(parent.lastIndexOf("/")));
        // figure out the resourceClass if this model has a parent
        String discriminatorRoot = getRootDiscriminator(name);
        if (discriminatorRoot != null) {
            model.getVendorExtensions().put("discriminatorRoot", discriminatorRoot);
        }
    }
    // We use '$ref' attributes with siblings, which isn't valid JSON schema (or swagger), so we need process
    // additional attributes from the raw schema
    Map<String, Object> modelDef = getRawSwaggerDefinition(name);
    codegenModel.vars.forEach(codegenProperty -> {
        Map<String, Object> rawPropertyMap = getRawSwaggerProperty(modelDef, codegenProperty.baseName);
        codegenProperty.isReadOnly = Boolean.TRUE.equals(rawPropertyMap.get("readOnly"));
    });
    return codegenModel;
}
Also used : CodegenOperation(io.swagger.codegen.CodegenOperation) SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) ModelImpl(io.swagger.models.ModelImpl) ArrayProperty(io.swagger.models.properties.ArrayProperty) StringUtils(org.apache.commons.lang3.StringUtils) Model(io.swagger.models.Model) Path(io.swagger.models.Path) Locale(java.util.Locale) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.swagger.models.Operation) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) Collection(java.util.Collection) Set(java.util.Set) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Response(io.swagger.models.Response) List(java.util.List) Optional(java.util.Optional) CodegenParameter(io.swagger.codegen.CodegenParameter) AbstractJavaCodegen(io.swagger.codegen.languages.AbstractJavaCodegen) Swagger(io.swagger.models.Swagger) BooleanUtils(org.apache.commons.lang3.BooleanUtils) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TreeSet(java.util.TreeSet) Yaml(org.yaml.snakeyaml.Yaml) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CodegenProperty(io.swagger.codegen.CodegenProperty) RefProperty(io.swagger.models.properties.RefProperty) Logger(org.slf4j.Logger) CodegenOperation(io.swagger.codegen.CodegenOperation) BodyParameter(io.swagger.models.parameters.BodyParameter) Iterator(java.util.Iterator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CodegenType(io.swagger.codegen.CodegenType) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) SwaggerException(io.swagger.parser.SwaggerException) CodegenModel(io.swagger.codegen.CodegenModel) HttpMethod(io.swagger.models.HttpMethod) Collections(java.util.Collections) Collection(java.util.Collection) CodegenModel(io.swagger.codegen.CodegenModel)

Example 2 with CodegenModel

use of io.swagger.codegen.CodegenModel in project okta-sdk-java by okta.

the class AbstractOktaJavaClientCodegen method postProcessModelsEnum.

@Override
public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
    objs = super.postProcessModelsEnum(objs);
    // Needed import for Gson based libraries
    if (additionalProperties.containsKey("gson")) {
        List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
        List<Object> models = (List<Object>) objs.get("models");
        for (Object _mo : models) {
            Map<String, Object> mo = (Map<String, Object>) _mo;
            CodegenModel cm = (CodegenModel) mo.get("model");
            // for enum model
            if (Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null) {
                cm.imports.add(importMapping.get("SerializedName"));
                Map<String, String> item = new HashMap<String, String>();
                item.put("import", importMapping.get("SerializedName"));
                imports.add(item);
            }
        }
    }
    return objs;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ArrayList(java.util.ArrayList) CodegenModel(io.swagger.codegen.CodegenModel) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with CodegenModel

use of io.swagger.codegen.CodegenModel in project okta-sdk-java by okta.

the class OktaJavaClientImplCodegen method fromModel.

@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
    CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
    // The 'Default' gets added in the template
    codegenModel.imports.add(toModelName(codegenModel.classname));
    Map<String, String> defaultValuesMap = new LinkedHashMap<>();
    ObjectNode rawDefaultValues = (ObjectNode) codegenModel.vendorExtensions.get("x-okta-defined-as");
    if (rawDefaultValues != null) {
        rawDefaultValues.fields().forEachRemaining(entry -> {
            defaultValuesMap.put(entry.getKey(), entry.getValue().textValue());
        });
    }
    // if the parent is set, we need to check for discrimination
    String parent = (String) codegenModel.vendorExtensions.get("x-okta-parent");
    if (parent != null) {
        parent = parent.substring(parent.lastIndexOf('/') + 1);
        if (discriminatorMap.containsKey(parent)) {
            Discriminator discriminator = discriminatorMap.get(parent);
            String fieldName = discriminator.getFieldName();
            String defaultValue = discriminator.getDefaultFieldValue(name);
            defaultValuesMap.put(fieldName, defaultValue);
        }
    }
    List<KeyValuePair> defaultTypeSetter = defaultValuesMap.entrySet().stream().map(entry -> new KeyValuePair(entry.getKey(), entry.getValue())).collect(Collectors.toList());
    codegenModel.vendorExtensions.put("defaultSetter", defaultTypeSetter);
    return codegenModel;
}
Also used : OutputStream(java.io.OutputStream) CodegenOperation(io.swagger.codegen.CodegenOperation) Swagger(io.swagger.models.Swagger) Collection(java.util.Collection) FileOutputStream(java.io.FileOutputStream) BooleanUtils(org.apache.commons.lang3.BooleanUtils) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Model(io.swagger.models.Model) Yaml(org.yaml.snakeyaml.Yaml) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) CodegenProperty(io.swagger.codegen.CodegenProperty) Map(java.util.Map) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) CodegenModel(io.swagger.codegen.CodegenModel) Operation(io.swagger.models.Operation) Collections(java.util.Collections) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CodegenModel(io.swagger.codegen.CodegenModel) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CodegenModel (io.swagger.codegen.CodegenModel)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CodegenOperation (io.swagger.codegen.CodegenOperation)2 CodegenProperty (io.swagger.codegen.CodegenProperty)2 Model (io.swagger.models.Model)2 Operation (io.swagger.models.Operation)2 Swagger (io.swagger.models.Swagger)2 File (java.io.File)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collectors (java.util.stream.Collectors)2 BooleanUtils (org.apache.commons.lang3.BooleanUtils)2 Yaml (org.yaml.snakeyaml.Yaml)2