Skip to content

Commit c2d7d80

Browse files
committed
refactor: address review comments for JsonSchema generation
- Apply LY OSS formatting and improve readability for JacksonPolymorphismTypeInfoProvider. - Update DiscriminatorInfo Javadoc to use FQCN for clarity. - Remove unused fields and imports in JsonSchemaGenerator. - Fix Checkstyle errors regarding operator wrapping in JsonSchemaGenerator.
1 parent c0de833 commit c2d7d80

3 files changed

Lines changed: 30 additions & 48 deletions

File tree

core/src/main/java/com/linecorp/armeria/internal/server/docs/JacksonPolymorphismTypeInfoProvider.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,25 @@ public DescriptiveTypeInfo newDescriptiveTypeInfo(Object typeDescriptor) {
101101
final DiscriminatorInfo discriminator = DiscriminatorInfo.of(propertyName, mapping);
102102

103103
final List<TypeSignature> oneOf = Arrays.stream(jsonSubTypes.value())
104-
.map(subType -> TypeSignature.ofStruct(subType.value()))
105-
.collect(toImmutableList());
104+
.map(subType -> TypeSignature.ofStruct(subType.value()))
105+
.collect(toImmutableList());
106106

107107
final JavaType javaType = mapper.constructType(clazz);
108108
final BeanDescription description = mapper.getSerializationConfig().introspect(javaType);
109109
final List<BeanPropertyDefinition> properties = description.findProperties();
110110

111111
final List<FieldInfo> fields = properties.stream()
112-
.map(prop -> FieldInfo.of(prop.getName(),
113-
toTypeSignature(
114-
prop.getPrimaryType())))
115-
.collect(toImmutableList());
112+
.map(prop -> FieldInfo.of(prop.getName(),
113+
toTypeSignature(
114+
prop.getPrimaryType())))
115+
.collect(toImmutableList());
116116

117117
final Description classDescription = clazz.getAnnotation(Description.class);
118118

119119
final DescriptionInfo descriptionInfo = classDescription == null ? DescriptionInfo.empty()
120-
: DescriptionInfo.from(classDescription);
120+
: DescriptionInfo.from(
121+
classDescription);
121122

122-
return new StructInfo(clazz.getName(), null, fields,
123-
descriptionInfo, oneOf, discriminator);
123+
return new StructInfo(clazz.getName(), null, fields, descriptionInfo, oneOf, discriminator);
124124
}
125125
}

