@@ -136,21 +136,25 @@ public JType apply(
136
136
Schema schema ) {
137
137
138
138
Optional <JType > refType = refType (nodeName , schemaNode , parent , generatableType , schema );
139
- List <JTypeWrapper > unionTypes = new ArrayList <>();
139
+ List <JTypeWrapper > oneOfTypes = new ArrayList <>();
140
+ List <JTypeWrapper > allOfTypes = new ArrayList <>();
140
141
141
- unionType ("oneOf" , nodeName , schemaNode , parent , generatableType , schema , unionTypes );
142
- unionType ("anyOf" , nodeName , schemaNode , parent , generatableType , schema , unionTypes );
143
- unionType ("allOf" , nodeName , schemaNode , parent , generatableType , schema , unionTypes );
142
+ unionType ("oneOf" , nodeName , schemaNode , parent , generatableType , schema , oneOfTypes );
143
+ unionType ("anyOf" , nodeName , schemaNode , parent , generatableType , schema , oneOfTypes );
144
+ unionType ("allOf" , nodeName , schemaNode , parent , generatableType , schema , allOfTypes );
144
145
145
- Collections .sort (unionTypes );
146
+ Collections .sort (oneOfTypes );
146
147
147
148
JType javaType ;
148
149
if (schemaNode .has ("enum" )) {
149
150
javaType =
150
151
ruleFactory .getEnumRule ().apply (nodeName , schemaNode , parent , generatableType , schema );
151
- } else if (!schemaNode .has ("properties" ) && unionTypes .isEmpty () && refType .isPresent ()) {
152
+ } else if (!schemaNode .has ("properties" )
153
+ && oneOfTypes .isEmpty ()
154
+ && allOfTypes .isEmpty ()
155
+ && refType .isPresent ()) {
152
156
javaType = refType .get ();
153
- } else if (!unionTypes .isEmpty ()) {
157
+ } else if (!oneOfTypes . isEmpty () || ! allOfTypes .isEmpty ()) {
154
158
JPackage container = generatableType .getPackage ();
155
159
JType generatedType =
156
160
ruleFactory .getTypeRule ().apply (nodeName , schemaNode , parent , container , schema );
@@ -166,15 +170,17 @@ public JType apply(
166
170
unionClass = container ._class (clazz .name () + "Union" );
167
171
commonType = Optional .of (clazz );
168
172
}
169
-
170
173
} else {
171
174
unionClass =
172
175
container ._class (
173
176
ruleFactory .getNameHelper ().getUniqueClassName (nodeName , schemaNode , container ));
174
177
commonType = Optional .empty ();
175
178
}
176
179
javaType =
177
- populateRef (populateClass (schema , unionClass , commonType , unionTypes ), refType , schema );
180
+ populateRef (
181
+ populateClass (schema , unionClass , commonType , oneOfTypes , allOfTypes ),
182
+ refType ,
183
+ schema );
178
184
schema .setJavaTypeIfEmpty (javaType );
179
185
} catch (JClassAlreadyExistsException ex ) {
180
186
throw new IllegalStateException (ex );
@@ -197,36 +203,37 @@ private JDefinedClass populateClass(
197
203
Schema parentSchema ,
198
204
JDefinedClass definedClass ,
199
205
Optional <JType > commonType ,
200
- Collection <JTypeWrapper > unionTypes ) {
206
+ Collection <JTypeWrapper > oneOfTypes ,
207
+ Collection <JTypeWrapper > allOfTypes ) {
201
208
JFieldVar valueField =
202
209
definedClass .field (
203
210
JMod .PRIVATE ,
204
- commonType . orElse ( definedClass .owner ().ref (Object .class ) ),
211
+ definedClass .owner ().ref (Object .class ),
205
212
ruleFactory .getNameHelper ().getPropertyName ("value" , null ),
206
213
null );
207
214
208
- definedClass ._implements (
209
- definedClass
210
- .owner ()
211
- .ref (GeneratorUtils .ONE_OF_VALUE_PROVIDER_INTERFACE_NAME )
212
- .narrow (valueField .type ()));
213
-
214
- GeneratorUtils .implementInterface (definedClass , valueField );
215
-
216
- try {
217
- JDefinedClass serializer = generateSerializer (definedClass );
218
- definedClass .annotate (JsonSerialize .class ).param ("using" , serializer );
219
- } catch (JClassAlreadyExistsException ex ) {
220
- // already serialized aware
215
+ if (!oneOfTypes .isEmpty ()) {
216
+ definedClass ._implements (
217
+ definedClass .owner ().ref (GeneratorUtils .ONE_OF_VALUE_PROVIDER_INTERFACE_NAME ));
218
+ GeneratorUtils .implementInterface (definedClass , valueField );
219
+ try {
220
+ JDefinedClass serializer = generateSerializer (definedClass );
221
+ definedClass .annotate (JsonSerialize .class ).param ("using" , serializer );
222
+ } catch (JClassAlreadyExistsException ex ) {
223
+ // already serialized aware
224
+ }
221
225
}
222
226
223
227
try {
224
- JDefinedClass deserializer = generateDeserializer (definedClass , unionTypes );
228
+ JDefinedClass deserializer = generateDeserializer (definedClass , oneOfTypes , allOfTypes );
225
229
definedClass .annotate (JsonDeserialize .class ).param ("using" , deserializer );
226
230
} catch (JClassAlreadyExistsException ex ) {
227
231
// already deserialized aware
228
232
}
229
233
234
+ Collection <JTypeWrapper > unionTypes = new ArrayList <>();
235
+ unionTypes .addAll (oneOfTypes );
236
+ unionTypes .addAll (allOfTypes );
230
237
Collection <JTypeWrapper > stringTypes = new ArrayList <>();
231
238
for (JTypeWrapper unionType : unionTypes ) {
232
239
if (isStringType (unionType .getType ())) {
@@ -291,40 +298,61 @@ private JDefinedClass generateSerializer(JDefinedClass relatedClass)
291
298
}
292
299
293
300
private JDefinedClass generateDeserializer (
294
- JDefinedClass relatedClass , Collection <JTypeWrapper > unionTypes )
301
+ JDefinedClass relatedClass ,
302
+ Collection <JTypeWrapper > oneOfTypes ,
303
+ Collection <JTypeWrapper > allOfTypes )
295
304
throws JClassAlreadyExistsException {
296
305
JDefinedClass definedClass = GeneratorUtils .deserializerClass (relatedClass );
297
306
GeneratorUtils .fillDeserializer (
298
307
definedClass ,
299
308
relatedClass ,
300
309
(method , parserParam ) -> {
301
310
JBlock body = method .body ();
302
- JInvocation list = definedClass .owner ().ref (List .class ).staticInvoke ("of" );
303
- unionTypes .forEach (c -> list .arg (((JClass ) c .getType ()).dotclass ()));
311
+
304
312
body ._return (
305
313
definedClass
306
314
.owner ()
307
315
.ref (GeneratorUtils .DESERIALIZE_HELPER_NAME )
308
316
.staticInvoke ("deserializeOneOf" )
309
317
.arg (parserParam )
310
318
.arg (relatedClass .dotclass ())
311
- .arg (list ));
319
+ .arg (list (definedClass , oneOfTypes ))
320
+ .arg (list (definedClass , allOfTypes )));
312
321
});
313
322
return definedClass ;
314
323
}
315
324
325
+ private JInvocation list (JDefinedClass definedClass , Collection <JTypeWrapper > list ) {
326
+ JInvocation result = definedClass .owner ().ref (List .class ).staticInvoke ("of" );
327
+ list .forEach (c -> result .arg (((JClass ) c .getType ()).dotclass ()));
328
+ return result ;
329
+ }
330
+
316
331
private void wrapIt (
317
332
Schema parentSchema ,
318
333
JDefinedClass definedClass ,
319
334
Optional <JFieldVar > valueField ,
320
335
JType unionType ,
321
336
JsonNode node ) {
322
337
JFieldVar instanceField = getInstanceField (parentSchema , definedClass , unionType , node );
323
- JMethod constructor = definedClass .constructor (JMod .PUBLIC );
324
- JVar instanceParam = constructor .param (unionType , instanceField .name ());
325
- JBlock body = constructor .body ();
326
- valueField .ifPresent (v -> body .assign (JExpr ._this ().ref (v ), instanceParam ));
327
- body .assign (JExpr ._this ().ref (instanceField ), instanceParam );
338
+ JMethod method = getSetterMethod (definedClass , instanceField , node );
339
+ JBlock body = method .body ();
340
+ JVar methodParam = method .param (instanceField .type (), instanceField .name ());
341
+ valueField .ifPresent (v -> body .assign (JExpr ._this ().ref (v ), methodParam ));
342
+ body .assign (JExpr ._this ().ref (instanceField ), methodParam );
343
+ }
344
+
345
+ private JMethod getSetterMethod (
346
+ JDefinedClass definedClass , JFieldVar instanceField , JsonNode node ) {
347
+ JMethod method =
348
+ definedClass .method (
349
+ JMod .PUBLIC ,
350
+ definedClass .owner ().VOID ,
351
+ ruleFactory .getNameHelper ().getSetterName (instanceField .name (), node ));
352
+ method
353
+ .annotate (definedClass .owner ().ref (GeneratorUtils .SETTER_ANNOTATION_NAME ))
354
+ .param ("value" , instanceField .type ());
355
+ return method ;
328
356
}
329
357
330
358
private void wrapStrings (
@@ -334,21 +362,20 @@ private void wrapStrings(
334
362
Collection <JTypeWrapper > stringTypes ) {
335
363
Iterator <JTypeWrapper > iter = stringTypes .iterator ();
336
364
JTypeWrapper first = iter .next ();
337
- JMethod constructor = definedClass .constructor (JMod .PUBLIC );
338
-
339
- JBlock body = constructor .body ();
340
365
String pattern = pattern (first .getNode (), parentSchema );
341
366
if (pattern == null && iter .hasNext ()) {
342
367
pattern = ".*" ;
343
368
}
344
369
JFieldVar instanceField =
345
370
getInstanceField (parentSchema , definedClass , first .getType (), first .getNode ());
346
- JVar instanceParam = constructor .param (first .type , instanceField .name ());
347
- body .assign (JExpr ._this ().ref (valueField ), instanceParam );
371
+ JMethod setterMethod = getSetterMethod (definedClass , instanceField , first .getNode ());
372
+ JBlock body = setterMethod .body ();
373
+ JVar methodParam = setterMethod .param (instanceField .type (), instanceField .name ());
374
+ body .assign (JExpr ._this ().ref (valueField ), methodParam );
348
375
if (pattern != null ) {
349
376
JConditional condition =
350
- body ._if (getPatternCondition (pattern , body , instanceField , instanceParam , definedClass ));
351
- condition ._then ().assign (JExpr ._this ().ref (instanceField ), instanceParam );
377
+ body ._if (getPatternCondition (pattern , body , instanceField , methodParam , definedClass ));
378
+ condition ._then ().assign (JExpr ._this ().ref (instanceField ), methodParam );
352
379
while (iter .hasNext ()) {
353
380
JTypeWrapper item = iter .next ();
354
381
instanceField =
@@ -359,8 +386,8 @@ private void wrapStrings(
359
386
}
360
387
condition =
361
388
condition ._elseif (
362
- getPatternCondition (pattern , body , instanceField , instanceParam , definedClass ));
363
- condition ._then ().assign (JExpr ._this ().ref (instanceField ), instanceParam );
389
+ getPatternCondition (pattern , body , instanceField , methodParam , definedClass ));
390
+ condition ._then ().assign (JExpr ._this ().ref (instanceField ), methodParam );
364
391
}
365
392
condition
366
393
._else ()
@@ -372,10 +399,10 @@ private void wrapStrings(
372
399
.ref (String .class )
373
400
.staticInvoke ("format" )
374
401
.arg ("%s does not match any pattern" )
375
- .arg (instanceParam ))
402
+ .arg (methodParam ))
376
403
.arg (JExpr ._null ()));
377
404
} else {
378
- body .assign (JExpr ._this ().ref (instanceField ), instanceParam );
405
+ body .assign (JExpr ._this ().ref (instanceField ), methodParam );
379
406
}
380
407
}
381
408
@@ -388,7 +415,7 @@ private JFieldVar getInstanceField(
388
415
ruleFactory
389
416
.getNameHelper ()
390
417
.getPropertyName (getTypeName (node , type , parentSchema ), node ));
391
- GeneratorUtils .buildMethod (
418
+ GeneratorUtils .getterMethod (
392
419
definedClass , instanceField , ruleFactory .getNameHelper (), instanceField .name ());
393
420
return instanceField ;
394
421
}
0 commit comments