Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ async function validate(input: ValidateSchemaInput<unknown, unknown>): Promise<S
}

async function parse(input: ParseSchemaInput<unknown, unknown>): Promise<SpecTypesV2.SchemaObject> {
const transformed = toJsonSchema(input.data, {
// cloning input, because input schema is modified during transformation despite setting cloneSchema
const inputSchema = structuredClone(input.data);
const transformed = toJsonSchema(inputSchema, {
cloneSchema: true,
keepNotSupported: [
'discriminator',
Expand Down Expand Up @@ -119,4 +121,4 @@ function getAjvInstance(): Ajv {
ajv.addSchema(jsonSchemaV3, 'openapi');

return ajv;
}
}
62 changes: 62 additions & 0 deletions test/documents/asyncapi3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
asyncapi: 3.0.0
info:
title: Address change events
version: 1.0.0
defaultContentType: application/json
channels:
AddressEventsChannel:
messages:
addressAdded:
$ref: '#/components/messages/AddressAddedEvent'
addressUpdated:
$ref: '#/components/messages/AddressUpdatedEvent'
operations:
receiveAddressNotification:
action: receive
channel:
$ref: '#/channels/AddressEventsChannel'
components:
messages:
AddressAddedEvent:
payload:
schemaFormat: application/vnd.oai.openapi+yaml;version=3.0.0
schema:
$ref: '#/components/schemas/AddressAddedEvent'
AddressUpdatedEvent:
payload:
schemaFormat: application/vnd.oai.openapi+yaml;version=3.0.0
schema:
$ref: '#/components/schemas/AddressUpdatedEvent'
schemas:
EventBase:
type: object
properties:
time2:
type: string
nullable: true
AddressEvent:
allOf:
- $ref: "#/components/schemas/EventBase"
type: object
properties:
type:
type: string
time:
type: string
nullable: true
required:
- type
AddressAddedEvent:
type: object
allOf:
- $ref: "#/components/schemas/AddressEvent"
properties:
addedAddress:
type: string
AddressUpdatedEvent:
allOf:
- $ref: "#/components/schemas/AddressEvent"
type: object
properties:
updatedAddress:
type: string
9 changes: 8 additions & 1 deletion test/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const inputWithInvalidOpenApi3 = toParseInput(fs.readFileSync(path.resolve(__dir

const inputWithValidAsyncAPI = fs.readFileSync(path.resolve(__dirname, './documents/valid-asyncapi.yaml'), 'utf8');

const inputWithValidAsyncAPI3 = fs.readFileSync(path.resolve(__dirname, './documents/asyncapi3.yaml'), 'utf8');

const inputWithInvalidAsyncAPI = fs.readFileSync(path.resolve(__dirname, './documents/invalid-asyncapi.yaml'), 'utf8');

describe('OpenAPISchemaParser', function () {
Expand Down Expand Up @@ -70,6 +72,11 @@ describe('OpenAPISchemaParser', function () {
doParseCoreTest((document?.json()?.components?.messages?.testMessage as any)?.payload, outputWithValidOpenApi3);
});

it('should parse valid AsyncAPI3', async function() {
const { document, diagnostics } = await coreParser.parse(inputWithValidAsyncAPI3);
expect(diagnostics).toHaveLength(0);
});

it('should validate valid AsyncAPI', async function() {
const diagnostics = await coreParser.validate(inputWithValidAsyncAPI);
expect(filterDiagnostics(diagnostics, 'asyncapi2-schemas')).toHaveLength(0);
Expand Down Expand Up @@ -181,4 +188,4 @@ function filterDiagnostics(diagnostics: Diagnostic[], code: string) {

function expectDiagnostics(diagnostics: Diagnostic[], code: string, results: SchemaValidateResult[]) {
expect(filterDiagnostics(diagnostics, code)).toEqual(results.map(e => expect.objectContaining(e)));
}
}