Skip to content

Commit

Permalink
[Elm] Add support for array schemas (#7729)
Browse files Browse the repository at this point in the history
The following schema definitions kinds are now supported:

MyStringArray:
  type: array
  items:
    type: string

MyObjectArray:
  type: array
  items:
    type: MyObject
  • Loading branch information
lundal authored and wing328 committed Feb 27, 2018
1 parent 56a0268 commit 769a65c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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;
Expand Down Expand Up @@ -103,6 +105,7 @@ public ElmClientCodegen() {
);

instantiationTypes.clear();
instantiationTypes.put("array", "List");

typeMapping.clear();
typeMapping.put("integer", "Int");
Expand Down Expand Up @@ -181,6 +184,17 @@ public String toEnumVarName(String value, String datatype) {
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 + "_";
Expand All @@ -196,6 +210,20 @@ public String modelFileFolder() {
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.
Expand Down Expand Up @@ -245,6 +273,20 @@ public int compare(CodegenModel cm1, CodegenModel cm2) {
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
Expand Down Expand Up @@ -418,7 +460,7 @@ public String toDefaultValue(Property p) {
return toOptionalValue(null);
}
}

private String toOptionalValue(String value) {
if (value == null) {
return "Nothing";
Expand Down Expand Up @@ -508,4 +550,4 @@ private static class ElmImport {
public Set<String> exposures;
public Boolean hasExposures;
}
}
}
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
@@ -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}}
Expand All @@ -10,6 +10,7 @@ type alias {{classname}} =
{{>union}}
{{/isEnum}}
{{/vars}}
{{/parent}}


{{>aliasDecoder}}
Expand Down

0 comments on commit 769a65c

Please sign in to comment.