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: ensure summaries + descriptions are strings #764

Merged
merged 2 commits into from
May 23, 2023
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
205 changes: 205 additions & 0 deletions __tests__/__datasets__/callbacks-weird-summary-description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"openapi": "3.0.3",
"info": {
"title": "Support for callbacks",
"description": "https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operationObject\n\nhttps://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameter-object\n\nhttps://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#schema-object",
"version": "1.0.0"
},
"servers": [
{
"url": "https://httpbin.org"
}
],
"paths": {
"/callbacks": {
"summary": {
"$ref": "foo-summary.md"
},
"description": {
"$ref": "foo-desc.md"
},
"get": {
"summary": {
"$ref": "foo-summary.md"
},
"description": {
"$ref": "foo-desc.md"
},
"responses": {
"200": {
"description": "OK"
}
},
"callbacks": {
"myCallback": {
"{$request.query.queryUrl}": {
"post": {
"summary": {
"$ref": "foo-summary.md"
},
"description": {
"$ref": "foo-desc.md"
},
"requestBody": {
"description": "Callback payload",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/dog"
}
}
}
},
"responses": {
"200": {
"description": "callback successfully processed",
"content": {
"application/json": {
"example": {
"id": 1,
"name": "Pug",
"is_a_good_dog": true
}
}
}
}
}
}
}
},
"multipleCallback": {
"{$request.multipleExpression.queryUrl}": {
"post": {
"requestBody": {
"description": "Callback payload",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/dog"
}
}
}
},
"responses": {
"200": {
"description": "callback successfully processed",
"content": {
"application/json": {
"example": {
"id": 1,
"name": "Pug",
"is_a_good_dog": true
}
}
}
}
}
}
},
"{$request.multipleMethod.queryUrl}": {
"summary": {
"$ref": "foo-summary.md"
},
"description": {
"$ref": "foo-desc.md"
},
"parameters": [
{
"in": "query",
"name": "queryParam",
"schema": {
"type": "string"
},
"required": true
}
],
"post": {
"requestBody": {
"description": "Callback payload",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/dog"
}
}
}
},
"responses": {
"200": {
"description": "callback successfully processed"
}
}
},
"get": {
"summary": "[get] callback summary",
"description": "[get] callback description",
"parameters": [
{
"in": "query",
"name": "queryParam",
"schema": {
"type": "string"
},
"required": true
},
{
"in": "query",
"name": "anotherQueryParam",
"schema": {
"type": "string"
},
"required": true
}
],
"responses": {
"200": {
"description": "callback successfully processed",
"content": {
"application/json": {
"example": {
"id": 1,
"name": "Pug",
"is_a_good_dog": true
}
}
}
}
}
}
}
}
}
},
"post": {
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {
"schemas": {
"dog": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"is_a_good_dog": {
"type": "boolean"
}
},
"example": {
"id": 1,
"name": "Pug",
"is_a_good_dog": true
}
}
}
}
}
41 changes: 41 additions & 0 deletions __tests__/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let parametersCommon: Oas;
let petstoreNondereferenced: Oas;
let oas31NoResponses: Oas;
let readme: Oas;
let callbacksWeirdSummaryDescription: Oas;

beforeAll(async () => {
petstore = await import('@readme/oas-examples/3.0/json/petstore.json').then(r => r.default).then(Oas.init);
Expand All @@ -35,6 +36,11 @@ beforeAll(async () => {
deprecatedSchema = await import('./__datasets__/schema-deprecated.json').then(r => r.default).then(Oas.init);
await deprecatedSchema.dereference();

callbacksWeirdSummaryDescription = await import('./__datasets__/callbacks-weird-summary-description.json')
.then(r => r.default)
.then(Oas.init);
await callbacksWeirdSummaryDescription.dereference();

parametersCommon = await import('@readme/oas-examples/3.0/json/parameters-common.json')
.then(r => r.default)
.then(Oas.init);
Expand Down Expand Up @@ -83,6 +89,20 @@ describe('#getSummary() + #getDescription()', () => {
expect(operation.getDescription()).toBe('[get] Description');
});

it('should account for non-string summaries and descriptions', () => {
const operation = callbacksWeirdSummaryDescription.operation('/callbacks', 'get');

expect(operation.getSummary()).toBeUndefined();
expect(operation.getDescription()).toBeUndefined();
});

it('should account for non-string common summaries and descriptions', () => {
const operation = callbacksWeirdSummaryDescription.operation('/callbacks', 'post');

expect(operation.getSummary()).toBeUndefined();
expect(operation.getDescription()).toBeUndefined();
});

describe('callbacks', () => {
it('should return a summary if present', () => {
const operation = callbackSchema.operation('/callbacks', 'get');
Expand Down Expand Up @@ -115,6 +135,27 @@ describe('#getSummary() + #getDescription()', () => {
expect(callback.getSummary()).toBe('[post] callback summary');
expect(callback.getDescription()).toBe('[post] callback description');
});

it('should account for non-string summaries and descriptions', () => {
const operation = callbacksWeirdSummaryDescription.operation('/callbacks', 'get');

const callback = operation.getCallback('myCallback', '{$request.query.queryUrl}', 'post') as Callback;

expect(callback.getSummary()).toBeUndefined();
expect(callback.getDescription()).toBeUndefined();
});

it('should account for non-string common callback summary + descriptions', () => {
const operation = callbacksWeirdSummaryDescription.operation('/callbacks', 'get');
const callback = operation.getCallback(
'multipleCallback',
'{$request.multipleMethod.queryUrl}',
'post'
) as Callback;

expect(callback.getSummary()).toBeUndefined();
expect(callback.getDescription()).toBeUndefined();
});
});
});

Expand Down
24 changes: 12 additions & 12 deletions src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ export default class Operation {
}

getSummary(): string {
if (this.schema?.summary) {
return this.schema.summary.trim();
} else if (this.api.paths[this.path].summary) {
if (this.schema?.summary && typeof this.schema.summary === 'string') {
return this.schema.summary;
} else if (this.api.paths[this.path].summary && typeof this.api.paths[this.path].summary === 'string') {
return this.api.paths[this.path].summary;
}

return undefined;
}

getDescription(): string {
if (this.schema?.description) {
return this.schema.description.trim();
} else if (this.api.paths[this.path].description) {
if (this.schema?.description && typeof this.schema.description === 'string') {
return this.schema.description;
} else if (this.api.paths[this.path].description && typeof this.api.paths[this.path].description === 'string') {
return this.api.paths[this.path].description;
}

Expand Down Expand Up @@ -854,19 +854,19 @@ export class Callback extends Operation {
}

getSummary(): string {
if (this.schema?.summary) {
return this.schema.summary.trim();
} else if (this.parentSchema.summary) {
if (this.schema?.summary && typeof this.schema.summary === 'string') {
return this.schema.summary;
} else if (this.parentSchema.summary && typeof this.parentSchema.summary === 'string') {
return this.parentSchema.summary;
}

return undefined;
}

getDescription(): string {
if (this.schema?.description) {
return this.schema.description.trim();
} else if (this.parentSchema.description) {
if (this.schema?.description && typeof this.schema.description === 'string') {
return this.schema.description;
} else if (this.parentSchema.description && typeof this.parentSchema.description === 'string') {
return this.parentSchema.description;
}

Expand Down