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

fix: channel parameter not always returning schema #868

Merged
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
8 changes: 1 addition & 7 deletions src/models/v3/channel-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ export class ChannelParameter extends BaseModel<v3.ParameterObject, { id: string
}

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

schema(): SchemaInterface | undefined {
if (!this.hasSchema()) return undefined;
return this.createModel(Schema, {
type: 'string',
description: this._json.description,
Expand Down
15 changes: 11 additions & 4 deletions test/models/v3/channel-parameter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ describe('ChannelParameter model', function() {
expect(d.hasSchema()).toEqual(true);
});

it('should return false when there is no value', function() {
it('should return true when there is no value', function() {
const doc = serializeInput<v3.ParameterObject>({});
const d = new ChannelParameter(doc);
expect(d.hasSchema()).toEqual(false);
expect(d.hasSchema()).toEqual(true);
Copy link
Member

Choose a reason for hiding this comment

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

I agree with the other change but this one is highly debatable. "hasSchema" is meant to tell you if the document has the schema specified, therefore it should be false. However, even though it's false, you should get a default schema in the "schema()" call. These are two different things IMHO.

Copy link
Member Author

Choose a reason for hiding this comment

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

But for v3, you will always have a schema (underneath) as the default behavior is that the parameters are of type: string 🤔 Regardless of what else you define in your parameter.

For v2 it makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

As @fmvilas mentioned, hasSchema is there for answering: Did the user set the schema?

Copy link
Member Author

Choose a reason for hiding this comment

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

But for v3 the schema is always set, even when an empty parameter is defined.

Copy link
Member

Choose a reason for hiding this comment

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

What I mean is if the user has set the schema key. hasSchema answers that question. If the schema key is there, then hasSchema is true, otherwise it's false. Now, the schema() call will always give you a schema because it has a default. Does that make sense?

Copy link
Member

Choose a reason for hiding this comment

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

Alright, discussed offline and this is fine. On v3, there's no schema keyword anymore so it should always be true.

Copy link
Member

Choose a reason for hiding this comment

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

But for v3, you will always have a schema (underneath) as the default behavior is that the parameters are of type: string 🤔 Regardless of what else you define in your parameter.

For v2 it makes sense.

Oh yeah its true😅

});
});

Expand All @@ -87,10 +87,17 @@ describe('ChannelParameter model', function() {
expect(d.schema()?.default()).toEqual('test');
});

it('should return undefined when there is no value', function() {
it('should be able to access description value', function() {
const doc = serializeInput<v3.ParameterObject>({ description: 'test' });
const d = new ChannelParameter(doc);
expect(d.schema()).toBeInstanceOf(Schema);
expect(d.schema()?.description()).toEqual('test');
});
it('should return empty schema with type string when there is no value', function() {
const doc = serializeInput<v3.ParameterObject>({});
const d = new ChannelParameter(doc);
expect(d.schema()).toBeUndefined();
expect(d.schema()).toBeInstanceOf(Schema);
expect(d.schema()?.type()).toEqual('string');
});
});

Expand Down