Search in sources :

Example 1 with CodegenProperty

use of io.swagger.codegen.CodegenProperty 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 CodegenProperty

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

the class AbstractOktaJavaClientCodegen method handleOktaLinkedOperations.

private void handleOktaLinkedOperations(Swagger swagger) {
    // we want to move any operations defined by the 'x-okta-operations' or 'x-okta-crud' vendor extension to the model
    Map<String, Model> modelMap = swagger.getDefinitions().entrySet().stream().filter(e -> e.getValue().getVendorExtensions().containsKey("x-okta-operations") || e.getValue().getVendorExtensions().containsKey("x-okta-crud")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    modelMap.forEach((k, model) -> {
        List<ObjectNode> linkNodes = new ArrayList<>();
        addAllIfNotNull(linkNodes, (List<ObjectNode>) model.getVendorExtensions().get("x-okta-operations"));
        addAllIfNotNull(linkNodes, (List<ObjectNode>) model.getVendorExtensions().get("x-okta-crud"));
        Map<String, CodegenOperation> operationMap = new HashMap<>();
        linkNodes.forEach(n -> {
            String operationId = n.get("operationId").textValue();
            // find the swagger path operation
            swagger.getPaths().forEach((pathName, path) -> {
                Optional<Map.Entry<HttpMethod, Operation>> operationEntry = path.getOperationMap().entrySet().stream().filter(e -> e.getValue().getOperationId().equals(operationId)).findFirst();
                if (operationEntry.isPresent()) {
                    Operation operation = operationEntry.get().getValue();
                    CodegenOperation cgOperation = fromOperation(pathName, operationEntry.get().getKey().name().toLowerCase(), operation, swagger.getDefinitions(), swagger);
                    boolean canLinkMethod = true;
                    JsonNode aliasNode = n.get("alias");
                    if (aliasNode != null) {
                        String alias = aliasNode.textValue();
                        cgOperation.vendorExtensions.put("alias", alias);
                        if ("update".equals(alias)) {
                            model.getVendorExtensions().put("saveable", true);
                        } else if ("delete".equals(alias)) {
                            model.getVendorExtensions().put("deletable", true);
                            cgOperation.vendorExtensions.put("selfDelete", true);
                        } else if ("read".equals(alias) || "create".equals(alias)) {
                            canLinkMethod = false;
                        }
                    }
                    // we do NOT link read or create methods, those need to be on the parent object
                    if (canLinkMethod) {
                        // now any params that match the models we need to use the model value directly
                        // for example if the path contained {id} we would call getId() instead
                        Map<String, String> argMap = createArgMap(n);
                        List<CodegenParameter> cgOtherPathParamList = new ArrayList<>();
                        List<CodegenParameter> cgParamAllList = new ArrayList<>();
                        List<CodegenParameter> cgParamModelList = new ArrayList<>();
                        cgOperation.pathParams.forEach(param -> {
                            if (argMap.containsKey(param.paramName)) {
                                String paramName = argMap.get(param.paramName);
                                cgParamModelList.add(param);
                                if (model.getProperties() != null) {
                                    CodegenProperty cgProperty = fromProperty(paramName, model.getProperties().get(paramName));
                                    param.vendorExtensions.put("fromModel", cgProperty);
                                } else {
                                    System.err.println("Model '" + model.getTitle() + "' has no properties");
                                }
                            } else {
                                cgOtherPathParamList.add(param);
                            }
                        });
                        // remove the body param if the body is the object itself
                        for (Iterator<CodegenParameter> iter = cgOperation.bodyParams.iterator(); iter.hasNext(); ) {
                            CodegenParameter bodyParam = iter.next();
                            if (argMap.containsKey(bodyParam.paramName)) {
                                cgOperation.vendorExtensions.put("bodyIsSelf", true);
                                iter.remove();
                            }
                        }
                        // do not add the parrent path params to the list (they will be parsed from the href)
                        SortedSet<String> pathParents = parentPathParams(n);
                        cgOtherPathParamList.forEach(param -> {
                            if (!pathParents.contains(param.paramName)) {
                                cgParamAllList.add(param);
                            }
                        });
                        if (!pathParents.isEmpty()) {
                            cgOperation.vendorExtensions.put("hasPathParents", true);
                            cgOperation.vendorExtensions.put("pathParents", pathParents);
                        }
                        cgParamAllList.addAll(cgOperation.queryParams);
                        cgParamAllList.addAll(cgOperation.bodyParams);
                        // set all params to have more
                        cgParamAllList.forEach(param -> param.hasMore = true);
                        // then grab the last one and mark it as the last
                        if (!cgParamAllList.isEmpty()) {
                            CodegenParameter param = cgParamAllList.get(cgParamAllList.size() - 1);
                            param.hasMore = false;
                        }
                        cgOperation.vendorExtensions.put("allParams", cgParamAllList);
                        cgOperation.vendorExtensions.put("fromModelPathParams", cgParamModelList);
                        addOptionalExtension(cgOperation, cgParamAllList);
                        operationMap.put(cgOperation.operationId, cgOperation);
                        // mark the operation as moved so we do NOT add it to the client
                        operation.getVendorExtensions().put("moved", true);
                    }
                }
            });
        });
        model.getVendorExtensions().put("operations", operationMap.values());
    });
}
Also used : 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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.swagger.models.Operation) CodegenOperation(io.swagger.codegen.CodegenOperation) CodegenOperation(io.swagger.codegen.CodegenOperation) CodegenParameter(io.swagger.codegen.CodegenParameter) CodegenProperty(io.swagger.codegen.CodegenProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) CodegenModel(io.swagger.codegen.CodegenModel) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 CodegenModel (io.swagger.codegen.CodegenModel)2 CodegenOperation (io.swagger.codegen.CodegenOperation)2 CodegenParameter (io.swagger.codegen.CodegenParameter)2 CodegenProperty (io.swagger.codegen.CodegenProperty)2 CodegenType (io.swagger.codegen.CodegenType)2 AbstractJavaCodegen (io.swagger.codegen.languages.AbstractJavaCodegen)2 HttpMethod (io.swagger.models.HttpMethod)2 Model (io.swagger.models.Model)2 ModelImpl (io.swagger.models.ModelImpl)2 Operation (io.swagger.models.Operation)2 Path (io.swagger.models.Path)2 RefModel (io.swagger.models.RefModel)2 Response (io.swagger.models.Response)2 Swagger (io.swagger.models.Swagger)2 BodyParameter (io.swagger.models.parameters.BodyParameter)2 ArrayProperty (io.swagger.models.properties.ArrayProperty)2