39
39
public class ScalaCaskServerCodegen extends AbstractScalaCodegen implements CodegenConfig {
40
40
public static final String PROJECT_NAME = "projectName" ;
41
41
42
+ // this is our opinionated json type - ujson.Value - which is a first-class
43
+ // citizen of cask
44
+ private static final String AdditionalPropertiesType = "Value" ;
45
+
42
46
private final Logger LOGGER = LoggerFactory .getLogger (ScalaCaskServerCodegen .class );
43
47
44
48
@ Override
@@ -115,6 +119,8 @@ public ScalaCaskServerCodegen() {
115
119
116
120
typeMapping .put ("integer" , "Int" );
117
121
typeMapping .put ("long" , "Long" );
122
+ typeMapping .put ("AnyType" , AdditionalPropertiesType );
123
+
118
124
//TODO binary should be mapped to byte array
119
125
// mapped to String as a workaround
120
126
typeMapping .put ("binary" , "String" );
@@ -241,6 +247,7 @@ public void processOpts() {
241
247
importMapping .put ("OffsetDateTime" , "java.time.OffsetDateTime" );
242
248
importMapping .put ("LocalTime" , "java.time.LocalTime" );
243
249
importMapping .put ("Value" , "ujson.Value" );
250
+ importMapping .put (AdditionalPropertiesType , "ujson.Value" );
244
251
}
245
252
246
253
static boolean consumesMimetype (CodegenOperation op , String mimetype ) {
@@ -614,7 +621,7 @@ private void setDefaultValueForCodegenProperty(CodegenProperty p) {
614
621
if (p .getIsEnumOrRef ()) {
615
622
p .defaultValue = "null" ;
616
623
} else {
617
- p .defaultValue = defaultValueNonOption (p );
624
+ p .defaultValue = defaultValueNonOption (p , "null" );
618
625
}
619
626
} else if (p .defaultValue .contains ("Seq.empty" )) {
620
627
p .defaultValue = "Nil" ;
@@ -767,6 +774,23 @@ private static String defaultValue(IJsonSchemaValidationProperties p, boolean re
767
774
return defaultValueNonOption (p , fallbackDefaultValue );
768
775
}
769
776
777
+ /**
778
+ * the subtypes of IJsonSchemaValidationProperties have an 'isNumeric', but that's not a method on IJsonSchemaValidationProperties.
779
+ *
780
+ * This helper method tries to isolate that noisy logic in a safe way so we can ask 'is this IJsonSchemaValidationProperties numeric'?
781
+ * @param p the property
782
+ * @return true if the property is numeric
783
+ */
784
+ private static boolean isNumeric (IJsonSchemaValidationProperties p ) {
785
+ if (p instanceof CodegenParameter ) {
786
+ return ((CodegenParameter )p ).isNumeric ;
787
+ } else if (p instanceof CodegenProperty ) {
788
+ return ((CodegenProperty )p ).isNumeric ;
789
+ } else {
790
+ return p .getIsNumber () || p .getIsFloat () || p .getIsDecimal () || p .getIsDouble () || p .getIsInteger () || p .getIsLong () || p .getIsUnboundedInteger ();
791
+ }
792
+ }
793
+
770
794
private static String defaultValueNonOption (IJsonSchemaValidationProperties p , String fallbackDefaultValue ) {
771
795
if (p .getIsArray ()) {
772
796
if (p .getUniqueItems ()) {
@@ -777,7 +801,7 @@ private static String defaultValueNonOption(IJsonSchemaValidationProperties p, S
777
801
if (p .getIsMap ()) {
778
802
return "Map.empty" ;
779
803
}
780
- if (p . getIsNumber ( )) {
804
+ if (isNumeric ( p )) {
781
805
return "0" ;
782
806
}
783
807
if (p .getIsEnum ()) {
@@ -792,37 +816,12 @@ private static String defaultValueNonOption(IJsonSchemaValidationProperties p, S
792
816
if (p .getIsString ()) {
793
817
return "\" \" " ;
794
818
}
795
- return fallbackDefaultValue ;
796
- }
797
-
798
- private static String defaultValueNonOption (CodegenProperty p ) {
799
- if (p .getIsArray ()) {
800
- return "Nil" ;
801
- }
802
- if (p .getIsMap ()) {
803
- return "Map.empty" ;
804
- }
805
- if (p .isNumber || p .isNumeric ) {
806
- return "0" ;
807
- }
808
- if (p .isBoolean ) {
809
- return "false" ;
810
- }
811
- if (p .isUuid ) {
812
- return "java.util.UUID.randomUUID()" ;
813
- }
814
- if (p .isModel ) {
815
- return "null" ;
816
- }
817
- if (p .isDate || p .isDateTime ) {
818
- return "null" ;
819
- }
820
- if (p .isString ) {
821
- return "\" \" " ;
819
+ if (fallbackDefaultValue != null && !fallbackDefaultValue .trim ().isEmpty ()) {
820
+ return fallbackDefaultValue ;
822
821
}
823
- return p .defaultValue ;
824
- }
825
822
823
+ return "null" ;
824
+ }
826
825
827
826
@ Override
828
827
public CodegenProperty fromProperty (String name , Schema schema ) {
@@ -847,9 +846,9 @@ public String getTypeDeclaration(Schema schema) {
847
846
848
847
@ Override
849
848
public String toModelImport (String name ) {
850
- final String result = super .toModelImport (name );
849
+ String result = super .toModelImport (name );
851
850
if (importMapping .containsKey (name )) {
852
- return importMapping .get (name );
851
+ result = importMapping .get (name );
853
852
}
854
853
return result ;
855
854
}
0 commit comments