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] 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() {