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: addition of a getRequestBody accessor #580

Merged
merged 4 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
67 changes: 58 additions & 9 deletions __tests__/operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,34 +933,83 @@ describe('#hasRequestBody()', () => {
});
});

describe('#getRequestBody()', () => {
it('should return false on an operation without a requestBody', async () => {
const oas = Oas.init(petstore);
await oas.dereference();

const operation = oas.operation('/pet/findByStatus', 'get');
expect(operation.getRequestBody('application/json')).toBe(false);
});

it('should return false on an operation without the specified requestBody media type', async () => {
const oas = Oas.init(petstore);
await oas.dereference();

const operation = oas.operation('/pet', 'put');
expect(operation.getRequestBody('text/xml')).toBe(false);
});

it('should return the specified requestBody media type', async () => {
const oas = Oas.init(petstore);
await oas.dereference();

const operation = oas.operation('/pet', 'put');
expect(operation.getRequestBody('application/json')).toStrictEqual({
schema: {
properties: {
category: expect.objectContaining({ 'x-readme-ref-name': 'Category' }),
id: expect.any(Object),
name: expect.any(Object),
photoUrls: expect.any(Object),
status: expect.any(Object),
tags: {
items: expect.objectContaining({ 'x-readme-ref-name': 'Tag' }),
type: 'array',
xml: {
name: 'tag',
wrapped: true,
},
},
},
required: ['name', 'photoUrls'],
type: 'object',
xml: {
name: 'Pet',
},
'x-readme-ref-name': 'Pet',
},
});
});
});

describe('#getResponseByStatusCode()', () => {
it('should return false if the status code doesnt exist', () => {
const operation = Oas.init(petstore).operation('/pet/findByStatus', 'get');
expect(operation.getResponseByStatusCode(202)).toBe(false);
});

it('should return the response', () => {
const operation = Oas.init(petstore).operation('/pet/findByStatus', 'get');
it('should return the response', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to rework this test a bit because the dereferencing library we use does parameter reassigning so if one test dereferences the petstore spec then tests that use that spec after it will have a dereferenced spec.

const oas = Oas.init(petstore);
await oas.dereference();

const operation = oas.operation('/pet/findByStatus', 'get');
expect(operation.getResponseByStatusCode(200)).toStrictEqual({
description: 'successful operation',
content: {
'application/json': {
schema: {
items: {
$ref: '#/components/schemas/Pet',
},
type: 'array',
items: expect.any(Object),
},
},
'application/xml': {
schema: {
items: {
$ref: '#/components/schemas/Pet',
},
type: 'array',
items: expect.any(Object),
},
},
},
description: 'successful operation',
});
});
});
Expand Down
22 changes: 21 additions & 1 deletion src/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,33 @@ export default class Operation {
}

/**
* Determine if the operation has a request body.
* Determine if the operation has any request bodies.
*
*/
hasRequestBody(): boolean {
return !!this.schema.requestBody;
}

/**
* Retrieve a specific request body content schema off this operation.
*
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#mediaTypeObject}
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#mediaTypeObject}
* @param mediaType Specific request body media type to retrieve if present.
*/
getRequestBody(mediaType: string) {
if (!this.hasRequestBody()) {
return false;
}

const requestBody = this.schema.requestBody;
if (RMOAS.isRef(requestBody) || !(mediaType in requestBody.content)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why return false if it's a ref? It's probably worth leaving those details as a comment here. I also don't see any tests for when the req body is a ref.

return false;
}

return requestBody.content[mediaType];
}

/**
* Retrieve an array of request body examples that this operation has.
*
Expand Down