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: create a response description if one is missing #3

Merged
merged 1 commit into from
May 1, 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
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"release": "npx conventional-changelog-cli -i CHANGELOG.md -s && git add CHANGELOG.md"
},
"dependencies": {
"@readme/http-status-codes": "^7.2.0",
"js-yaml": "^4.1.0",
"jsonc-parser": "3.2.0",
"lodash.camelcase": "^4.3.0",
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
promises: { writeFile, readFile },
} = require('fs');

const { getStatusCodeMessage } = require('@readme/http-status-codes');
const { dump } = require('js-yaml');
const jsonc = require('jsonc-parser');
const camelCase = require('lodash.camelcase');
Expand Down Expand Up @@ -552,6 +553,16 @@ function parseResponseFromExamples(responses, responseHeaders) {
code = 'default';
}

// The OpenAPI spec requires that `description` be present on responses with content so
// we'll make it match the message that cooresponds to the HTTP status code.
if (!description) {
try {
description = getStatusCodeMessage({ code });
} catch (err) {
description = code;
}
}

if (code in statusMap) {
if (!(language in statusMap[code].bodies)) {
statusMap[code].bodies[language] = [];
Expand Down
1 change: 1 addition & 0 deletions test/resources/output/ResponsesWithoutHTTPStatusCode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ paths:
avatar: https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg
responses:
default:
description: default
headers:
Server:
schema:
Expand Down
1 change: 1 addition & 0 deletions types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@readme/http-status-codes';
Copy link
Member

Choose a reason for hiding this comment

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

This feels like a very easy library to export types for lol

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I might rewrite it in TS in a bit.