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

Mark schema objects with default values as non-nullable #613

Merged
merged 1 commit into from
May 27, 2021

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented May 25, 2021

Partially fixes #584.

This is a very good fix pointed out by @rustyconover, and handled in PR #584, but that currently would be blocking remote schemas, as $refs would no-longer be resolvable (or at least, not easily). This PR does half of #584, where $refs are not resolved. But it does improve the handling of default: without blocking remote schemas.

@codecov
Copy link

codecov bot commented May 25, 2021

Codecov Report

Merging #613 (54ef1ea) into main (a36a728) will increase coverage by 1.71%.
The diff coverage is 88.31%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #613      +/-   ##
==========================================
+ Coverage   88.53%   90.25%   +1.71%     
==========================================
  Files           9       10       +1     
  Lines         349      390      +41     
  Branches      123      141      +18     
==========================================
+ Hits          309      352      +43     
- Misses         37       38       +1     
+ Partials        3        0       -3     
Impacted Files Coverage Δ
src/transform/headers.ts 23.07% <0.00%> (-4.20%) ⬇️
src/transform/index.ts 87.93% <78.78%> (+8.30%) ⬆️
src/transform/responses.ts 84.21% <81.81%> (-3.89%) ⬇️
src/transform/paths.ts 92.00% <87.50%> (ø)
src/transform/request.ts 95.00% <95.00%> (ø)
src/transform/parameters.ts 98.00% <96.42%> (+0.50%) ⬆️
src/index.ts 79.31% <100.00%> (+6.58%) ⬆️
src/transform/operation.ts 100.00% <100.00%> (+3.12%) ⬆️
src/transform/schema.ts 98.79% <100.00%> (+1.73%) ⬆️
... and 1 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 c0458ac...54ef1ea. Read the comment docs.

@rustyconover
Copy link

Looks awesome!

@@ -15,10 +15,14 @@ interface TransformSchemaObjOptions extends GlobalContext {
required: Set<string>;
}

function hasDefaultValue(node: any): boolean {
if (node.hasOwnProperty("default")) return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only change here: check if a schema object has a default value. But like the description states, we can’t currently look this up for $ref (which might be a remote schema)

@@ -72,7 +72,7 @@ export interface components {
shipDate?: string
/** Order Status */
status?: 'placed' | 'approved' | 'delivered'
complete?: boolean
complete: boolean
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated snapshots show it’s improved a lot of schemas

@gr2m
Copy link
Contributor

gr2m commented May 28, 2021

Do you mind if we revert this change until we figure out to differentiate between request parameter types and response types? This change made a bunch of requestBody parameters from GitHub's OpenAPI spec required because there is a defaults key, but that is incorrect. The parameters are not required, the spec just states what value the parameter will get if it is not set.

I don't think this is a simple fix

gr2m added a commit to octokit/openapi-types.ts that referenced this pull request May 28, 2021
@rustyconover
Copy link

rustyconover commented May 28, 2021 via email

@drwpow
Copy link
Contributor Author

drwpow commented May 28, 2021

@gr2m thanks for your feedback! And apologies for the changes to the GitHub schema. I too am a bit on the fence about this one, because this is an area where there’s overlap between type generation and business logic, and the lines are a bit blurry. Do these types sit between the API response and your business logic? If so, they should probably be stricter, and err on the side of more nullable properties as they were before so that you can fill in the defaults. Or do these types sit between the business logic and your frontend, after the defaults have been applied? If so, the recent change to respect default may be preferred.

I’m sure people use types in different ways according to their different needs, which leads to differences in interpreting OpenAPI -> TypeScript.

If this is causing issues for people, I’d rather err on the side of doing what the library was doing before, revert the behavior, and make this behavior opt-in via a flag (e.g. --default-non-nullable or something). Would that be doable?

@gr2m
Copy link
Contributor

gr2m commented May 28, 2021

GItHub's OpenAPI Spec is for public consumption. They use it internally as well, but it's also used to generate the docs at https://docs.github.com/en/rest/reference. So if a default value is set on a request parameter, it cannot be interpreted as a required parameter.

I use the OpenAPI spec to generate several SDKs (e.g. https://github.com/octokit/plugin-rest-endpoint-methods.js/), and they types (github.com/octokit/openapi-types.ts and github.com/octokit/types.ts), so I have the API consumer perspective.

If this is causing issues for people, I’d rather err on the side of doing what the library was doing before, revert the behavior, and make this behavior opt-in via a flag (e.g. --default-non-nullable or something). Would that be doable?

yes that sounds like a good plan

@drwpow
Copy link
Contributor Author

drwpow commented Jun 3, 2021

@gr2m Fixed in v3.4.0 and a --default-non-nullable flag was added to opt into this behavior

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.

Property values with defaults are being incorrectly typed as possibly undefined when they shouldn't be.
3 participants