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

fix(code): generated code for arrays with nullable items #563

Merged

Conversation

codebutler
Copy link
Contributor

@codebutler codebutler commented Aug 31, 2022

Status

READY

Description

The code generator for arrays outputs ambiguous types, and breaks for nullable array items.
Currently, the generated code looks like: string | null[]
This is interpreted as "string or an array of null", which is incorrect.
With this fix, the generated code is: (string | null)[]

This change wraps all array types in parenthesis (()). This is not necessarily needed for a single type, but it causes no harm, avoids any ambiguity, and avoids the need for more complex changes to the code generator.

I also added a regression test that fails to compile without this fix. I didn't see an existing pattern for regression tests so I came up with something. Let me know if you'd prefer a different approach.

Related PRs

n/a

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

Note See tests for complete example.

  1. Create a schema like:
     nullable_items:
       type: array
       items:
         type: string
         nullable: true
  2. Generate code, attempt to assign a value to the type, see error:
    TS2322: Type '[string]' is not assignable to type 'string | null[]'.   Type '[string]' is not assignable to type 'null[]'.     Type 'string' is not assignable to type 'null'.
    

@vercel
Copy link

vercel bot commented Aug 31, 2022

Someone is attempting to deploy a commit to a Personal Account owned by @anymaniax on Vercel.

@anymaniax first needs to authorize it.

hallaca
hallaca previously approved these changes Aug 31, 2022
@codebutler codebutler force-pushed the eric/fix-array-items-nullable branch from c49e710 to 2864175 Compare August 31, 2022 18:06
@codebutler
Copy link
Contributor Author

can someone please authorize this?

@anymaniax
Copy link
Collaborator

I will just let me the time to review your comment. And also It's complicated for me lately. That's why it take a bit of time to be merged and review too

Copy link
Collaborator

@anymaniax anymaniax left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@anymaniax anymaniax merged commit 6b3f770 into orval-labs:master Sep 7, 2022
@melloware
Copy link
Collaborator

thanks @anymaniax any chance we can get a 6.9.7 release?

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.

4 participants