Search in sources :

Example 1 with CodegenParameter

use of io.swagger.codegen.CodegenParameter in project carbon-apimgt by wso2.

the class DynamicHtmlGenTestCase method testFromParameter.

@Test
public void testFromParameter() throws Exception {
    Parameter parameter = new QueryParameter();
    parameter.setName("query");
    final String description = "Sample parameter description";
    parameter.setDescription(description);
    DynamicHtmlGen htmlGen = new DynamicHtmlGen();
    CodegenParameter modified = htmlGen.fromParameter(parameter, new HashSet<>());
    Assert.assertEquals(modified.description, description);
}
Also used : CodegenParameter(io.swagger.codegen.CodegenParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) DynamicHtmlGen(org.wso2.carbon.apimgt.rest.api.common.codegen.DynamicHtmlGen) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) CodegenParameter(io.swagger.codegen.CodegenParameter) Test(org.testng.annotations.Test)

Example 2 with CodegenParameter

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

the class AbstractOktaJavaClientCodegen method addOptionalExtension.

private void addOptionalExtension(CodegenOperation co, List<CodegenParameter> params) {
    if (params.parallelStream().anyMatch(param -> !param.required)) {
        co.vendorExtensions.put("hasOptional", true);
        List<CodegenParameter> nonOptionalParams = params.stream().filter(param -> param.required).map(CodegenParameter::copy).collect(Collectors.toList());
        if (!nonOptionalParams.isEmpty()) {
            CodegenParameter param = nonOptionalParams.get(nonOptionalParams.size() - 1);
            param.hasMore = false;
            co.vendorExtensions.put("nonOptionalParams", nonOptionalParams);
        }
        // remove the noOptionalParams if we have trimmed down the list.
        if (co.vendorExtensions.get("nonOptionalParams") != null && nonOptionalParams.isEmpty()) {
            co.vendorExtensions.remove("nonOptionalParams");
        }
        // remove th body parameter if it was optional
        if (co.bodyParam != null && !co.bodyParam.required) {
            co.vendorExtensions.put("optionalBody", true);
        }
    }
}
Also used : CodegenParameter(io.swagger.codegen.CodegenParameter)

Example 3 with CodegenParameter

use of io.swagger.codegen.CodegenParameter in project carbon-apimgt by wso2.

the class DynamicHtmlGen method fromParameter.

@Override
public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
    CodegenParameter parameter = super.fromParameter(param, imports);
    parameter.description = param.getDescription();
    return parameter;
}
Also used : CodegenParameter(io.swagger.codegen.CodegenParameter)

Example 4 with CodegenParameter

use of io.swagger.codegen.CodegenParameter in project carbon-apimgt by wso2.

the class DynamicHtmlGen method fromParameter.

@Override
public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
    CodegenParameter parameter = super.fromParameter(param, imports);
    parameter.description = param.getDescription();
    return parameter;
}
Also used : CodegenParameter(io.swagger.codegen.CodegenParameter)

Example 5 with CodegenParameter

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

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