@@ -77,7 +77,7 @@ public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass,
77
77
ruleFactory .getAnnotator ().propertyField (field , jclass , nodeName , node );
78
78
79
79
if (isIncludeGetters ) {
80
- JMethod getter = addGetter (jclass , field , nodeName , node , isRequired (nodeName , node , schema ));
80
+ JMethod getter = addGetter (jclass , field , nodeName , node , isRequired (nodeName , node , schema ), useOptional ( nodeName , node , schema ) );
81
81
ruleFactory .getAnnotator ().propertyGetter (getter , jclass , nodeName );
82
82
propertyAnnotations (nodeName , node , schema , getter );
83
83
}
@@ -129,6 +129,24 @@ private boolean isRequired(String nodeName, JsonNode node, Schema schema) {
129
129
return false ;
130
130
}
131
131
132
+ private boolean useOptional (String nodeName , JsonNode node , Schema schema ) {
133
+ if (node .has ("javaOptional" )) {
134
+ final JsonNode requiredNode = node .get ("javaOptional" );
135
+ return requiredNode .asBoolean ();
136
+ }
137
+
138
+ JsonNode javaOptionalArray = schema .getContent ().get ("javaOptional" );
139
+
140
+ if (javaOptionalArray != null ) {
141
+ for (JsonNode requiredNode : javaOptionalArray ) {
142
+ if (nodeName .equals (requiredNode .asText ()))
143
+ return true ;
144
+ }
145
+ }
146
+
147
+ return false ;
148
+ }
149
+
132
150
private void propertyAnnotations (String nodeName , JsonNode node , Schema schema , JDocCommentable generatedJavaConstruct ) {
133
151
if (node .has ("title" )) {
134
152
ruleFactory .getTitleRule ().apply (nodeName , node .get ("title" ), generatedJavaConstruct , schema );
@@ -178,9 +196,9 @@ private boolean isArray(JsonNode node) {
178
196
return node .path ("type" ).asText ().equals ("array" );
179
197
}
180
198
181
- private JType getReturnType (final JDefinedClass c , final JFieldVar field , final boolean required ) {
199
+ private JType getReturnType (final JDefinedClass c , final JFieldVar field , final boolean required , final boolean usesOptional ) {
182
200
JType returnType = field .type ();
183
- if (ruleFactory .getGenerationConfig ().isUseOptionalForGetters ()) {
201
+ if (ruleFactory .getGenerationConfig ().isUseOptionalForGetters () || usesOptional ) {
184
202
if (!required && field .type ().isReference ()) {
185
203
returnType = c .owner ().ref ("java.util.Optional" ).narrow (field .type ());
186
204
}
@@ -189,14 +207,14 @@ private JType getReturnType(final JDefinedClass c, final JFieldVar field, final
189
207
return returnType ;
190
208
}
191
209
192
- private JMethod addGetter (JDefinedClass c , JFieldVar field , String jsonPropertyName , JsonNode node , boolean isRequired ) {
210
+ private JMethod addGetter (JDefinedClass c , JFieldVar field , String jsonPropertyName , JsonNode node , boolean isRequired , boolean usesOptional ) {
193
211
194
- JType type = getReturnType (c , field , isRequired );
212
+ JType type = getReturnType (c , field , isRequired , usesOptional );
195
213
196
214
JMethod getter = c .method (JMod .PUBLIC , type , getGetterName (jsonPropertyName , field .type (), node ));
197
215
198
216
JBlock body = getter .body ();
199
- if (ruleFactory .getGenerationConfig ().isUseOptionalForGetters () && !isRequired
217
+ if (( ruleFactory .getGenerationConfig ().isUseOptionalForGetters () || usesOptional ) && !isRequired
200
218
&& field .type ().isReference ()) {
201
219
body ._return (c .owner ().ref ("java.util.Optional" ).staticInvoke ("ofNullable" ).arg (field ));
202
220
} else {
0 commit comments