Skip to content

Fix requestBodies that use hypenated naming #550

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

Merged

Conversation

ashsmith
Copy link
Contributor

@ashsmith ashsmith commented Apr 6, 2021

If a request body's name is hypenated it breaks when prettier formats the code as it is an invalid object.

Wrapping the request body's name in quotes resolves the issue, and prettier will strip the quotes when they're not required.

Example for reproducing:

npx openapi-typescript https://github.com/raw/ashsmith/api-specs/fix-invalid-examples/reference/channels.v3.yml --output channels.v3.ts

Current result:

SyntaxError: Property or signature expected. (505:7)
  503 |   }
  504 |   requestBodies: {
> 505 |       post-channels-channel_id-currency-assignmentsBody: {    content: {
      |       ^
  506 |       "application/json": {
  507 | /** Currencies that are enabled for the given channel, in ISO 4217 3 character alphabetic */
  508 | "enabled_currencies": (string)[];
    at e (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/parser-typescript.js:1:322)
    at Object.parse (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/parser-typescript.js:1:3072720)
    at Object.parse (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/index.js:13625:19)
    at coreFormat (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/index.js:14899:14)
    at format (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/index.js:15131:14)
    at /Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/index.js:57542:12
    at Object.format (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/prettier/index.js:57562:12)
    at swaggerToTS (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/dist/cjs/index.js:51:31)
    at main (/Users/ash/.npm/_npx/21d31b7563e6adbc/node_modules/openapi-typescript/bin/cli.js:75:18)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

If a request body's name is hypenated it breaks when prettier formats the code as it is an invalid object.

Wrapping the request body's name in quotes resolves the issue, and prettier will strip the quotes when they're not required.

Example for reproducing:

```
npx openapi-typescript https://github.com/raw/ashsmith/api-specs/fix-invalid-examples/reference/channels.v3.yml --output channels.v3.ts
```
@codecov
Copy link

codecov bot commented Apr 6, 2021

Codecov Report

Merging #550 (524d2a0) into main (8089397) will increase coverage by 0.29%.
The diff coverage is 100.00%.

❗ Current head 524d2a0 differs from pull request most recent head 3402d34. Consider uploading reports for the commit 3402d34 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main     #550      +/-   ##
==========================================
+ Coverage   85.20%   85.50%   +0.29%     
==========================================
  Files           9        9              
  Lines         338      338              
  Branches      112      112              
==========================================
+ Hits          288      289       +1     
+ Misses         46       45       -1     
  Partials        4        4              
Impacted Files Coverage Δ
src/transform/responses.ts 87.50% <100.00%> (ø)
src/utils.ts 94.33% <100.00%> (+1.88%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bf2b791...3402d34. Read the comment docs.

@@ -68,7 +68,7 @@ export function transformRequestBodies(requestBodies: Record<string, RequestBody

Object.entries(requestBodies).forEach(([bodyName, requestBody]) => {
if (requestBody && requestBody.description) output += ` ${comment(requestBody.description)}`;
output += ` ${bodyName}: {`;
output += ` "${bodyName}": {`;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻

};
};`)
);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome test, thank you!

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

Thank you! Will release this as v3.2.2 right now.

@drwpow drwpow merged commit c60c764 into openapi-ts:main Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants