Skip to content

Commit e1e15a3

Browse files
committed
Support "const" keyword
1 parent fc96e97 commit e1e15a3

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

core/src/main/java/com/cosium/json_schema_to_java_record/JsonSchemaContent.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ record JsonSchemaContent(
3535
@Nullable JsonSchemaContent items,
3636
@Nullable List<Object> enumeration,
3737
Map<String, JsonSchemaContent> properties,
38-
Set<String> required) {
38+
Set<String> required,
39+
@Nullable String constValue) {
3940

4041
private static final Logger LOGGER = LoggerFactory.getLogger(JsonSchemaContent.class);
4142

@@ -48,7 +49,8 @@ record JsonSchemaContent(
4849
@JsonProperty("items") @Nullable JsonSchemaContent items,
4950
@JsonProperty("enum") @Nullable List<Object> enumeration,
5051
@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) {
5254
this(
5355
$schema,
5456
$id,
@@ -57,7 +59,8 @@ record JsonSchemaContent(
5759
items,
5860
enumeration,
5961
Optional.ofNullable(properties).orElseGet(Map::of),
60-
Optional.ofNullable(required).orElseGet(Set::of));
62+
Optional.ofNullable(required).orElseGet(Set::of),
63+
constValue);
6164
}
6265

6366
public TypeName writeJavaType(
@@ -66,6 +69,10 @@ public TypeName writeJavaType(
6669
ClassName fallbackClassName,
6770
boolean preferPrimitive) {
6871

72+
if (constValue != null) {
73+
return ClassName.get(String.class);
74+
}
75+
6976
final TypeName processedTypeName =
7077
switch (type()) {
7178
case STRING -> {
@@ -137,6 +144,20 @@ public TypeName writeJavaType(
137144
String propertyName = property.getKey();
138145
JsonSchemaContent propertySchema = property.getValue();
139146

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+
140161
TypeName propertyType =
141162
propertySchema.writeJavaType(
142163
javaTypes,
@@ -211,10 +232,16 @@ private boolean isList(TypeName type) {
211232
return className.canonicalName().equals(List.class.getCanonicalName());
212233
}
213234

214-
private void addJsonRelatedAnnotations(
215-
ParameterSpec.Builder parameterBuilder, String propertyName) {
235+
private void addJsonRelatedAnnotations(ParameterSpec.Builder builder, String propertyName) {
216236

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(
218245
AnnotationSpec.builder(JsonProperty.class).addMember("value", "$S", propertyName).build());
219246
}
220247

@@ -239,6 +266,14 @@ private void addNullRelatedAnnotations(
239266
}
240267
}
241268

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+
242277
private void addRecordBuilderRelatedAnnotations(
243278
JavaTypes javaTypes, TypeSpec.Builder typeBuilder) {
244279

tests/src/test/resources/com/cosium/json_schema_to_java_record_tests/case1/address.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://json-schema.org/draft/2020-12/schema",
2+
"$schema": "https://json-schema.org/draft-06/schema",
33
"$id": "address",
44
"type": "object",
55
"properties": {
@@ -11,6 +11,9 @@
1111
},
1212
"country": {
1313
"$ref": "classpath:/com/cosium/json_schema_to_java_record_tests/case1/country.json"
14+
},
15+
"planet": {
16+
"const": "earth"
1417
}
1518
},
1619
"required": [ "streetNumber", "streetName" ]

0 commit comments

Comments
 (0)