@@ -180,44 +180,55 @@ public ElixirClientCodegen() {
180
180
*/
181
181
languageSpecificPrimitives = new HashSet <>(
182
182
Arrays .asList (
183
- "Integer" ,
184
- "Float" ,
185
- "Decimal" ,
186
- "Boolean" ,
187
- "String" ,
188
- "List" ,
189
- "Atom" ,
190
- "Map" ,
191
- "AnyType" ,
192
- "Tuple" ,
193
- "PID" ,
194
- // This is a workaround, since the DefaultCodeGen uses our elixir TypeSpec
195
- // datetype to evaluate the primitive
183
+ "integer()" ,
184
+ "float()" ,
185
+ "number()" ,
186
+ "boolean()" ,
187
+ "String.t" ,
188
+ "Date.t" ,
189
+ "DateTime.t" ,
190
+ "binary()" ,
191
+ "list()" ,
196
192
"map()" ,
197
- "any()" ));
193
+ "any()" ,
194
+ "nil" ));
198
195
199
196
// ref:
200
197
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
201
198
typeMapping = new HashMap <>();
202
- typeMapping .put ("integer" , "Integer" );
203
- typeMapping .put ("long" , "Integer" );
204
- typeMapping .put ("number" , "Float" );
205
- typeMapping .put ("float" , "Float" );
206
- typeMapping .put ("double" , "Float" );
207
- typeMapping .put ("string" , "String" );
208
- typeMapping .put ("byte" , "Integer" );
209
- typeMapping .put ("boolean" , "Boolean" );
210
- typeMapping .put ("Date" , "Date" );
211
- typeMapping .put ("DateTime" , "DateTime" );
212
- typeMapping .put ("file" , "String" );
213
- typeMapping .put ("map" , "Map" );
214
- typeMapping .put ("array" , "List" );
215
- typeMapping .put ("list" , "List" );
216
- typeMapping .put ("object" , "Map" );
217
- typeMapping .put ("binary" , "String" );
218
- typeMapping .put ("ByteArray" , "String" );
219
- typeMapping .put ("UUID" , "String" );
220
- typeMapping .put ("URI" , "String" );
199
+ // primitive types
200
+ typeMapping .put ("string" , "String.t" );
201
+ typeMapping .put ("number" , "number()" );
202
+ typeMapping .put ("integer" , "integer()" );
203
+ typeMapping .put ("boolean" , "boolean()" );
204
+ typeMapping .put ("array" , "list()" );
205
+ typeMapping .put ("object" , "map()" );
206
+ typeMapping .put ("map" , "map()" );
207
+ typeMapping .put ("null" , "nil" );
208
+ // string formats
209
+ typeMapping .put ("byte" , "String.t" );
210
+ typeMapping .put ("binary" , "binary()" );
211
+ typeMapping .put ("password" , "String.t" );
212
+ typeMapping .put ("uuid" , "String.t" );
213
+ typeMapping .put ("email" , "String.t" );
214
+ typeMapping .put ("uri" , "String.t" );
215
+ typeMapping .put ("file" , "String.t" );
216
+ // integer formats
217
+ typeMapping .put ("int32" , "integer()" );
218
+ typeMapping .put ("int64" , "integer()" );
219
+ typeMapping .put ("long" , "integer()" );
220
+ // float formats
221
+ typeMapping .put ("float" , "float()" );
222
+ typeMapping .put ("double" , "float()" );
223
+ typeMapping .put ("decimal" , "float()" );
224
+ // date-time formats
225
+ typeMapping .put ("date" , "Date.t" );
226
+ typeMapping .put ("date-time" , "DateTime.t" );
227
+ // other
228
+ typeMapping .put ("ByteArray" , "binary()" );
229
+ typeMapping .put ("DateTime" , "DateTime.t" );
230
+ typeMapping .put ("UUID" , "String.t" );
231
+
221
232
222
233
cliOptions .add (new CliOption (CodegenConstants .INVOKER_PACKAGE ,
223
234
"The main namespace to use for all classes. e.g. Yay.Pets" ));
@@ -570,49 +581,19 @@ public String toOperationId(String operationId) {
570
581
*/
571
582
@ Override
572
583
public String getTypeDeclaration (Schema p ) {
573
- if (ModelUtils .isAnyType (p )) {
574
- return "any()" ;
575
- } else if (ModelUtils .isFreeFormObject (p , null )) {
576
- return "%{optional(String.t) => any()}" ;
577
- } else if (ModelUtils .isArraySchema (p )) {
584
+ if (ModelUtils .isArraySchema (p )) {
578
585
Schema inner = ModelUtils .getSchemaItems (p );
579
586
return "[" + getTypeDeclaration (inner ) + "]" ;
580
587
} else if (ModelUtils .isMapSchema (p )) {
581
588
Schema inner = ModelUtils .getAdditionalProperties (p );
582
589
return "%{optional(String.t) => " + getTypeDeclaration (inner ) + "}" ;
583
- } else if (ModelUtils .isPasswordSchema (p )) {
584
- return "String.t" ;
585
- } else if (ModelUtils .isEmailSchema (p )) {
586
- return "String.t" ;
587
- } else if (ModelUtils .isByteArraySchema (p )) {
588
- return "binary()" ;
589
- } else if (ModelUtils .isUUIDSchema (p )) {
590
- return "String.t" ;
591
- } else if (ModelUtils .isDateSchema (p )) {
592
- return "Date.t" ;
593
- } else if (ModelUtils .isDateTimeSchema (p )) {
594
- return "DateTime.t" ;
595
- } else if (ModelUtils .isObjectSchema (p )) {
596
- return "map()" ;
597
- } else if (ModelUtils .isIntegerSchema (p )) {
598
- return "integer()" ;
599
- } else if (ModelUtils .isNumberSchema (p )) {
600
- return "float()" ;
601
- } else if (ModelUtils .isBinarySchema (p ) || ModelUtils .isFileSchema (p )) {
602
- return "String.t" ;
603
- } else if (ModelUtils .isBooleanSchema (p )) {
604
- return "boolean()" ;
605
590
} else if (!StringUtils .isEmpty (p .get$ref ())) {
606
- switch ( super .getTypeDeclaration (p )) {
607
- case "String" :
608
- return "String.t" ;
609
- default :
610
- return this .moduleName + ".Model." + super . getTypeDeclaration ( p ) + ".t" ;
591
+ String refType = super .getTypeDeclaration (p );
592
+ if ( languageSpecificPrimitives . contains ( refType )) {
593
+ return refType ;
594
+ } else {
595
+ return this .moduleName + ".Model." + refType + ".t" ;
611
596
}
612
- } else if (ModelUtils .isFileSchema (p )) {
613
- return "String.t" ;
614
- } else if (ModelUtils .isStringSchema (p )) {
615
- return "String.t" ;
616
597
} else if (p .getType () == null ) {
617
598
return "any()" ;
618
599
}
@@ -630,14 +611,11 @@ public String getTypeDeclaration(Schema p) {
630
611
@ Override
631
612
public String getSchemaType (Schema p ) {
632
613
String openAPIType = super .getSchemaType (p );
633
- String type = null ;
634
614
if (typeMapping .containsKey (openAPIType )) {
635
- type = typeMapping .get (openAPIType );
636
- if (languageSpecificPrimitives .contains (type ))
637
- return toModelName (type );
638
- } else
639
- type = openAPIType ;
640
- return toModelName (type );
615
+ return typeMapping .get (openAPIType );
616
+ } else {
617
+ return toModelName (openAPIType );
618
+ }
641
619
}
642
620
643
621
class ExtendedCodegenResponse extends CodegenResponse {
@@ -784,24 +762,6 @@ public ExtendedCodegenOperation(CodegenOperation o) {
784
762
this .operationIdCamelCase = o .operationIdCamelCase ;
785
763
}
786
764
787
- private void translateBaseType (StringBuilder returnEntry , String baseType ) {
788
- switch (baseType ) {
789
- case "AnyType" :
790
- returnEntry .append ("any()" );
791
- break ;
792
- case "Boolean" :
793
- returnEntry .append ("boolean()" );
794
- break ;
795
- case "Float" :
796
- returnEntry .append ("float()" );
797
- break ;
798
- default :
799
- returnEntry .append (baseType );
800
- returnEntry .append (".t" );
801
- break ;
802
- }
803
- }
804
-
805
765
public String typespec () {
806
766
StringBuilder sb = new StringBuilder ("@spec " );
807
767
sb .append (underscore (operationId ));
@@ -819,16 +779,10 @@ public String typespec() {
819
779
for (CodegenResponse response : this .responses ) {
820
780
ExtendedCodegenResponse exResponse = (ExtendedCodegenResponse ) response ;
821
781
StringBuilder returnEntry = new StringBuilder ();
822
- if (exResponse .baseType == null ) {
823
- returnEntry .append ("nil" );
824
- } else if (exResponse .containerType == null ) { // not container (array, map, set)
825
- returnEntry .append (normalizeTypeName (exResponse .dataType , exResponse .primitiveType ));
782
+ if (exResponse .schema != null ) {
783
+ returnEntry .append (getTypeDeclaration ((Schema ) exResponse .schema ));
826
784
} else {
827
- if (exResponse .containerType .equals ("array" ) || exResponse .containerType .equals ("set" )) {
828
- returnEntry .append (exResponse .dataType );
829
- } else if (exResponse .containerType .equals ("map" )) {
830
- returnEntry .append ("map()" );
831
- }
785
+ returnEntry .append (normalizeTypeName (exResponse .dataType , exResponse .primitiveType ));
832
786
}
833
787
uniqueResponseTypes .add (returnEntry .toString ());
834
788
}
@@ -845,14 +799,11 @@ private String normalizeTypeName(String baseType, boolean isPrimitive) {
845
799
if (baseType == null ) {
846
800
return "nil" ;
847
801
}
848
- if (isPrimitive || "String.t" . equals (baseType )) {
802
+ if (isPrimitive || languageSpecificPrimitives . contains (baseType )) {
849
803
return baseType ;
850
804
}
851
805
if (!baseType .startsWith (moduleName + ".Model." )) {
852
- baseType = moduleName + ".Model." + baseType ;
853
- }
854
- if (!baseType .endsWith (".t" )) {
855
- baseType += ".t" ;
806
+ baseType = moduleName + ".Model." + baseType + ".t" ;
856
807
}
857
808
return baseType ;
858
809
}
0 commit comments