Skip to content

Commit

Permalink
fix implementation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jul 25, 2023
1 parent 2000140 commit f09b2d1
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 10 deletions.
238 changes: 238 additions & 0 deletions src/models/v3/channel-parameter-schema.ts
Original file line number Diff line number Diff line change
@@ -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<v3.ParameterObject, { }> 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<SchemaInterface> | undefined {
return undefined;
}

anyOf(): Array<SchemaInterface> | 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<string, SchemaInterface> | undefined {
return undefined;
}

description(): string | undefined {
return this._json.description;
}

dependencies(): Record<string, SchemaInterface | Array<string>> | undefined {
return undefined;
}

deprecated(): boolean {
return false;
}

discriminator(): string | undefined {
return undefined;
}

else(): SchemaInterface | undefined {
return undefined;
}

enum(): Array<any> | undefined {
return this._json.enum;
}

examples(): Array<any> | 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<SchemaInterface> | 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<SchemaInterface> | undefined {
return undefined;
}

pattern(): string | undefined {
return undefined;
}

patternProperties(): Record<string, SchemaInterface> | undefined {
return undefined;
}

properties(): Record<string, SchemaInterface> | undefined {
return undefined;
}

property(): SchemaInterface | undefined {
return undefined;
}

propertyNames(): SchemaInterface | undefined {
return undefined;
}

readOnly(): boolean | undefined {
return undefined;
}

required(): Array<string> | undefined {
return undefined;
}

then(): SchemaInterface | undefined {
return undefined;
}

title(): string | undefined {
return undefined;
}

type(): string | Array<string> | 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<v3.AsyncAPISchemaDefinition>);
}
}
12 changes: 8 additions & 4 deletions src/models/v3/channel-parameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseModel } from '../base';
import { Schema } from './schema';

import { hasDescription, description, extensions } from './mixins';

Expand All @@ -8,19 +7,24 @@ 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<v3.ParameterObject, { id: string }> implements ChannelParameterInterface {
id(): string {
return this._meta.id;
}

hasSchema(): boolean {
return !!this._json.schema;
return this._json && (
this._json.default !== undefined ||
this._json.enum !== undefined ||
this._json.examples !== undefined
);
}

schema(): SchemaInterface | undefined {
if (!this._json.schema) return undefined;
return this.createModel(Schema, this._json.schema, { pointer: `${this._meta.pointer}/schema` });
if (!this.hasSchema()) return undefined;
return this.createModel(ParameterSchema, this._json, { pointer: `${this._meta.pointer}` });
}

hasLocation(): boolean {
Expand Down
4 changes: 3 additions & 1 deletion src/spec-types/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ export type ParametersObject = Record<string, ParameterObject | ReferenceObject>

export interface ParameterObject extends SpecificationExtensions {
description?: string;
schema?: SchemaObject;
enum?: string[];
default?: string;
examples?: string[];
location?: string;
}

Expand Down
35 changes: 30 additions & 5 deletions test/models/v3/channel-parameter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -43,12 +44,23 @@ describe('ChannelParameter model', function() {
});

describe('.hasSchema()', function() {
it('should return true when there is a value', function() {
const doc = serializeInput<v3.ParameterObject>({ schema: {} });
it('should return true if enum are sat', function() {
const doc = serializeInput<v3.ParameterObject>({ enum: ['test'] });
const d = new ChannelParameter(doc);
expect(d.hasSchema()).toEqual(true);
});
it('should return true if default are sat', function() {
const doc = serializeInput<v3.ParameterObject>({ default: 'test' });
const d = new ChannelParameter(doc);
expect(d.hasSchema()).toEqual(true);
});

it('should return true if examples are sat', function() {
const doc = serializeInput<v3.ParameterObject>({ 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<v3.ParameterObject>({});
const d = new ChannelParameter(doc);
Expand All @@ -57,10 +69,23 @@ describe('ChannelParameter model', function() {
});

describe('.schema()', function() {
it('should return the value', function() {
const doc = serializeInput<v3.ParameterObject>({ schema: {} });
it('should be able to access enum values', function() {
const doc = serializeInput<v3.ParameterObject>({ 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<v3.ParameterObject>({ 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<v3.ParameterObject>({ 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() {
Expand Down

0 comments on commit f09b2d1

Please sign in to comment.