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: add simplified channel parameter #816

Merged
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
21 changes: 15 additions & 6 deletions src/models/v3/channel-parameter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { BaseModel } from '../base';
import { Schema } from './schema';

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';

export class ChannelParameter extends BaseModel<v3.ParameterObject, { id: string }> implements ChannelParameterInterface {
Expand All @@ -15,12 +13,23 @@ export class ChannelParameter extends BaseModel<v3.ParameterObject, { id: string
}

hasSchema(): boolean {
return !!this._json.schema;
return this._json && (
this._json.description !== undefined ||
this._json.default !== undefined ||
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
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(Schema, {
type: 'string',
description: this._json.description,
enum: this._json.enum,
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
default: this._json.default,
examples: this._json.examples
}, { 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
32 changes: 28 additions & 4 deletions test/models/v3/channel-parameter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,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 +68,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(Schema);
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(Schema);
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()?.default()).toEqual('test');
});

it('should return undefined when there is no value', function() {
Expand Down