11package com .sngular .api .generator .plugin .asyncapi .handler ;
22
3+ import java .io .File ;
4+ import java .io .IOException ;
5+ import java .io .InputStream ;
6+ import java .net .URISyntaxException ;
7+ import java .util .Arrays ;
8+ import java .util .Collections ;
9+ import java .util .HashMap ;
10+ import java .util .Iterator ;
11+ import java .util .List ;
12+ import java .util .Map ;
13+ import java .util .Map .Entry ;
14+ import java .util .Objects ;
15+
316import com .fasterxml .jackson .databind .JsonNode ;
417import com .fasterxml .jackson .databind .ObjectMapper ;
5- import com .sngular .api .generator .plugin .asyncapi .exception .*;
18+ import com .sngular .api .generator .plugin .asyncapi .exception .ChannelNameException ;
19+ import com .sngular .api .generator .plugin .asyncapi .exception .DuplicatedOperationException ;
20+ import com .sngular .api .generator .plugin .asyncapi .exception .ExternalRefComponentNotFoundException ;
21+ import com .sngular .api .generator .plugin .asyncapi .exception .FileSystemException ;
22+ import com .sngular .api .generator .plugin .asyncapi .exception .InvalidAsyncAPIException ;
23+ import com .sngular .api .generator .plugin .asyncapi .exception .InvalidAvroException ;
624import com .sngular .api .generator .plugin .asyncapi .model .ProcessBindingsResult ;
725import com .sngular .api .generator .plugin .asyncapi .model .ProcessMethodResult ;
826import com .sngular .api .generator .plugin .asyncapi .parameter .OperationParameterObject ;
2038import org .apache .commons .lang3 .StringUtils ;
2139import org .apache .commons .lang3 .tuple .Pair ;
2240
23- import java .io .File ;
24- import java .io .IOException ;
25- import java .io .InputStream ;
26- import java .net .URISyntaxException ;
27- import java .util .*;
28- import java .util .Map .Entry ;
29-
3041public class AsyncApi2Handler extends BaseAsyncApiHandler {
3142
32- public AsyncApi2Handler (final Integer springBootVersion ,
33- boolean overwriteModel ,
34- final File targetFolder ,
35- final String processedGeneratedSourcesFolder ,
36- final String groupId ,
37- final File baseDir ) {
43+ public AsyncApi2Handler (
44+ final Integer springBootVersion ,
45+ boolean overwriteModel ,
46+ final File targetFolder ,
47+ final String processedGeneratedSourcesFolder ,
48+ final String groupId ,
49+ final File baseDir ) {
3850 super (springBootVersion , overwriteModel , targetFolder , processedGeneratedSourcesFolder , groupId , baseDir );
3951 }
4052
@@ -83,15 +95,15 @@ protected Map<String, JsonNode> getAllSchemas(final FileLocation ymlParent, fina
8395
8496 ApiTool .getComponent (node , SCHEMAS ).forEachRemaining (
8597 schema -> totalSchemas .putIfAbsent (SCHEMAS .toUpperCase () + SLASH + MapperUtil .getSchemaKey (schema .getKey ()), schema .getValue ())
86- );
98+ );
8799
88100 ApiTool .getComponent (node , MESSAGES ).forEachRemaining (
89101 message -> getMessageSchemas (message .getKey (), message .getValue (), ymlParent , totalSchemas )
90- );
102+ );
91103
92104 getChannels (node ).forEachRemaining (
93105 channel -> getChannelSchemas (channel .getValue (), totalSchemas , ymlParent )
94- );
106+ );
95107
96108 return totalSchemas ;
97109 }
@@ -132,7 +144,8 @@ protected void processSupplierMethod(
132144
133145 @ Override
134146 protected void processStreamBridgeMethod (
135- final String operationId , final JsonNode channel , final OperationParameterObject operationObject , final FileLocation ymlParent , final String channelName , final Map <String , JsonNode > totalSchemas )
147+ final String operationId , final JsonNode channel , final OperationParameterObject operationObject , final FileLocation ymlParent , final String channelName ,
148+ final Map <String , JsonNode > totalSchemas )
136149 throws IOException {
137150 final ProcessMethodResult result = processMethod (operationId , channel , operationObject , ymlParent , totalSchemas );
138151 final String regex = "[a-zA-Z0-9.\\ -]*" ;
@@ -153,7 +166,8 @@ protected void processSubscribeMethod(
153166 }
154167
155168 @ Override
156- protected void fillTemplateFactory (final String operationId ,
169+ protected void fillTemplateFactory (
170+ final String operationId ,
157171 final ProcessMethodResult processedMethod , final Map <String , JsonNode > totalSchemas , final OperationParameterObject operationObject )
158172 throws IOException {
159173 final String classFullName = processedMethod .getNamespace ();
@@ -238,7 +252,7 @@ protected String processMessageRef(final JsonNode messageBody, final String mode
238252 if (messageContent .startsWith ("#" )) {
239253 namespace = processModelPackage (MapperUtil .getLongRefClass (messageBody ), modelPackage );
240254 } else if (messageContent .contains ("#" ) || StringUtils .endsWith (messageContent , "yml" )
241- || StringUtils .endsWith (messageContent , "yaml" ) || StringUtils .endsWith (messageContent , "json" )) {
255+ || StringUtils .endsWith (messageContent , "yaml" ) || StringUtils .endsWith (messageContent , "json" )) {
242256 namespace = processExternalRef (modelPackage , ymlParent , messageBody );
243257 } else {
244258 namespace = processExternalAvro (ymlParent , messageContent );
@@ -261,7 +275,9 @@ protected String processExternalAvro(final FileLocation ymlParent, final String
261275 final JsonNode fileTree = mapper .readTree (avroFile );
262276 final JsonNode avroNamespace = fileTree .get ("namespace" );
263277
264- if (avroNamespace == null ) throw new InvalidAvroException (avroFilePath );
278+ if (avroNamespace == null ) {
279+ throw new InvalidAvroException (avroFilePath );
280+ }
265281
266282 namespace = avroNamespace .asText () + PACKAGE_SEPARATOR + fileTree .get ("name" ).asText ();
267283 } catch (final IOException e ) {
@@ -292,7 +308,8 @@ protected String processExternalRef(final String modelPackage, final FileLocatio
292308 }
293309
294310 @ Override
295- protected void processBindings (final ProcessBindingsResult .ProcessBindingsResultBuilder bindingsResult , final JsonNode message ,
311+ protected void processBindings (
312+ final ProcessBindingsResult .ProcessBindingsResultBuilder bindingsResult , final JsonNode message ,
296313 final CommonSpecFile commonSpecFile ) {
297314 if (message .has (BINDINGS )) {
298315 final var bindingsNode = message .get (BINDINGS );
@@ -335,6 +352,27 @@ protected String processModelPackage(final String extractedPackage, final String
335352 return processedPackage ;
336353 }
337354
355+ @ Override
356+ protected JsonNode getChannelFromOperation (final JsonNode openApi , final JsonNode operation ) {
357+ return operation ;
358+ }
359+
360+ @ Override
361+ protected String getOperationId (final JsonNode channel ) {
362+ final JsonNode channelDefinition = getChannelDefinition (channel );
363+ if (!channelDefinition .has (OPERATION_ID )) {
364+ throw new InvalidAsyncAPIException ("Operation ID is required" );
365+ }
366+
367+ final String operationId = channelDefinition .get (OPERATION_ID ).asText ();
368+ if (processedOperationIds .contains (operationId )) {
369+ throw new DuplicatedOperationException (operationId );
370+ }
371+
372+ processedOperationIds .add (operationId );
373+ return operationId ;
374+ }
375+
338376 private Iterator <Entry <String , JsonNode >> getChannels (final JsonNode node ) {
339377 return ApiTool .hasNode (node , CHANNELS ) ? ApiTool .getNode (node , CHANNELS ).fields () : Collections .emptyIterator ();
340378 }
@@ -408,25 +446,4 @@ private JsonNode getChannelDefinition(final JsonNode channel) {
408446
409447 return channelDefinition ;
410448 }
411-
412- @ Override
413- protected String getOperationId (final JsonNode channel ) {
414- final JsonNode channelDefinition = getChannelDefinition (channel );
415- if (!channelDefinition .has (OPERATION_ID )) {
416- throw new InvalidAsyncAPIException ("Operation ID is required" );
417- }
418-
419- final String operationId = channelDefinition .get (OPERATION_ID ).asText ();
420- if (processedOperationIds .contains (operationId )) {
421- throw new DuplicatedOperationException (operationId );
422- }
423-
424- processedOperationIds .add (operationId );
425- return operationId ;
426- }
427-
428- @ Override
429- protected JsonNode getChannelFromOperation (final JsonNode openApi , final JsonNode operation ) {
430- return operation ;
431- }
432449}
0 commit comments