@@ -8,6 +8,7 @@ import {AsyncAPISchemaDefinition} from '@asyncapi/parser/esm/spec-types/v3';
88
99const ROOT_FILENAME = 'root' ;
1010const COMMENT_ROOT_NODE = '@RootNode' ;
11+ const COMMENT_OPTION = '@Option' ;
1112const COMMENT_EXAMPLE = '@Example' ;
1213const COMMENT_DEFAULT = '@Default' ;
1314
@@ -18,11 +19,39 @@ class Proto2JsonSchema {
1819 keepCase : true ,
1920 alternateCommentMode : true
2021 } ;
22+ private mapperOptions : { [ key : string ] : string | boolean } = {
23+ primitiveTypesWithLimits : true
24+ } ;
2125
2226 constructor ( rawSchema : string ) {
27+ this . parseOptionsAnnotation ( rawSchema ) ;
28+
2329 this . process ( ROOT_FILENAME , rawSchema ) ;
2430 }
2531
32+ private parseOptionsAnnotation ( rawSchema : string ) {
33+ const regex = / \s * ( \/ \/ | \* ) \s * @ O p t i o n \s + (?< key > \w { 1 , 50 } ) \s + (?< value > [ ^ \r \n ] { 1 , 200 } ) / g;
34+ let m : RegExpExecArray | null ;
35+ while ( ( m = regex . exec ( rawSchema ) ) !== null ) {
36+ // This is necessary to avoid infinite loops with zero-width matches
37+ if ( m . index === regex . lastIndex ) {
38+ regex . lastIndex ++ ;
39+ }
40+
41+ if ( m . groups === undefined ) {
42+ break ;
43+ }
44+
45+ if ( m . groups . value === 'true' ) {
46+ this . mapperOptions [ m . groups . key ] = true ;
47+ } else if ( m . groups . value === 'false' ) {
48+ this . mapperOptions [ m . groups . key ] = false ;
49+ } else {
50+ this . mapperOptions [ m . groups . key ] = m . groups . value ;
51+ }
52+ }
53+ }
54+
2655 private process ( filename : string , source : string | ProtoAsJson ) {
2756 if ( ! isString ( source ) ) {
2857 const srcObject = source as ProtoAsJson ;
@@ -182,7 +211,7 @@ class Proto2JsonSchema {
182211 */
183212 // eslint-disable-next-line sonarjs/cognitive-complexity
184213 private compileMessage ( item : protobuf . Type , stack : string [ ] ) : AsyncAPISchemaDefinition {
185- const properties : { [ key : string ] : AsyncAPISchemaDefinition } = { } ;
214+ const properties : { [ key : string ] : AsyncAPISchemaDefinition } = { } ;
186215
187216 const obj : v3 . AsyncAPISchemaDefinition = {
188217 title : item . name ,
@@ -228,7 +257,7 @@ class Proto2JsonSchema {
228257 }
229258
230259 if ( field . comment ) {
231- const minItemsPattern = / @ m a x I t e m s \\ s ( \\ d + ?) / i;
260+ const minItemsPattern = / @ m i n I t e m s \\ s ( \\ d + ?) / i;
232261 const maxItemsPattern = / @ m a x I t e m s \\ s ( \\ d + ?) / i;
233262 let m : RegExpExecArray | null ;
234263 if ( ( m = minItemsPattern . exec ( field . comment ) ) !== null ) {
@@ -294,8 +323,10 @@ class Proto2JsonSchema {
294323 private compileField ( field : protobuf . Field , parentItem : protobuf . Type , stack : string [ ] ) : v3 . AsyncAPISchemaDefinition {
295324 let obj : v3 . AsyncAPISchemaDefinition = { } ;
296325
297- if ( PrimitiveTypes . PRIMITIVE_TYPES [ field . type . toLowerCase ( ) ] ) {
298- obj = Object . assign ( obj , PrimitiveTypes . PRIMITIVE_TYPES [ field . type . toLowerCase ( ) ] ) ;
326+ if ( PrimitiveTypes . PRIMITIVE_TYPES_WITH_LIMITS [ field . type . toLowerCase ( ) ] ) {
327+ obj = ( this . mapperOptions . primitiveTypesWithLimits ) ?
328+ Object . assign ( obj , PrimitiveTypes . PRIMITIVE_TYPES_WITH_LIMITS [ field . type . toLowerCase ( ) ] ) :
329+ Object . assign ( obj , PrimitiveTypes . PRIMITIVE_TYPES_MINIMAL [ field . type . toLowerCase ( ) ] ) ;
299330 obj [ 'x-primitive' ] = field . type ;
300331 } else {
301332 const item = parentItem . lookupTypeOrEnum ( field . type ) ;
@@ -339,6 +370,7 @@ class Proto2JsonSchema {
339370 comment = comment
340371 . replace ( new RegExp ( `\\s{0,15}${ COMMENT_EXAMPLE } \\s{0,15}(.+)` , 'ig' ) , '' )
341372 . replace ( new RegExp ( `\\s{0,15}${ COMMENT_DEFAULT } \\s{0,15}(.+)` , 'ig' ) , '' )
373+ . replace ( new RegExp ( `\\s{0,15}${ COMMENT_OPTION } \\s{0,15}(.+)` , 'ig' ) , '' )
342374 . replace ( new RegExp ( `\\s{0,15}${ COMMENT_ROOT_NODE } ` , 'ig' ) , '' )
343375 . replace ( new RegExp ( '\\s{0,15}@(Min|Max|Pattern|Minimum|Maximum|ExclusiveMinimum|ExclusiveMaximum|MultipleOf|MaxLength|MinLength|MaxItems|MinItems)\\s{0,15}[\\d.]{1,20}' , 'ig' ) , '' )
344376 . trim ( ) ;
@@ -350,12 +382,12 @@ class Proto2JsonSchema {
350382 return comment ;
351383 }
352384
353- private extractExamples ( comment : string | null ) : ( string | ProtoAsJson ) [ ] | null {
385+ private extractExamples ( comment : string | null ) : ( string | ProtoAsJson ) [ ] | null {
354386 if ( ! comment ) {
355387 return null ;
356388 }
357389
358- const examples : ( string | ProtoAsJson ) [ ] = [ ] ;
390+ const examples : ( string | ProtoAsJson ) [ ] = [ ] ;
359391
360392 let m : RegExpExecArray | null ;
361393 const examplePattern = new RegExp ( `\\s*${ COMMENT_EXAMPLE } \\s(.+)$` , 'i' ) ;
@@ -413,6 +445,7 @@ class Proto2JsonSchema {
413445 }
414446 }
415447 }
448+
416449 /* eslint-enable security/detect-unsafe-regex */
417450
418451 private addDefaultFromCommentAnnotations ( obj : AsyncAPISchemaDefinition , comment : string | null ) {
@@ -442,7 +475,7 @@ function tryParseToObject(value: string): string | ProtoAsJson {
442475 try {
443476 const json = JSON . parse ( value ) ;
444477 if ( json ) {
445- return json ;
478+ return json ;
446479 }
447480 } catch ( _ ) {
448481 // Ignored error, seams not to be a valid json. Maybe just an example starting with an "{" but is not a json.
0 commit comments