Skip to content

Always checks for required properties, even if they are missing from properties #620

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 2 commits into from
Jun 14, 2021
Merged

Always checks for required properties, even if they are missing from properties #620

merged 2 commits into from
Jun 14, 2021

Conversation

shuluster
Copy link
Contributor

Fixes #619

@codecov
Copy link

codecov bot commented Jun 3, 2021

Codecov Report

Merging #620 (5d9aa6e) into main (026eb74) will decrease coverage by 0.07%.
The diff coverage is 94.09%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #620      +/-   ##
==========================================
- Coverage   93.81%   93.73%   -0.08%     
==========================================
  Files           5       11       +6     
  Lines         291      527     +236     
  Branches       97      188      +91     
==========================================
+ Hits          273      494     +221     
- Misses         16       32      +16     
+ Partials        2        1       -1     
Impacted Files Coverage Δ
src/transform/index.ts 87.93% <87.93%> (ø)
src/transform/responses.ts 89.47% <89.47%> (ø)
src/transform/paths.ts 92.30% <92.30%> (ø)
src/load.ts 92.47% <92.47%> (ø)
src/index.ts 89.09% <93.47%> (+7.27%) ⬆️
src/utils.ts 94.82% <93.54%> (-3.36%) ⬇️
src/transform/request.ts 94.44% <94.44%> (ø)
src/transform/schema.ts 97.97% <97.97%> (ø)
src/transform/parameters.ts 98.00% <98.00%> (ø)
src/transform/headers.ts 100.00% <100.00%> (ø)
... and 10 more

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 2da69c1...5d9aa6e. Read the comment docs.

}
let output = "";
for (const r of missingRequired) {
output += `${r}: any;\n`;
Copy link
Contributor

@drwpow drwpow Jun 3, 2021

Choose a reason for hiding this comment

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

In anticipation of #625 (not merged yet), let’s use unknown instead of any here (full explanation why here: #554)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
output += `${r}: any;\n`;
output += `${r}: unknown;\n`;

@@ -45,6 +45,19 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
return output.replace(/\n+$/, "\n"); // replace repeat line endings with only one
}

/** make sure all required fields exist **/
export function addRequiredProps(properties: Record<string, any>, required: string[]): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a minor thing, but I’d recommend using Set<string> rather than string[], so that duplicates will automatically be removed for us. (e.g. required: ["foo", "foo", "foo", "bar"]). I don’t think it’s invalid to repeat properties in required, but the generated TypeScript will be incorrect if this happens (I know that this is on the schema author, but the error will be really weird so I’d rather just avoid it).

// if empty object, then return generic map type
if (
!isAnyOfOrOneOfOrAllOf &&
(!node.properties || !Object.keys(node.properties).length) &&
!node.additionalProperties
) {
output += `{ ${readonly}[key: string]: any }`;
const emptyObj = `{ ${readonly}[key: string]: any }`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here:

Suggested change
const emptyObj = `{ ${readonly}[key: string]: any }`;
const emptyObj = `{ ${readonly}[key: string]: unknown }`;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thinking about this, this might be a breaking change for a lot of folks, who use empty projects and rely on the any. In my project, this wouldn't be a problem.

I will include this change for now. Let me know your thoughts

Copy link
Contributor

Choose a reason for hiding this comment

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

I’m really sorry but I think you do raise a good point and you’ve changed my mind on this—this actually would cause disruption for people. And #554 didn’t really get a lot of support from people, so I’m starting to second-guess it. This PR shouldn’t be blocked by another that may or may not merge.

It seems there is another merge conflict (apologies); if you’re able to update that would you mind changing this back to any again? Again I’m sorry and this is 100% on me. If I don’t hear from you soon, I can update this PR myself and still give you credit for it. Consider this PR ready-to-go!

@@ -9,7 +9,7 @@ const defaults = {
immutableTypes: false,
required: new Set<string>(),
rawSchema: false,
version: 3,
version: 3
Copy link
Contributor

Choose a reason for hiding this comment

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

Not blocking for this PR, but if you could use the local Prettier settings for this project that’d cut down on some unnecessary diff here

})`);
});

it("empty object with required fields", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome tests! ✨

@drwpow
Copy link
Contributor

drwpow commented Jun 3, 2021

Requested a few minor changes; once those are in I’d be happy to merge

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.

A few minor changes requested in comments

@shuluster
Copy link
Contributor Author

Will address those later today.

@shuluster
Copy link
Contributor Author

Updated the branch based on feedbaack

@shuluster
Copy link
Contributor Author

fixed the merge conflict.

@drwpow drwpow merged commit bba4ba8 into openapi-ts:main Jun 14, 2021
@drwpow
Copy link
Contributor

drwpow commented Jun 14, 2021

@all-contributors please add @shuluster for code

@allcontributors
Copy link
Contributor

@drwpow

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

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.

required properties are not respected when they are missing from properties
2 participants