From e2fa6fb83500268cf52ef4c2ff68604c908aa74b Mon Sep 17 00:00:00 2001 From: Stephane Carrez Date: Sun, 3 Jun 2018 18:43:21 +0200 Subject: [PATCH] Fix #210: [Ada] Update the code generator for required and optional parameters - Do not check for required parameter in getTypeDeclaration() - Update the nullableTypeMapping map to map an Ada type into a corresponding Nullable_ Ada type that allows the value to be null - Check for optional parameter in postProccessOperations() and postProcessModels() to replace the Ada type into an Ada type that supports null values --- .../codegen/languages/AbstractAdaCodegen.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java index 6af50d145823..12abb58094ee 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java @@ -51,9 +51,9 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg protected String packageName = "defaultPackage"; protected String projectName = "defaultProject"; protected List> orderedModels; - protected Map> modelDepends; - protected Map nullableTypeMapping; - protected HashMap operationsScopes; + protected final Map> modelDepends; + protected final Map nullableTypeMapping; + protected final HashMap operationsScopes; protected int scopeIndex = 0; public AbstractAdaCodegen() { @@ -153,14 +153,15 @@ public AbstractAdaCodegen() { typeMapping.put("file", "Swagger.Http_Content_Type"); typeMapping.put("binary", "Swagger.Binary"); + // Mapping to convert an Ada required type to an optional type (nullable). nullableTypeMapping = new HashMap(); - nullableTypeMapping.put("date", "Swagger.Nullable_Date"); - nullableTypeMapping.put("DateTime", "Swagger.Nullable_Date"); - nullableTypeMapping.put("string", "Swagger.Nullable_UString"); - nullableTypeMapping.put("integer", "Swagger.Nullable_Integer"); - nullableTypeMapping.put("long", "Swagger.Nullable_Long"); - nullableTypeMapping.put("boolean", "Swagger.Nullable_Boolean"); - nullableTypeMapping.put("object", "Swagger.Object"); + nullableTypeMapping.put("Swagger.Date", "Swagger.Nullable_Date"); + nullableTypeMapping.put("Swagger.Datetime", "Swagger.Nullable_Date"); + nullableTypeMapping.put("Swagger.UString", "Swagger.Nullable_UString"); + nullableTypeMapping.put("Integer", "Swagger.Nullable_Integer"); + nullableTypeMapping.put("Swagger.Long", "Swagger.Nullable_Long"); + nullableTypeMapping.put("Boolean", "Swagger.Nullable_Boolean"); + nullableTypeMapping.put("Swagger.Object", "Swagger.Object"); modelDepends = new HashMap>(); orderedModels = new ArrayList>(); @@ -336,13 +337,8 @@ public String getTypeDeclaration(Schema p) { return "Swagger." + name; } } - // TODO need to revise/test the logic below to check "required" if (typeMapping.containsKey(schemaType)) { - if (p.getRequired() != null && p.getRequired().contains(p.getName())) { - return typeMapping.get(schemaType); - } else { - return nullableTypeMapping.get(schemaType); - } + return typeMapping.get(schemaType); } // LOGGER.info("Swagger type " + schemaType); if (languageSpecificPrimitives.contains(schemaType)) { @@ -443,6 +439,10 @@ public Map postProcessOperations(Map objs) { if (p.isFormParam && p.isFile) { p.dataType = "Swagger.File_Part_Type"; } + // Convert optional parameters to use the Nullable_ type. + if (!p.required && nullableTypeMapping.containsKey(p.dataType)) { + p.dataType = nullableTypeMapping.get(p.dataType); + } } for (CodegenParameter p : op1.formParams) { if (p.isFile) { @@ -471,7 +471,7 @@ public Map postProcessOperations(Map objs) { if (last < 0) { break; } - if (path.substring(pos, last - 1) == p.baseName) { + if (path.substring(pos, last - 1).equals(p.baseName)) { break; } pos = last + 1; @@ -505,6 +505,11 @@ public Map postProcessModels(Map objs) { isModel = true; } p.vendorExtensions.put("x-is-model-type", isModel); + + // Convert optional members to use the Nullable_ type. + if (!p.required && nullableTypeMapping.containsKey(p.dataType)) { + p.dataType = nullableTypeMapping.get(p.dataType); + } } // let us work with fully qualified names only modelDepends.put(modelPackage + ".Models." + m.classname, d); @@ -518,7 +523,7 @@ public Map postProcessModels(Map objs) { // cycle over orderedModels // if I find a model that has no dependencies, or all of its dependencies are in revisedOrderedModels, consider it the independentModel // put the independentModel at the end of revisedOrderedModels, and remove it from orderedModels - // + // List> revisedOrderedModels = new ArrayList>(); List collectedModelNames = new ArrayList(); int sizeOrderedModels = orderedModels.size();