Skip to content

Fix wrong generated type when processing enums containing "null" #492

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
merged 4 commits into from
Mar 2, 2021
Merged

Fix wrong generated type when processing enums containing "null" #492

merged 4 commits into from
Mar 2, 2021

Conversation

FedeBev
Copy link
Contributor

@FedeBev FedeBev commented Mar 2, 2021

Hi there,

this is a WIP PR cointaining a failing test related to issue #482

this PR contains a fix for the issue #482.

Please note: I tried to keep the enum generation logic as is, but in my opinion an enum declared as string should not be allowed to contain number or boolean.

@codecov
Copy link

codecov bot commented Mar 2, 2021

Codecov Report

Merging #492 (ff029e5) into main (a11a3ca) will increase coverage by 0.28%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #492      +/-   ##
==========================================
+ Coverage   78.52%   78.80%   +0.28%     
==========================================
  Files           9        9              
  Lines         298      302       +4     
  Branches       92       95       +3     
==========================================
+ Hits          234      238       +4     
  Misses         64       64              
Impacted Files Coverage Δ
src/transform/schema.ts 98.38% <100.00%> (+0.11%) ⬆️
src/utils.ts 91.22% <100.00%> (ø)

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 e382efa...ff029e5. Read the comment docs.

@FedeBev FedeBev changed the title [WIF] Fix wrong generated type when processing enums containing "null" Fix wrong generated type when processing enums containing "null" Mar 2, 2021
if (typeof item === "string") items.push(`'${item.replace(/'/g, "\\'")}'`);
else if (typeof item === "number" || typeof item === "boolean") items.push(item);
else if (item === null && !node.nullable) items.push("null");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice! This is much better handling than we were doing.

type: "object",
})
).toBe(`{
"string"?: (('Totoro') | (2) | (false)) | null;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻

properties:
myField:
type: string
enum: ["foo", 2, false, null]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Thanks for adding a snapshot test, too 💯

@drwpow
Copy link
Contributor

drwpow commented Mar 2, 2021

This looks great! Thank you 🙏🏻

@drwpow drwpow merged commit 15524ca into openapi-ts:main Mar 2, 2021
@drwpow
Copy link
Contributor

drwpow commented Mar 2, 2021

@all-contributors please add @FedeBev for bug, code, test

@allcontributors
Copy link
Contributor

@drwpow

I've put up a pull request to add @FedeBev! 🎉

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