Skip to content

Commit 769a65c

Browse files
lundalwing328
authored andcommitted
[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
1 parent 56a0268 commit 769a65c

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ElmClientCodegen.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import io.swagger.codegen.CodegenType;
1010
import io.swagger.codegen.DefaultCodegen;
1111
import io.swagger.codegen.SupportingFile;
12+
import io.swagger.models.ArrayModel;
13+
import io.swagger.models.Model;
1214
import io.swagger.models.Response;
1315
import io.swagger.models.parameters.Parameter;
1416
import io.swagger.models.properties.ArrayProperty;
@@ -103,6 +105,7 @@ public ElmClientCodegen() {
103105
);
104106

105107
instantiationTypes.clear();
108+
instantiationTypes.put("array", "List");
106109

107110
typeMapping.clear();
108111
typeMapping.put("integer", "Int");
@@ -181,6 +184,17 @@ public String toEnumVarName(String value, String datatype) {
181184
return camelized;
182185
}
183186

187+
@Override
188+
public String toInstantiationType(Property p) {
189+
if (p instanceof ArrayProperty) {
190+
ArrayProperty ap = (ArrayProperty) p;
191+
String inner = getSwaggerType(ap.getItems());
192+
return instantiationTypes.get("array") + " " + inner;
193+
} else {
194+
return null;
195+
}
196+
}
197+
184198
@Override
185199
public String escapeReservedWord(String name) {
186200
return name + "_";
@@ -196,6 +210,20 @@ public String modelFileFolder() {
196210
return outputFolder + "/src/Data/" + modelPackage().replace('.', File.separatorChar);
197211
}
198212

213+
@Override
214+
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
215+
CodegenModel m = super.fromModel(name, model, allDefinitions);
216+
217+
if (model instanceof ArrayModel) {
218+
ArrayModel am = (ArrayModel) model;
219+
ArrayProperty arrayProperty = new ArrayProperty(am.getItems());
220+
CodegenProperty codegenProperty = fromProperty(name, arrayProperty);
221+
m.vendorExtensions.putAll(codegenProperty.vendorExtensions);
222+
}
223+
224+
return m;
225+
}
226+
199227
@SuppressWarnings({ "static-method", "unchecked" })
200228
public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
201229
// Index all CodegenModels by model name.
@@ -245,6 +273,20 @@ public int compare(CodegenModel cm1, CodegenModel cm2) {
245273
elmImports.add(createPropertyImport(property));
246274
}
247275
}
276+
if (cm.isArrayModel) {
277+
if (cm.arrayModelType != null) {
278+
// add type imports
279+
final ElmImport elmImport = new ElmImport();
280+
final String modulePrefix = customPrimitives.contains(cm.arrayModelType) ? "" : "Data.";
281+
elmImport.moduleName = modulePrefix + cm.arrayModelType;
282+
elmImport.exposures = new TreeSet<>();
283+
elmImport.exposures.add(cm.arrayModelType);
284+
elmImport.exposures.add(camelize(cm.arrayModelType, true) + "Decoder");
285+
elmImport.exposures.add(camelize(cm.arrayModelType, true) + "Encoder");
286+
elmImport.hasExposures = true;
287+
elmImports.add(elmImport);
288+
}
289+
}
248290
if (cm.discriminator != null) {
249291
for (CodegenModel child : cm.children) {
250292
// add child imports
@@ -418,7 +460,7 @@ public String toDefaultValue(Property p) {
418460
return toOptionalValue(null);
419461
}
420462
}
421-
463+
422464
private String toOptionalValue(String value) {
423465
if (value == null) {
424466
return "Nothing";
@@ -508,4 +550,4 @@ private static class ElmImport {
508550
public Set<String> exposures;
509551
public Boolean hasExposures;
510552
}
511-
}
553+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{classVarName}}Decoder : Decoder {{classname}}
22
{{classVarName}}Decoder =
3-
decode {{classname}}
3+
{{#parent}}Decode.list {{vendorExtensions.x-decoder}}{{/parent}}{{^parent}}decode {{classname}}
44
{{#allVars}}{{^discriminatorValue}} |> {{>fieldDecoder}}
5-
{{/discriminatorValue}}{{/allVars}}
5+
{{/discriminatorValue}}{{/allVars}}{{/parent}}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{classVarName}}Encoder : {{classname}} -> Encode.Value
22
{{classVarName}}Encoder model =
3-
Encode.object
3+
{{#parent}}Encode.list (List.map {{vendorExtensions.x-encoder}} model){{/parent}}{{^parent}}Encode.object
44
{{#allVars}}
55
{{#-first}}[{{/-first}}{{^-first}},{{/-first}} {{>fieldEncoder}}
66
{{/allVars}}
7-
]
7+
]{{/parent}}

modules/swagger-codegen/src/main/resources/elm/modelTypeAlias.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
type alias {{classname}} =
3+
type alias {{classname}} ={{#parent}} {{parent}}{{/parent}}{{^parent}}
44
{ {{#vars}}{{^-first}} , {{/-first}}{{name}} : {{^required}}Maybe {{/required}}{{#isContainer}}(List {{/isContainer}}{{#isEnum}}{{nameInCamelCase}}{{/isEnum}}{{^isEnum}}{{datatype}}{{/isEnum}}{{#isContainer}}){{/isContainer}}
55
{{/vars}} }
66
{{#vars}}
@@ -10,6 +10,7 @@ type alias {{classname}} =
1010
{{>union}}
1111
{{/isEnum}}
1212
{{/vars}}
13+
{{/parent}}
1314

1415

1516
{{>aliasDecoder}}

0 commit comments

Comments
 (0)