[Elm] Add support for array schemas (#7729)

The following schema definitions kinds are now supported:

MyStringArray:
  type: array
  items:
    type: string

MyObjectArray:
  type: array
  items:
    type: MyObject
This commit is contained in:
Per Thomas Lundal 2018-02-27 10:58:12 +01:00 committed by William Cheng
parent 56a0268e39
commit 769a65c95f
4 changed files with 50 additions and 7 deletions

View File

@ -9,6 +9,8 @@ import io.swagger.codegen.CodegenResponse;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.Response;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.ArrayProperty;
@ -103,6 +105,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
);
instantiationTypes.clear();
instantiationTypes.put("array", "List");
typeMapping.clear();
typeMapping.put("integer", "Int");
@ -181,6 +184,17 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
return camelized;
}
@Override
public String toInstantiationType(Property p) {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
String inner = getSwaggerType(ap.getItems());
return instantiationTypes.get("array") + " " + inner;
} else {
return null;
}
}
@Override
public String escapeReservedWord(String name) {
return name + "_";
@ -196,6 +210,20 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
return outputFolder + "/src/Data/" + modelPackage().replace('.', File.separatorChar);
}
@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
CodegenModel m = super.fromModel(name, model, allDefinitions);
if (model instanceof ArrayModel) {
ArrayModel am = (ArrayModel) model;
ArrayProperty arrayProperty = new ArrayProperty(am.getItems());
CodegenProperty codegenProperty = fromProperty(name, arrayProperty);
m.vendorExtensions.putAll(codegenProperty.vendorExtensions);
}
return m;
}
@SuppressWarnings({ "static-method", "unchecked" })
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
// Index all CodegenModels by model name.
@ -245,6 +273,20 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
elmImports.add(createPropertyImport(property));
}
}
if (cm.isArrayModel) {
if (cm.arrayModelType != null) {
// add type imports
final ElmImport elmImport = new ElmImport();
final String modulePrefix = customPrimitives.contains(cm.arrayModelType) ? "" : "Data.";
elmImport.moduleName = modulePrefix + cm.arrayModelType;
elmImport.exposures = new TreeSet<>();
elmImport.exposures.add(cm.arrayModelType);
elmImport.exposures.add(camelize(cm.arrayModelType, true) + "Decoder");
elmImport.exposures.add(camelize(cm.arrayModelType, true) + "Encoder");
elmImport.hasExposures = true;
elmImports.add(elmImport);
}
}
if (cm.discriminator != null) {
for (CodegenModel child : cm.children) {
// add child imports
@ -418,7 +460,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
return toOptionalValue(null);
}
}
private String toOptionalValue(String value) {
if (value == null) {
return "Nothing";
@ -508,4 +550,4 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
public Set<String> exposures;
public Boolean hasExposures;
}
}
}

View File

@ -1,5 +1,5 @@
{{classVarName}}Decoder : Decoder {{classname}}
{{classVarName}}Decoder =
decode {{classname}}
{{#parent}}Decode.list {{vendorExtensions.x-decoder}}{{/parent}}{{^parent}}decode {{classname}}
{{#allVars}}{{^discriminatorValue}} |> {{>fieldDecoder}}
{{/discriminatorValue}}{{/allVars}}
{{/discriminatorValue}}{{/allVars}}{{/parent}}

View File

@ -1,7 +1,7 @@
{{classVarName}}Encoder : {{classname}} -> Encode.Value
{{classVarName}}Encoder model =
Encode.object
{{#parent}}Encode.list (List.map {{vendorExtensions.x-encoder}} model){{/parent}}{{^parent}}Encode.object
{{#allVars}}
{{#-first}}[{{/-first}}{{^-first}},{{/-first}} {{>fieldEncoder}}
{{/allVars}}
]
]{{/parent}}

View File

@ -1,6 +1,6 @@
type alias {{classname}} =
type alias {{classname}} ={{#parent}} {{parent}}{{/parent}}{{^parent}}
{ {{#vars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{/required}}{{#isContainer}}(List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{datatype}}{{/isEnum}}{{#isContainer}}){{/isContainer}}
{{/vars}} }
{{#vars}}
@ -10,6 +10,7 @@ type alias {{classname}} =
{{>union}}
{{/isEnum}}
{{/vars}}
{{/parent}}
{{>aliasDecoder}}