Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable validation of AsyncAPI Spec v3 documents in validate command #780

Merged
merged 6 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
14,682 changes: 8,407 additions & 6,275 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"@asyncapi/modelina": "^1.9.0",
"@asyncapi/openapi-schema-parser": "^3.0.3",
"@asyncapi/optimizer": "^0.2.1",
"@asyncapi/parser": "^2.1.0",
"@asyncapi/raml-dt-schema-parser": "^4.0.3",
"@asyncapi/parser": "^3.0.0-next-major-spec.2",
"@asyncapi/protobuf-schema-parser": "3.0.0",
"@asyncapi/raml-dt-schema-parser": "^4.0.3",
"@asyncapi/studio": "^0.17.3",
"@oclif/core": "^1.26.2",
"@oclif/errors": "^1.3.6",
"@oclif/plugin-not-found": "^2.3.22",
"@smoya/multi-parser": "^3.0.0",
"@stoplight/spectral-cli": "6.9.0",
"ajv": "^8.12.0",
"chalk": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { ConvertVersion } from '@asyncapi/converter';
// @ts-ignore
import specs from '@asyncapi/specs';

const latestVersion = Object.keys(specs).pop() as string;
const latestVersion = Object.keys(specs.schemas).pop() as string;

export default class Convert extends Command {
static description = 'Convert asyncapi documents older to newer versions';
Expand Down
10 changes: 8 additions & 2 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, CSHARP_NEWTONSOFT_SERIALIZER_PRESET, CSHARP_COMMON_PRESET, CSHARP_JSON_SERIALIZER_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import { ConvertDocumentParserAPIVersion } from '@smoya/multi-parser';
import Command from '../../base';
import { load } from '../../models/SpecificationFile';
import { formatOutput, parse, validationFlags } from '../../parser';
Expand Down Expand Up @@ -161,6 +162,11 @@ export default class Models extends Command {
this.log(`Input is not a correct AsyncAPI document so it cannot be processed.${formatOutput(severityErrors,'stylish','error')}`);
return;
}

// Modelina, atm, is not using @asyncapi/[email protected] but @asyncapi/[email protected], so it still uses Parser-API v1.0.0.
// This call converts the parsed document object using @asyncapi/[email protected] (Parser-API v2.0.0) to a document compatible with the Parser-API version in use in @asyncapi/[email protected] (v1.0.0)
// This is needed until https://github.com/asyncapi/modelina/issues/1493 gets fixed.
const convertedDoc = ConvertDocumentParserAPIVersion(document.json(), '1.0.0');

Logger.setLogger({
info: (message) => {
Expand Down Expand Up @@ -314,7 +320,7 @@ export default class Models extends Command {

if (output) {
const models = await fileGenerator.generateToFiles(
document as any,
convertedDoc as any,
output,
{ ...fileOptions, } as any);
const generatedModels = models.map((model) => { return model.modelName; });
Expand All @@ -323,7 +329,7 @@ export default class Models extends Command {
}

const models = await fileGenerator.generateCompleteModels(
document as any,
convertedDoc as any,
{ ...fileOptions } as any);
const generatedModels = models.map((model) => {
return `
Expand Down
1 change: 1 addition & 0 deletions test.asyncapi-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"store":{}}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed @Souvikns ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can add it to the gitignore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

48 changes: 48 additions & 0 deletions test/fixtures/valid-specification-latest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
asyncapi: 3.0.0
id: urn:some:company
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0
contact:
name: API Support
url: https://www.example.com/support
email: [email protected]
tags:
- name: some-tag
defaultContentType: 'application/json'
servers:
production:
host: development.gigantic-server.com
description: Development server
protocol: kafka
protocolVersion: '1.0.0'
channels:
UserSignedUp:
address: 'user/signedup'
messages:
UserSignedUp:
$ref: '#/components/messages/UserSignedUp'
operations:
UserSignedUp:
description: User signedup
action: 'send'
channel:
$ref: '#/channels/UserSignedUp'
components:
messages:
UserSignedUp:
messageId: UserSignedUp
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
8 changes: 4 additions & 4 deletions test/integration/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ describe('validate', () => {
test
.stderr()
.stdout()
.command(['validate', './test/fixtures/valid-specification.yml'])
.command(['validate', './test/fixtures/valid-specification-latest.yml'])
.it('works when file path is passed', (ctx, done) => {
expect(ctx.stdout).toMatch('File ./test/fixtures/valid-specification.yml is valid! File ./test/fixtures/valid-specification.yml and referenced documents don\'t have governance issues.');
expect(ctx.stdout).toMatch('File ./test/fixtures/valid-specification-latest.yml is valid! File ./test/fixtures/valid-specification-latest.yml and referenced documents don\'t have governance issues.');
expect(ctx.stderr).toEqual('');
done();
});
Expand Down Expand Up @@ -221,9 +221,9 @@ describe('validate', () => {
test
.stderr()
.stdout()
.command(['validate', './test/fixtures/valid-specification.yml', '--diagnostics-format=text'])
.command(['validate', './test/fixtures/valid-specification-latest.yml', '--diagnostics-format=text'])
.it('works with --diagnostics-format flag (without governance issues)', (ctx, done) => {
expect(ctx.stdout).toMatch('File ./test/fixtures/valid-specification.yml is valid! File ./test/fixtures/valid-specification.yml and referenced documents don\'t have governance issues.');
expect(ctx.stdout).toMatch('File ./test/fixtures/valid-specification-latest.yml is valid! File ./test/fixtures/valid-specification-latest.yml and referenced documents don\'t have governance issues.');
expect(ctx.stderr).toEqual('');
done();
});
Expand Down