1515 */
1616package com .linecorp .armeria .server .docs ;
1717
18- import static com .google .common .collect .ImmutableMap .toImmutableMap ;
1918import static java .util .Objects .requireNonNull ;
2019
2120import java .util .ArrayList ;
2221import java .util .HashMap ;
2322import java .util .List ;
2423import java .util .Locale ;
2524import java .util .Map ;
26- import java .util .function .Function ;
2725
2826import com .fasterxml .jackson .databind .ObjectMapper ;
2927import com .fasterxml .jackson .databind .node .ArrayNode ;
3028import com .fasterxml .jackson .databind .node .ObjectNode ;
31- import com .google .common .collect .ImmutableMap ;
3229
3330import 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