From f09b2d1590db9025165b2bf1923a30134ae9e449 Mon Sep 17 00:00:00 2001 From: "jonas-lt@live.dk" Date: Tue, 25 Jul 2023 13:24:38 +0200 Subject: [PATCH 1/5] fix implementation and tests --- src/models/v3/channel-parameter-schema.ts | 238 ++++++++++++++++++++++ src/models/v3/channel-parameter.ts | 12 +- src/spec-types/v3.ts | 4 +- test/models/v3/channel-parameter.spec.ts | 35 +++- 4 files changed, 279 insertions(+), 10 deletions(-) create mode 100644 src/models/v3/channel-parameter-schema.ts diff --git a/src/models/v3/channel-parameter-schema.ts b/src/models/v3/channel-parameter-schema.ts new file mode 100644 index 000000000..36a5f3190 --- /dev/null +++ b/src/models/v3/channel-parameter-schema.ts @@ -0,0 +1,238 @@ +import { BaseModel } from '../base'; + +import { xParserSchemaId } from '../../constants'; +import { extensions } from './mixins'; +import { retrievePossibleRef } from '../../utils'; + +import type { ModelMetadata } from '../base'; +import type { ExtensionsInterface } from '../extensions'; +import type { ExternalDocumentationInterface } from '../external-docs'; +import type { SchemaInterface } from '../schema'; + +import type { v3 } from '../../spec-types'; + +export class ParameterSchema extends BaseModel implements SchemaInterface { + constructor( + _json: v3.ParameterObject, + _meta: ModelMetadata & { } = {} as any, + ) { + _json = retrievePossibleRef(_json, _meta.pointer as string, _meta.asyncapi?.parsed); + super(_json, _meta); + } + + id(): string { + return this.$id() || this.json(xParserSchemaId as any) as string; + } + + $comment(): string | undefined { + return undefined; + } + + $id(): string | undefined { + return undefined; + } + + $schema(): string { + return 'http://json-schema.org/draft-07/schema#'; + } + + additionalItems(): boolean | SchemaInterface { + return false; + } + + additionalProperties(): boolean | SchemaInterface { + return false; + } + + allOf(): Array | undefined { + return undefined; + } + + anyOf(): Array | undefined { + return undefined; + } + + const(): any { + return undefined; + } + + contains(): SchemaInterface | undefined { + return undefined; + } + + contentEncoding(): string | undefined { + return undefined; + } + + contentMediaType(): string | undefined { + return undefined; + } + + default(): any { + return this._json.default; + } + + definitions(): Record | undefined { + return undefined; + } + + description(): string | undefined { + return this._json.description; + } + + dependencies(): Record> | undefined { + return undefined; + } + + deprecated(): boolean { + return false; + } + + discriminator(): string | undefined { + return undefined; + } + + else(): SchemaInterface | undefined { + return undefined; + } + + enum(): Array | undefined { + return this._json.enum; + } + + examples(): Array | undefined { + return this._json.examples; + } + + exclusiveMaximum(): number | undefined { + return undefined; + } + + exclusiveMinimum(): number | undefined { + return undefined; + } + + format(): string | undefined { + return undefined; + } + + isBooleanSchema(): boolean { + return false; + } + + if(): SchemaInterface | undefined { + return undefined; + } + + isCircular(): boolean { + return false; + } + + items(): SchemaInterface | Array | undefined { + return undefined; + } + + maximum(): number | undefined { + return undefined; + } + + maxItems(): number | undefined { + return undefined; + } + + maxLength(): number | undefined { + return undefined; + } + + maxProperties(): number | undefined { + return undefined; + } + + minimum(): number | undefined { + return undefined; + } + + minItems(): number | undefined { + return undefined; + } + + minLength(): number | undefined { + return undefined; + } + + minProperties(): number | undefined { + return undefined; + } + + multipleOf(): number | undefined { + return undefined; + } + + not(): SchemaInterface | undefined { + return undefined; + } + + oneOf(): Array | undefined { + return undefined; + } + + pattern(): string | undefined { + return undefined; + } + + patternProperties(): Record | undefined { + return undefined; + } + + properties(): Record | undefined { + return undefined; + } + + property(): SchemaInterface | undefined { + return undefined; + } + + propertyNames(): SchemaInterface | undefined { + return undefined; + } + + readOnly(): boolean | undefined { + return undefined; + } + + required(): Array | undefined { + return undefined; + } + + then(): SchemaInterface | undefined { + return undefined; + } + + title(): string | undefined { + return undefined; + } + + type(): string | Array | undefined { + return 'string'; + } + + uniqueItems(): boolean | undefined { + return undefined; + } + + writeOnly(): boolean | undefined { + return undefined; + } + + hasExternalDocs(): boolean { + return false; + } + + externalDocs(): ExternalDocumentationInterface | undefined { + return undefined; + } + + extensions(): ExtensionsInterface { + return extensions(this as BaseModel); + } +} diff --git a/src/models/v3/channel-parameter.ts b/src/models/v3/channel-parameter.ts index b5fc343e4..a8f17b803 100644 --- a/src/models/v3/channel-parameter.ts +++ b/src/models/v3/channel-parameter.ts @@ -1,5 +1,4 @@ import { BaseModel } from '../base'; -import { Schema } from './schema'; import { hasDescription, description, extensions } from './mixins'; @@ -8,6 +7,7 @@ import type { SchemaInterface } from '../schema'; import type { ExtensionsInterface } from '../extensions'; import type { v3 } from '../../spec-types'; +import { ParameterSchema } from './channel-parameter-schema'; export class ChannelParameter extends BaseModel implements ChannelParameterInterface { id(): string { @@ -15,12 +15,16 @@ export class ChannelParameter extends BaseModel export interface ParameterObject extends SpecificationExtensions { description?: string; - schema?: SchemaObject; + enum?: string[]; + default?: string; + examples?: string[]; location?: string; } diff --git a/test/models/v3/channel-parameter.spec.ts b/test/models/v3/channel-parameter.spec.ts index 66c6c76a1..bc9da91b7 100644 --- a/test/models/v3/channel-parameter.spec.ts +++ b/test/models/v3/channel-parameter.spec.ts @@ -4,6 +4,7 @@ import { Schema } from '../../../src/models/v3/schema'; import { serializeInput, assertDescription, assertExtensions } from './utils'; import type { v3 } from '../../../src/spec-types'; +import { ParameterSchema } from '../../../src/models/v3/channel-parameter-schema'; describe('ChannelParameter model', function() { describe('.id()', function() { @@ -43,12 +44,23 @@ describe('ChannelParameter model', function() { }); describe('.hasSchema()', function() { - it('should return true when there is a value', function() { - const doc = serializeInput({ schema: {} }); + it('should return true if enum are sat', function() { + const doc = serializeInput({ enum: ['test'] }); + const d = new ChannelParameter(doc); + expect(d.hasSchema()).toEqual(true); + }); + it('should return true if default are sat', function() { + const doc = serializeInput({ default: 'test' }); const d = new ChannelParameter(doc); expect(d.hasSchema()).toEqual(true); }); + it('should return true if examples are sat', function() { + const doc = serializeInput({ examples: ['test'] }); + const d = new ChannelParameter(doc); + expect(d.hasSchema()).toEqual(true); + }); + it('should return false when there is no value', function() { const doc = serializeInput({}); const d = new ChannelParameter(doc); @@ -57,10 +69,23 @@ describe('ChannelParameter model', function() { }); describe('.schema()', function() { - it('should return the value', function() { - const doc = serializeInput({ schema: {} }); + it('should be able to access enum values', function() { + const doc = serializeInput({ enum: ['test'] }); + const d = new ChannelParameter(doc); + expect(d.schema()).toBeInstanceOf(ParameterSchema); + expect(d.schema()?.enum()).toEqual(['test']); + }); + it('should be able to access examples values', function() { + const doc = serializeInput({ examples: ['test'] }); + const d = new ChannelParameter(doc); + expect(d.schema()).toBeInstanceOf(ParameterSchema); + expect(d.schema()?.examples()).toEqual(['test']); + }); + it('should be able to access default value', function() { + const doc = serializeInput({ default: 'test' }); const d = new ChannelParameter(doc); - expect(d.schema()).toBeInstanceOf(Schema); + expect(d.schema()).toBeInstanceOf(ParameterSchema); + expect(d.schema()?.default()).toEqual('test'); }); it('should return undefined when there is no value', function() { From cd9631d65b82debbe35334df55ce422b1f07d11e Mon Sep 17 00:00:00 2001 From: "jonas-lt@live.dk" Date: Tue, 25 Jul 2023 15:05:02 +0200 Subject: [PATCH 2/5] simplified implementation --- src/models/v3/channel-parameter-schema.ts | 238 ---------------------- src/models/v3/channel-parameter.ts | 9 +- test/models/v3/channel-parameter.spec.ts | 7 +- 3 files changed, 10 insertions(+), 244 deletions(-) delete mode 100644 src/models/v3/channel-parameter-schema.ts diff --git a/src/models/v3/channel-parameter-schema.ts b/src/models/v3/channel-parameter-schema.ts deleted file mode 100644 index 36a5f3190..000000000 --- a/src/models/v3/channel-parameter-schema.ts +++ /dev/null @@ -1,238 +0,0 @@ -import { BaseModel } from '../base'; - -import { xParserSchemaId } from '../../constants'; -import { extensions } from './mixins'; -import { retrievePossibleRef } from '../../utils'; - -import type { ModelMetadata } from '../base'; -import type { ExtensionsInterface } from '../extensions'; -import type { ExternalDocumentationInterface } from '../external-docs'; -import type { SchemaInterface } from '../schema'; - -import type { v3 } from '../../spec-types'; - -export class ParameterSchema extends BaseModel implements SchemaInterface { - constructor( - _json: v3.ParameterObject, - _meta: ModelMetadata & { } = {} as any, - ) { - _json = retrievePossibleRef(_json, _meta.pointer as string, _meta.asyncapi?.parsed); - super(_json, _meta); - } - - id(): string { - return this.$id() || this.json(xParserSchemaId as any) as string; - } - - $comment(): string | undefined { - return undefined; - } - - $id(): string | undefined { - return undefined; - } - - $schema(): string { - return 'http://json-schema.org/draft-07/schema#'; - } - - additionalItems(): boolean | SchemaInterface { - return false; - } - - additionalProperties(): boolean | SchemaInterface { - return false; - } - - allOf(): Array | undefined { - return undefined; - } - - anyOf(): Array | undefined { - return undefined; - } - - const(): any { - return undefined; - } - - contains(): SchemaInterface | undefined { - return undefined; - } - - contentEncoding(): string | undefined { - return undefined; - } - - contentMediaType(): string | undefined { - return undefined; - } - - default(): any { - return this._json.default; - } - - definitions(): Record | undefined { - return undefined; - } - - description(): string | undefined { - return this._json.description; - } - - dependencies(): Record> | undefined { - return undefined; - } - - deprecated(): boolean { - return false; - } - - discriminator(): string | undefined { - return undefined; - } - - else(): SchemaInterface | undefined { - return undefined; - } - - enum(): Array | undefined { - return this._json.enum; - } - - examples(): Array | undefined { - return this._json.examples; - } - - exclusiveMaximum(): number | undefined { - return undefined; - } - - exclusiveMinimum(): number | undefined { - return undefined; - } - - format(): string | undefined { - return undefined; - } - - isBooleanSchema(): boolean { - return false; - } - - if(): SchemaInterface | undefined { - return undefined; - } - - isCircular(): boolean { - return false; - } - - items(): SchemaInterface | Array | undefined { - return undefined; - } - - maximum(): number | undefined { - return undefined; - } - - maxItems(): number | undefined { - return undefined; - } - - maxLength(): number | undefined { - return undefined; - } - - maxProperties(): number | undefined { - return undefined; - } - - minimum(): number | undefined { - return undefined; - } - - minItems(): number | undefined { - return undefined; - } - - minLength(): number | undefined { - return undefined; - } - - minProperties(): number | undefined { - return undefined; - } - - multipleOf(): number | undefined { - return undefined; - } - - not(): SchemaInterface | undefined { - return undefined; - } - - oneOf(): Array | undefined { - return undefined; - } - - pattern(): string | undefined { - return undefined; - } - - patternProperties(): Record | undefined { - return undefined; - } - - properties(): Record | undefined { - return undefined; - } - - property(): SchemaInterface | undefined { - return undefined; - } - - propertyNames(): SchemaInterface | undefined { - return undefined; - } - - readOnly(): boolean | undefined { - return undefined; - } - - required(): Array | undefined { - return undefined; - } - - then(): SchemaInterface | undefined { - return undefined; - } - - title(): string | undefined { - return undefined; - } - - type(): string | Array | undefined { - return 'string'; - } - - uniqueItems(): boolean | undefined { - return undefined; - } - - writeOnly(): boolean | undefined { - return undefined; - } - - hasExternalDocs(): boolean { - return false; - } - - externalDocs(): ExternalDocumentationInterface | undefined { - return undefined; - } - - extensions(): ExtensionsInterface { - return extensions(this as BaseModel); - } -} diff --git a/src/models/v3/channel-parameter.ts b/src/models/v3/channel-parameter.ts index a8f17b803..4e36649e4 100644 --- a/src/models/v3/channel-parameter.ts +++ b/src/models/v3/channel-parameter.ts @@ -7,7 +7,7 @@ import type { SchemaInterface } from '../schema'; import type { ExtensionsInterface } from '../extensions'; import type { v3 } from '../../spec-types'; -import { ParameterSchema } from './channel-parameter-schema'; +import { Schema } from './schema'; export class ChannelParameter extends BaseModel implements ChannelParameterInterface { id(): string { @@ -24,7 +24,12 @@ export class ChannelParameter extends BaseModel({ enum: ['test'] }); const d = new ChannelParameter(doc); - expect(d.schema()).toBeInstanceOf(ParameterSchema); + expect(d.schema()).toBeInstanceOf(Schema); expect(d.schema()?.enum()).toEqual(['test']); }); it('should be able to access examples values', function() { const doc = serializeInput({ examples: ['test'] }); const d = new ChannelParameter(doc); - expect(d.schema()).toBeInstanceOf(ParameterSchema); + expect(d.schema()).toBeInstanceOf(Schema); expect(d.schema()?.examples()).toEqual(['test']); }); it('should be able to access default value', function() { const doc = serializeInput({ default: 'test' }); const d = new ChannelParameter(doc); - expect(d.schema()).toBeInstanceOf(ParameterSchema); + expect(d.schema()).toBeInstanceOf(Schema); expect(d.schema()?.default()).toEqual('test'); }); From 2b870f399055bbfe2d4dbd182a07deb99551b884 Mon Sep 17 00:00:00 2001 From: "jonas-lt@live.dk" Date: Tue, 25 Jul 2023 15:11:46 +0200 Subject: [PATCH 3/5] move import --- src/models/v3/channel-parameter.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/models/v3/channel-parameter.ts b/src/models/v3/channel-parameter.ts index 4e36649e4..3506e391b 100644 --- a/src/models/v3/channel-parameter.ts +++ b/src/models/v3/channel-parameter.ts @@ -1,13 +1,11 @@ import { BaseModel } from '../base'; - import { hasDescription, description, extensions } from './mixins'; +import { Schema } from './schema'; import type { ChannelParameterInterface } from '../channel-parameter'; import type { SchemaInterface } from '../schema'; import type { ExtensionsInterface } from '../extensions'; - import type { v3 } from '../../spec-types'; -import { Schema } from './schema'; export class ChannelParameter extends BaseModel implements ChannelParameterInterface { id(): string { From 505dc5423490e5dd5205610d9dc44bfbdbd605b7 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Tue, 25 Jul 2023 15:14:44 +0200 Subject: [PATCH 4/5] Update src/models/v3/channel-parameter.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- src/models/v3/channel-parameter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/models/v3/channel-parameter.ts b/src/models/v3/channel-parameter.ts index 3506e391b..1fedb64c9 100644 --- a/src/models/v3/channel-parameter.ts +++ b/src/models/v3/channel-parameter.ts @@ -24,6 +24,7 @@ export class ChannelParameter extends BaseModel Date: Tue, 25 Jul 2023 15:14:51 +0200 Subject: [PATCH 5/5] Update src/models/v3/channel-parameter.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maciej Urbańczyk --- src/models/v3/channel-parameter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/models/v3/channel-parameter.ts b/src/models/v3/channel-parameter.ts index 1fedb64c9..54099a351 100644 --- a/src/models/v3/channel-parameter.ts +++ b/src/models/v3/channel-parameter.ts @@ -14,6 +14,7 @@ export class ChannelParameter extends BaseModel