-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add migration function from v2 to v1 API (#607)
- Loading branch information
1 parent
3f27890
commit d8c3c03
Showing
32 changed files
with
1,421 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { xParserMessageName, xParserSchemaId } from '../constants'; | ||
import { traverseAsyncApiDocument } from '../iterator'; | ||
import { setExtension } from '../utils'; | ||
|
||
import type { | ||
AsyncAPIDocumentInterface, | ||
SchemaInterface | ||
} from '../models'; | ||
|
||
export function anonymousNaming(document: AsyncAPIDocumentInterface) { | ||
assignNameToComponentMessages(document); | ||
assignNameToAnonymousMessages(document); | ||
|
||
assignUidToComponentSchemas(document); | ||
assignUidToComponentParameterSchemas(document); | ||
assignUidToChannelParameterSchemas(document); | ||
assignUidToAnonymousSchemas(document); | ||
} | ||
|
||
function assignNameToComponentMessages(document: AsyncAPIDocumentInterface) { | ||
document.components().messages().forEach(message => { | ||
if (message.name() === undefined) { | ||
setExtension(xParserMessageName, message.id(), message); | ||
} | ||
}); | ||
} | ||
|
||
function assignNameToAnonymousMessages(document: AsyncAPIDocumentInterface) { | ||
let anonymousMessageCounter = 0; | ||
document.messages().forEach(message => { | ||
if (message.name() === undefined && message.extensions().get(xParserMessageName) === undefined) { | ||
setExtension(xParserMessageName, `<anonymous-message-${++anonymousMessageCounter}>`, message); | ||
} | ||
}); | ||
} | ||
|
||
function assignUidToComponentParameterSchemas(document: AsyncAPIDocumentInterface) { | ||
document.components().channelParameters().forEach(parameter => { | ||
const schema = parameter.schema(); | ||
if (schema && !schema.uid()) { | ||
setExtension(xParserSchemaId, parameter.id(), schema); | ||
} | ||
}); | ||
} | ||
|
||
function assignUidToChannelParameterSchemas(document: AsyncAPIDocumentInterface) { | ||
document.channels().forEach(channel => { | ||
channel.parameters().forEach(parameter => { | ||
const schema = parameter.schema(); | ||
if (schema && !schema.uid()) { | ||
setExtension(xParserSchemaId, parameter.id(), schema); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function assignUidToComponentSchemas(document: AsyncAPIDocumentInterface) { | ||
document.components().schemas().forEach(schema => { | ||
setExtension(xParserSchemaId, schema.uid(), schema); | ||
}); | ||
} | ||
|
||
function assignUidToAnonymousSchemas(doc: AsyncAPIDocumentInterface) { | ||
let anonymousSchemaCounter = 0; | ||
function callback(schema: SchemaInterface) { | ||
if (!schema.uid()) { | ||
setExtension(xParserSchemaId, `<anonymous-schema-${++anonymousSchemaCounter}>`, schema); | ||
} | ||
} | ||
traverseAsyncApiDocument(doc, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { setExtension } from '../utils'; | ||
import { xParserCircular } from '../constants'; | ||
|
||
import type { AsyncAPIDocumentInterface } from '../models'; | ||
|
||
export function checkCircularRefs(document: AsyncAPIDocumentInterface) { | ||
if (hasInlineRef(document.json())) { | ||
setExtension(xParserCircular, true, document); | ||
} | ||
} | ||
|
||
function hasInlineRef(data: Record<string, any>): boolean { | ||
if (data && typeof data === 'object' && !Array.isArray(data)) { | ||
if (Object.prototype.hasOwnProperty.call(data, '$ref')) { | ||
return true; | ||
} | ||
for (const p in data) { | ||
if (hasInlineRef(data[p])) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.