core/src/main/java/com/linecorp/armeria/server/docs/DiscriminatorInfo.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
@UnstableApi
4040
public final class DiscriminatorInfo {
4141

42-
private final String propertyName;
43-
private final Map<String, String> mapping;
44-
4542
/**
4643
* Creates a new {@link DiscriminatorInfo} with {@code propertyName}, the name
4744
* of the property
@@ -52,6 +49,9 @@ public static DiscriminatorInfo of(String propertyName, Map<String, String> mapp
5249
return new DiscriminatorInfo(propertyName, mapping);
5350
}
5451

52+
private final String propertyName;
53+
private final Map<String, String> mapping;
54+
5555
/**
5656
* Creates a new instance.
5757
*/
@@ -74,7 +74,7 @@ public String propertyName() {
7474
* The keys are the values that appear in the {@link #propertyName()} field, and
7575
* the values are
7676
* the schema definitions to use for that value (e.g.,
77-
* {@code "#/$defs/models/Cat"}).
77+
* {@code "#/$defs/models/com.linecorp.armeria.Cat"}).
7878
*/
7979
@JsonProperty
8080
public Map<String, String> mapping() {
@@ -101,6 +101,6 @@ public int hashCode() {
101101
@Override
102102
public String toString() {
103103
return MoreObjects.toStringHelper(this).add("propertyName", propertyName).add("mapping", mapping)
104-
.toString();
104+
.toString();
105105
}
106106
}

core/src/main/java/com/linecorp/armeria/server/docs/JsonSchemaGenerator.java

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
*/
1616
package com.linecorp.armeria.server.docs;
1717

18-
import static com.google.common.collect.ImmutableMap.toImmutableMap;
1918
import static java.util.Objects.requireNonNull;
2019

2120
import java.util.ArrayList;
2221
import java.util.HashMap;
2322
import java.util.List;
2423
import java.util.Locale;
2524
import java.util.Map;
26-
import java.util.function.Function;
2725

2826
import com.fasterxml.jackson.databind.ObjectMapper;
2927
import com.fasterxml.jackson.databind.node.ArrayNode;
3028
import com.fasterxml.jackson.databind.node.ObjectNode;
31-
import com.google.common.collect.ImmutableMap;
3229

3330
import com.linecorp.armeria.internal.common.JacksonUtil;
3431

@@ -42,28 +39,13 @@ final class JsonSchemaGenerator {
4239
private static final ObjectMapper mapper = JacksonUtil.newDefaultObjectMapper();
4340

4441
private final ServiceSpecification serviceSpecification;
45-
private final Map<String, StructInfo> structs;
46-
private final Map<String, EnumInfo> enums;
4742
private final Map<String, DiscriminatorInfo> polymorphismToBase;
4843
private final Map<String, DescriptionInfo> docStrings;
4944

5045
private JsonSchemaGenerator(ServiceSpecification serviceSpecification) {
5146
this.serviceSpecification = requireNonNull(serviceSpecification, "serviceSpecification");
5247
docStrings = serviceSpecification.docStrings();
5348

54-
final ImmutableMap.Builder<String, StructInfo> structsBuilder = ImmutableMap
55-
.builderWithExpectedSize(serviceSpecification.structs().size());
56-
for (final StructInfo structInfo : serviceSpecification.structs()) {
57-
structsBuilder.put(structInfo.name(), structInfo);
58-
if (structInfo.alias() != null) {
59-
structsBuilder.put(structInfo.alias(), structInfo);
60-
}
61-
}
62-
structs = structsBuilder.build();
63-
64-
enums = serviceSpecification.enums().stream()
65-
.collect(toImmutableMap(EnumInfo::name, Function.identity()));
66-
6749
// Pre-compute mappings from subtype to its base type's DiscriminatorInfo
6850
final Map<String, String> nameToAlias = new HashMap<>();
6951
for (final StructInfo struct : serviceSpecification.structs()) {
@@ -146,6 +128,19 @@ private static String getSchemaType(TypeSignature typeSignature) {
146128
}
147129
}
148130

131+
private static ObjectNode generateEnumDefinition(EnumInfo enumInfo) {
132+
final ObjectNode schemaNode = mapper.createObjectNode();
133+
schemaNode.put("type", "string");
134+
final String docString = enumInfo.descriptionInfo().docString();
135+
if (!docString.isEmpty()) {
136+
schemaNode.put("description", docString);
137+
}
138+
final ArrayNode enumValues = mapper.createArrayNode();
139+
enumInfo.values().forEach(value -> enumValues.add(value.name()));
140+
schemaNode.set("enum", enumValues);
141+
return schemaNode;
142+
}
143+
149144
private ObjectNode doGenerate() {
150145
final ObjectNode root = mapper.createObjectNode();
151146
if (serviceSpecification.services().isEmpty()) {
@@ -252,19 +247,6 @@ private ObjectNode generateStructDefinition(StructInfo structInfo) {
252247
return schemaNode;
253248
}
254249

255-
private static ObjectNode generateEnumDefinition(EnumInfo enumInfo) {
256-
final ObjectNode schemaNode = mapper.createObjectNode();
257-
schemaNode.put("type", "string");
258-
final String docString = enumInfo.descriptionInfo().docString();
259-
if (!docString.isEmpty()) {
260-
schemaNode.put("description", docString);
261-
}
262-
final ArrayNode enumValues = mapper.createArrayNode();
263-
enumInfo.values().forEach(value -> enumValues.add(value.name()));
264-
schemaNode.set("enum", enumValues);
265-
return schemaNode;
266-
}
267-
268250
private ObjectNode generateMethodSchema(String serviceName, MethodInfo methodInfo) {
269251
final ObjectNode root = mapper.createObjectNode();
270252
root.put("$id", methodInfo.id());
@@ -322,13 +304,13 @@ private ObjectNode generateFieldSchema(FieldInfo field) {
322304
}
323305

324306
if (typeSignature.type() == TypeSignatureType.STRUCT ||
325-
typeSignature.type() == TypeSignatureType.ENUM) {
307+
typeSignature.type() == TypeSignatureType.ENUM) {
326308
fieldNode.put("$ref", "#/$defs/models/" + typeSignature.name());
327309
return fieldNode;
328310
}
329311

330312
if (typeSignature.type() == TypeSignatureType.OPTIONAL ||
331-
typeSignature.type() == TypeSignatureType.CONTAINER) {
313+
typeSignature.type() == TypeSignatureType.CONTAINER) {
332314
final TypeSignature inner = ((ContainerTypeSignature) typeSignature).typeParameters().get(0);
333315
final ObjectNode innerNode = generateFieldSchema(FieldInfo.of("", inner));
334316
if (!docString.isEmpty()) {
@@ -349,7 +331,7 @@ private ObjectNode generateFieldSchema(FieldInfo field) {
349331
case MAP: {
350332
final TypeSignature valueType = ((MapTypeSignature) typeSignature).valueTypeSignature();
351333
fieldNode.set("additionalProperties",
352-
generateFieldSchema(FieldInfo.of("", valueType)));
334+
generateFieldSchema(FieldInfo.of("", valueType)));
353335
break;
354336
}
355337
default:

0 commit comments

Comments
 (0)