Skip to content

Improve generated types for unknown schema objects #769

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 1 commit into from
Sep 30, 2021
Merged

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Sep 30, 2021

Fixes #693, #554

Background

According to #693, I believe this is correct:

According to the OpenAPI spec, when a schema is defined without a type it is the equivalent of:

     anyOf:
       - type: string
       - type: number
       - type: integer
       - type: boolean
       - type: array
         items: {}
       - type: object

but openapi-typescript uses object type as the default and generates { [key: string]: unknown }. Instead it should generate just unknown.

This PR addresses that, fixing some bugs in the process. And also switches from any to unknown in other areas (#554).

I had originally thought that changing to unknown would be disruptive for people, but after looking again at the output I don’t believe it will be too bad for most. It should actually surface errors that people weren’t aware of before, which is the whole goal! So this will be released as a minor version rather than a breaking major version.

@codecov
Copy link

codecov bot commented Sep 30, 2021

Codecov Report

Merging #769 (6aff470) into main (091b053) will increase coverage by 0.08%.
The diff coverage is 94.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #769      +/-   ##
==========================================
+ Coverage   94.15%   94.24%   +0.08%     
==========================================
  Files          13       13              
  Lines         565      573       +8     
  Branches      200      204       +4     
==========================================
+ Hits          532      540       +8     
  Misses         32       32              
  Partials        1        1              
Impacted Files Coverage Δ
src/utils.ts 95.45% <90.90%> (+0.45%) ⬆️
src/load.ts 82.78% <100.00%> (+0.28%) ⬆️
src/transform/schema.ts 99.00% <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 4ae2277...6aff470. Read the comment docs.

@@ -9151,7 +9151,7 @@ export interface operations {
state?: string;
town?: string;
};
dob?: { [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.

This is a big improvement! unknown should be the correct type here.

@drwpow drwpow merged commit 4a6475e into main Sep 30, 2021
@drwpow drwpow deleted the type-changes branch September 30, 2021 22:23
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.

Schema without a type should be "any" or "unknown" instead of a generic map type
1 participant