@@ -58,45 +58,57 @@ object SwaggerSerializers extends Serializers {
58
58
)
59
59
}, {
60
60
case x : Model =>
61
- implicit val fmts = formats
62
- val required : List [String ] = (for ((name, prop) <- x.properties) yield {
63
- if (prop.required) Some (name)
64
- else None
65
- }).flatten.toList
66
- (" id" -> x.id) ~
67
- (" description" -> x.description) ~
68
- (" required" -> (required.size match {
69
- case 0 => JNothing
70
- case _ => Extraction .decompose(required)
71
- })) ~
72
- (" extends" -> {
73
- x.baseModel match {
74
- case Some (e) if (e != " void" && e != " java.lang.Void" ) => Extraction .decompose(e)
75
- case _ => JNothing
61
+ val ContainerMatcher = " (List|Array|Set)\\ [(.*)\\ ].*?" .r
62
+ val MapMatcher = " Map\\ [([^\\ ],]*),\\ s*([^\\ ],]*)\\ ].*?" .r
63
+ x.name match {
64
+ case ContainerMatcher (container, value) => {
65
+ (" id" -> x.id) ~ toJsonSchemaContainer(value)
76
66
}
77
- }) ~
78
- (" discriminator" -> {
79
- x.discriminator match {
80
- case Some (e) if (e.trim != " " && e.trim != " void" ) => Extraction .decompose(e)
81
- case _ => JNothing
67
+ case MapMatcher (key, value) => {
68
+ (" id" -> x.id) ~ toJsonSchemaMap(value)
82
69
}
83
- }) ~
84
- (" properties" -> {
85
- x.properties match {
86
- case e : LinkedHashMap [String , ModelProperty ] => {
87
- (for ((key, value) <- e) yield (key -> Extraction .decompose(value))).toList
88
- }
89
- case _ => List .empty
90
- }
91
- }) ~
92
- (" subTypes" -> {
93
- x.subTypes match {
94
- case e : List [String ] if (e.size > 0 ) => {
95
- Extraction .decompose(for (m <- e) yield ModelUtil .cleanDataType(m))
96
- }
97
- case _ => JNothing
70
+ case _ => {
71
+ implicit val fmts = formats
72
+ val required : List [String ] = (for ((name, prop) <- x.properties) yield {
73
+ if (prop.required) Some (name)
74
+ else None
75
+ }).flatten.toList
76
+ (" id" -> x.id) ~
77
+ (" description" -> x.description) ~
78
+ (" required" -> (required.size match {
79
+ case 0 => JNothing
80
+ case _ => Extraction .decompose(required)
81
+ })) ~
82
+ (" extends" -> {
83
+ x.baseModel match {
84
+ case Some (e) if (e != " void" && e != " java.lang.Void" ) => Extraction .decompose(e)
85
+ case _ => JNothing
86
+ }
87
+ }) ~
88
+ (" discriminator" -> {
89
+ x.discriminator match {
90
+ case Some (e) if (e.trim != " " && e.trim != " void" ) => Extraction .decompose(e)
91
+ case _ => JNothing
92
+ }
93
+ }) ~
94
+ (" properties" -> {
95
+ x.properties match {
96
+ case e : LinkedHashMap [String , ModelProperty ] => {
97
+ (for ((key, value) <- e) yield (key -> Extraction .decompose(value))).toList
98
+ }
99
+ case _ => List .empty
100
+ }
101
+ }) ~
102
+ (" subTypes" -> {
103
+ x.subTypes match {
104
+ case e : List [String ] if (e.size > 0 ) => {
105
+ Extraction .decompose(for (m <- e) yield ModelUtil .cleanDataType(m))
106
+ }
107
+ case _ => JNothing
108
+ }
109
+ })
98
110
}
99
- })
111
+ }
100
112
}
101
113
))
102
114
@@ -115,6 +127,7 @@ object SwaggerSerializers extends Serializers {
115
127
case " date-time" => (name -> " string" ) ~ (" format" -> " date-time" )
116
128
case _ => {
117
129
val ContainerMatcher = " (List|Array|Set)\\ [(.*)\\ ].*?" .r
130
+ val MapMatcher = " Map\\ [([^\\ ],]*),\\ s*([^\\ ],]*)\\ ].*?" .r
118
131
`type` match {
119
132
case ContainerMatcher (container, value) =>
120
133
toJsonSchemaContainer(container) ~ {
@@ -123,6 +136,8 @@ object SwaggerSerializers extends Serializers {
123
136
else
124
137
toJsonSchema(" $ref" , value)})
125
138
}
139
+ case MapMatcher (key, value) =>
140
+ toJsonSchemaMap(value)
126
141
case _ => (name -> `type`) ~ (" format" -> JNothing )
127
142
}
128
143
}
@@ -138,6 +153,16 @@ object SwaggerSerializers extends Serializers {
138
153
}
139
154
}
140
155
156
+ def toJsonSchemaMap (value : String ): JObject = {
157
+ (" type" -> " object" ) ~ (" patternProperties" ->
158
+ (" .*" -> {
159
+ if (isSimpleType(value))
160
+ toJsonSchema(" type" , value)
161
+ else
162
+ toJsonSchema(" $ref" , value)
163
+ }))
164
+ }
165
+
141
166
def isSimpleType (name : String ) = {
142
167
Set (" int" , " long" , " float" , " double" , " string" , " byte" , " boolean" , " Date" , " date" , " date-time" , " array" ).contains(name)
143
168
}
@@ -154,6 +179,16 @@ object SwaggerSerializers extends Serializers {
154
179
case " Set" => (" type" -> " array" ) ~ (" uniqueItems" -> true )
155
180
}
156
181
}
182
+ else if (prop.`type`.startsWith(" Map[" )) {
183
+ val MapMatcher = " Map\\ [([^\\ ],]*),\\ s*([^\\ ],]*)\\ ].*?" .r
184
+ prop.`type` match {
185
+ case MapMatcher (key, value) => {
186
+ toJsonSchemaMap(value)
187
+ }
188
+ case _ =>
189
+ (" $ref" -> prop.`type`) ~ (" format" -> JNothing )
190
+ }
191
+ }
157
192
else (" $ref" -> prop.`type`) ~ (" format" -> JNothing )
158
193
}
159
194
@@ -403,26 +438,6 @@ object SwaggerSerializers extends Serializers {
403
438
))
404
439
}
405
440
406
- object SwaggerJsonSchemaSerializers extends Serializers {
407
- // object SwaggerSerializers extends Serializers {
408
- implicit val formats = DefaultFormats +
409
- new ModelSerializer +
410
- new ModelPropertySerializer +
411
- new ModelRefSerializer +
412
- new AllowableValuesSerializer +
413
- new ParameterSerializer +
414
- new OperationSerializer +
415
- new ResponseMessageSerializer +
416
- new SampleSerializer +
417
- new ApiDescriptionSerializer +
418
- new ApiListingReferenceSerializer +
419
- new ResourceListingSerializer +
420
- new ApiInfoSerializer +
421
- new ApiListingSerializer +
422
- new AuthorizationTypeSerializer +
423
- new AuthorizationSerializer
424
- }
425
-
426
441
trait Serializers {
427
442
import ValidationMessage ._
428
443
val validationMessages = ListBuffer .empty[ValidationMessage ]
@@ -861,116 +876,6 @@ trait Serializers {
861
876
}
862
877
))
863
878
864
- class ModelSerializer extends CustomSerializer [Model ](formats => ({
865
- case json =>
866
- implicit val fmts : Formats = formats
867
- val output = new LinkedHashMap [String , ModelProperty ]
868
- val properties = (json \ " properties" ) match {
869
- case JObject (entries) => {
870
- entries.map({
871
- case (key, value) => output += key -> value.extract[ModelProperty ]
872
- })
873
- }
874
- case _ =>
875
- }
876
-
877
- Model (
878
- (json \ " id" ).extractOrElse({
879
- !! (json, MODEL , " id" , " missing required field" , ERROR )
880
- " "
881
- }),
882
- (json \ " name" ).extractOrElse((json \ " id" ).extract[String ]),
883
- (json \ " qualifiedType" ).extractOrElse(" " ),
884
- output,
885
- (json \ " description" ).extractOpt[String ],
886
- (json \ " extends" ).extractOpt[String ],
887
- (json \ " discriminator" ).extractOpt[String ]
888
- )
889
- }, {
890
- case x : Model =>
891
- implicit val fmts = formats
892
- (" id" -> x.id) ~
893
- (" name" -> x.name) ~
894
- (" properties" -> {
895
- x.properties match {
896
- case e : LinkedHashMap [String , ModelProperty ] => Extraction .decompose(e.toMap)
897
- case _ => JNothing
898
- }
899
- }) ~
900
- (" description" -> x.description) ~
901
- (" extends" -> {
902
- x.baseModel match {
903
- case Some (e) if (e != " void" ) => Extraction .decompose(e)
904
- case _ => JNothing
905
- }
906
- }) ~
907
- (" discriminator" -> {
908
- x.discriminator match {
909
- case Some (e) if (e.trim != " " && e.trim != " void" ) => Extraction .decompose(e)
910
- case _ => JNothing
911
- }
912
- })
913
- }
914
- ))
915
-
916
- class ModelPropertySerializer extends CustomSerializer [ModelProperty ] (formats => ({
917
- case json =>
918
- implicit val fmts : Formats = formats
919
- ModelProperty (
920
- `type` = (json \ " type" ).extractOrElse(" " ),
921
- qualifiedType = (json \ " type" ).extractOrElse(" " ),
922
- position = (json \ " position" ).extractOrElse(0 ),
923
- (json \ " required" ) match {
924
- case e: JString => e.s.toBoolean
925
- case e: JBool => e.value
926
- case _ => false
927
- },
928
- description = (json \ " description" ).extractOpt[String ],
929
- allowableValues = (json \ " allowableValues" ).extract[AllowableValues ],
930
- items = {
931
- (json \ " items" ).extractOpt[ModelRef ] match {
932
- case Some (e : ModelRef ) if (e.`type` != null || e.ref != None ) => Some (e)
933
- case _ => None
934
- }
935
- }
936
- )
937
- }, {
938
- case x : ModelProperty =>
939
- implicit val fmts = formats
940
- (" type" -> x.`type`) ~
941
- (" required" -> x.required) ~
942
- (" description" -> x.description) ~
943
- (" allowableValues" -> {
944
- x.allowableValues match {
945
- case AnyAllowableValues => JNothing // don't serialize when not a concrete type
946
- case e: AllowableValues => Extraction .decompose(x.allowableValues)
947
- case _ => JNothing
948
- }
949
- }) ~
950
- (" items" -> Extraction .decompose(x.items))
951
- }
952
- ))
953
-
954
- class ModelRefSerializer extends CustomSerializer [ModelRef ](formats => ({
955
- case json =>
956
- implicit val fmts : Formats = formats
957
- ModelRef (
958
- (json \ " type" ).extractOrElse(null : String ),
959
- (json \ " $ref" ).extractOpt[String ]
960
- )
961
- }, {
962
- case x : ModelRef =>
963
- implicit val fmts = formats
964
- (" type" -> {
965
- x.`type` match {
966
- case e: String => Some (e)
967
- case _ => None
968
- }
969
- }) ~
970
- (" $ref" -> x.ref)
971
- }
972
- ))
973
-
974
879
class AuthorizationTypeSerializer extends CustomSerializer [AuthorizationType ](formats => ({
975
880
case json =>
976
881
implicit val fmts : Formats = formats
0 commit comments