@@ -35,7 +35,8 @@ record JsonSchemaContent(
35
35
@ Nullable JsonSchemaContent items ,
36
36
@ Nullable List <Object > enumeration ,
37
37
Map <String , JsonSchemaContent > properties ,
38
- Set <String > required ) {
38
+ Set <String > required ,
39
+ @ Nullable String constValue ) {
39
40
40
41
private static final Logger LOGGER = LoggerFactory .getLogger (JsonSchemaContent .class );
41
42
@@ -48,7 +49,8 @@ record JsonSchemaContent(
48
49
@ JsonProperty ("items" ) @ Nullable JsonSchemaContent items ,
49
50
@ JsonProperty ("enum" ) @ Nullable List <Object > enumeration ,
50
51
@ JsonProperty ("properties" ) @ Nullable Map <String , JsonSchemaContent > properties ,
51
- @ JsonProperty ("required" ) @ Nullable Set <String > required ) {
52
+ @ JsonProperty ("required" ) @ Nullable Set <String > required ,
53
+ @ JsonProperty ("const" ) @ Nullable String constValue ) {
52
54
this (
53
55
$schema ,
54
56
$id ,
@@ -57,7 +59,8 @@ record JsonSchemaContent(
57
59
items ,
58
60
enumeration ,
59
61
Optional .ofNullable (properties ).orElseGet (Map ::of ),
60
- Optional .ofNullable (required ).orElseGet (Set ::of ));
62
+ Optional .ofNullable (required ).orElseGet (Set ::of ),
63
+ constValue );
61
64
}
62
65
63
66
public TypeName writeJavaType (
@@ -66,6 +69,10 @@ public TypeName writeJavaType(
66
69
ClassName fallbackClassName ,
67
70
boolean preferPrimitive ) {
68
71
72
+ if (constValue != null ) {
73
+ return ClassName .get (String .class );
74
+ }
75
+
69
76
final TypeName processedTypeName =
70
77
switch (type ()) {
71
78
case STRING -> {
@@ -137,6 +144,20 @@ public TypeName writeJavaType(
137
144
String propertyName = property .getKey ();
138
145
JsonSchemaContent propertySchema = property .getValue ();
139
146
147
+ String propertyConstValue = propertySchema .constValue ();
148
+ if (propertyConstValue != null ) {
149
+ typeBuilder .addMethod (
150
+ addNotNullRelatedAnnotations (
151
+ javaTypes ,
152
+ addJsonRelatedAnnotations (
153
+ MethodSpec .methodBuilder (propertyName ), propertyName ))
154
+ .addModifiers (Modifier .PUBLIC )
155
+ .returns (String .class )
156
+ .addStatement ("return $S" , propertyConstValue )
157
+ .build ());
158
+ continue ;
159
+ }
160
+
140
161
TypeName propertyType =
141
162
propertySchema .writeJavaType (
142
163
javaTypes ,
@@ -211,10 +232,16 @@ private boolean isList(TypeName type) {
211
232
return className .canonicalName ().equals (List .class .getCanonicalName ());
212
233
}
213
234
214
- private void addJsonRelatedAnnotations (
215
- ParameterSpec .Builder parameterBuilder , String propertyName ) {
235
+ private void addJsonRelatedAnnotations (ParameterSpec .Builder builder , String propertyName ) {
216
236
217
- parameterBuilder .addAnnotation (
237
+ builder .addAnnotation (
238
+ AnnotationSpec .builder (JsonProperty .class ).addMember ("value" , "$S" , propertyName ).build ());
239
+ }
240
+
241
+ private MethodSpec .Builder addJsonRelatedAnnotations (
242
+ MethodSpec .Builder builder , String propertyName ) {
243
+
244
+ return builder .addAnnotation (
218
245
AnnotationSpec .builder (JsonProperty .class ).addMember ("value" , "$S" , propertyName ).build ());
219
246
}
220
247
@@ -239,6 +266,14 @@ private void addNullRelatedAnnotations(
239
266
}
240
267
}
241
268
269
+ private MethodSpec .Builder addNotNullRelatedAnnotations (
270
+ JavaTypes javaTypes , MethodSpec .Builder builder ) {
271
+ if (!javaTypes .existsOnClassPath (ClassName .get (NonNull .class ))) {
272
+ return builder ;
273
+ }
274
+ return builder .addAnnotation (NonNull .class );
275
+ }
276
+
242
277
private void addRecordBuilderRelatedAnnotations (
243
278
JavaTypes javaTypes , TypeSpec .Builder typeBuilder ) {
244
279
0 commit comments