@@ -16,6 +16,8 @@ import {
1616 type Oas3Definition ,
1717 type Oas2Definition ,
1818 type Exact ,
19+ type Async3Definition ,
20+ type Async2Definition ,
1921} from '@redocly/openapi-core' ;
2022import { blue , gray , green , red , yellow } from 'colorette' ;
2123import { hasMagic , glob } from 'glob' ;
@@ -446,58 +448,81 @@ export async function loadConfigAndHandleErrors(
446448 }
447449}
448450
449- export function sortTopLevelKeysForOas (
450- document : Oas3Definition | Oas2Definition
451- ) : Oas3Definition | Oas2Definition {
452- if ( 'swagger' in document ) {
453- return sortOas2Keys ( document ) ;
451+ function isAsync2Definition ( doc : Async2Definition | Async3Definition ) : doc is Async2Definition {
452+ return doc . asyncapi ?. startsWith ( '2' ) ;
453+ }
454+
455+ const oas2OrderedKeys = [
456+ 'swagger' ,
457+ 'info' ,
458+ 'host' ,
459+ 'basePath' ,
460+ 'schemes' ,
461+ 'consumes' ,
462+ 'produces' ,
463+ 'security' ,
464+ 'tags' ,
465+ 'externalDocs' ,
466+ 'paths' ,
467+ 'definitions' ,
468+ 'parameters' ,
469+ 'responses' ,
470+ 'securityDefinitions' ,
471+ ] ;
472+
473+ const oas3OrderedKeys = [
474+ 'openapi' ,
475+ 'info' ,
476+ 'jsonSchemaDialect' ,
477+ 'servers' ,
478+ 'security' ,
479+ 'tags' ,
480+ 'externalDocs' ,
481+ 'paths' ,
482+ 'webhooks' ,
483+ 'x-webhooks' ,
484+ 'components' ,
485+ ] ;
486+
487+ const asyncApi2OrderedKeys = [
488+ 'asyncapi' ,
489+ 'id' ,
490+ 'info' ,
491+ 'externalDocs' ,
492+ 'tags' ,
493+ 'defaultContentType' ,
494+ 'servers' ,
495+ 'channels' ,
496+ 'components' ,
497+ ] ;
498+
499+ const asyncApi3OrderedKeys = [
500+ 'asyncapi' ,
501+ 'info' ,
502+ 'defaultContentType' ,
503+ 'servers' ,
504+ 'channels' ,
505+ 'operations' ,
506+ 'components' ,
507+ ] ;
508+
509+ export function sortTopLevelKeys (
510+ document : Oas3Definition | Oas2Definition | Async2Definition | Async3Definition
511+ ) : Oas3Definition | Oas2Definition | Async2Definition | Async3Definition {
512+ if ( 'asyncapi' in document ) {
513+ return isAsync2Definition ( document )
514+ ? sortDocumentKeys ( document , asyncApi2OrderedKeys )
515+ : sortDocumentKeys ( document , asyncApi3OrderedKeys ) ;
454516 }
455- return sortOas3Keys ( document as Oas3Definition ) ;
456- }
457-
458- function sortOas2Keys ( document : Oas2Definition ) : Oas2Definition {
459- const orderedKeys = [
460- 'swagger' ,
461- 'info' ,
462- 'host' ,
463- 'basePath' ,
464- 'schemes' ,
465- 'consumes' ,
466- 'produces' ,
467- 'security' ,
468- 'tags' ,
469- 'externalDocs' ,
470- 'paths' ,
471- 'definitions' ,
472- 'parameters' ,
473- 'responses' ,
474- 'securityDefinitions' ,
475- ] ;
476- const result : any = { } ;
477- for ( const key of orderedKeys as ( keyof Oas2Definition ) [ ] ) {
478- if ( document . hasOwnProperty ( key ) ) {
479- result [ key ] = document [ key ] ;
480- }
517+ if ( 'swagger' in document ) {
518+ return sortDocumentKeys ( document , oas2OrderedKeys ) ;
481519 }
482- // merge any other top-level keys (e.g. vendor extensions)
483- return Object . assign ( result , document ) ;
520+ return sortDocumentKeys ( document , oas3OrderedKeys ) ;
484521}
485- function sortOas3Keys ( document : Oas3Definition ) : Oas3Definition {
486- const orderedKeys = [
487- 'openapi' ,
488- 'info' ,
489- 'jsonSchemaDialect' ,
490- 'servers' ,
491- 'security' ,
492- 'tags' ,
493- 'externalDocs' ,
494- 'paths' ,
495- 'webhooks' ,
496- 'x-webhooks' ,
497- 'components' ,
498- ] ;
522+
523+ function sortDocumentKeys < T extends object > ( document : T , orderedKeys : string [ ] ) : T {
499524 const result : any = { } ;
500- for ( const key of orderedKeys as ( keyof Oas3Definition ) [ ] ) {
525+ for ( const key of orderedKeys as ( keyof T ) [ ] ) {
501526 if ( document . hasOwnProperty ( key ) ) {
502527 result [ key ] = document [ key ] ;
503528 }
0 commit comments