-
Notifications
You must be signed in to change notification settings - Fork 504
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
Union with null is not supported #479
Comments
Hi @atomicus. Out of curiosity, why have you chosen to represent the type as cc’ing @WoH since he and I have discussed this use case before, but we were both interested in learning more about the use case from users. So any context you can provide about the usefulness would be helpful @atomicus. |
This also applies to unions with To add to @dgreene1's points, while there is the concept of ---
UnionModel:
description: Contains a nullable union (ideally)
properties:
and:
allOf:
- "$ref": "#/components/schemas/TypeAliasModel1"
- "$ref": "#/components/schemas/TypeAliasModel2"
nullable: true will not actually work. Even ---
UnionModel:
description: Contains a nullable union (ideally)
properties:
and:
allOf:
- "$ref": "#/components/schemas/TypeAliasModel1"
nullable: true
- "$ref": "#/components/schemas/TypeAliasModel2"
nullable: true will not work for now. As far as I'm aware, this should work in a future version of json schema that will eventually be adopted by OpenAPI. Once that's there I'll take another look. This is why I decided to not work on this for now, however, I think it's great to have an issue to refer to. |
@WoH why won’t the second case work? |
When working with relational db i don't think I'm going to try working on fixing NullKeyword if only for simple use cases like |
I’d encourage you and anyone reading this keep your tsoa types separate from your database types. It’s incredibly valuable for software to use abstractions. This allows your database to change and evolve while keeping your contract the same for consumers. I realize that I’ve just given unsolicited architectural advice, so please forgive me. But it’s still unclear to me how we can support I think at best we can log a warning to console that says, “please use |
So what do you think of us having a more helpful error message that says, unions with null are not yet supported. Please use a union of undefined instead. |
that's definitely good advice, unfortunately i think these other teams haven't done that and now they are keen on using the older tsoa version forever until it explodes in classic tech debt style. I don't think a different error would be that helpful in this case as the existing error already links to the type and the line which it was found and that's really helpful. Really, they'd probably say they don't care as the swagger docs that they currently have via older tsoa, while wrong, are still better than most other services. Your comment had me go and look a bit more at what the spec actually is and now i'm probably in favor of changing the public interfaces. gah... |
We find a "solution" for this OpenApi problem: interface SomeModel {
nullableField: NullString
}
type NullString = string | null This not the best or more clean solution but work for us, but swagger don't represent well the model |
This will not work in tsoa v3.x since we are properly able to resolve type aliases. nullableProperty:
type: boolean
enum:
- null
nullable: true While I think this is not ideal, it allows typing |
I’m really not sure. I guess I don’t enough to have a strong opinion. I think we might need outside input. Maybe we can tag someone from the OpenAPI spec? |
I can add some more context for now, feel free to tag someone: OpenAPI v3.0.3. adds language that clarifies that #479 (comment) won't work (OAI/OpenAPI-Specification#2046) Since the behavior around Enum with
OpenAPI says JSON-Schema behavior should be adopted unless explicitly stated otherwise:
In the list of properties taken from the JSON Schema definition with adjusted definitions, enum is not mentioned. |
if you think that would work then I guess we could give it a go. My only hesitation is that it looks strange to me that the enum only has one type in it. Ultimately the best way to gain confidence in this is to try to compile that schema back into TypeScript using some popular converter. If it becomes |
Swagger Core works: swagger-api/swagger-core#3100 / swagger-api/swagger-core#3098 Supposedly it's fixed in OpenAPI generator: OpenAPITools/openapi-generator#1997, however the bugfix only addresses python as far as I can tell and that one crashed on me when I did |
I know this is a bit of an older issue. However, as a workaround I created a wrapper generic type for a nullable, which works fine, and sets nullable: true, or x-nullable: true in v2 spec. The code I used: type Nullable<T> = T | null;
interface SomeModel {
nullableField: Nullable<string>
} You can use that with any type / interfaces as well where a union with null is required. |
See: #479 (comment) I think this is a fine hack for now, I'll make sure 3.0 properly documents |
I'm having issues with this in our build. I'm unable to create an interface using
But numberToJSON is not included in the file and creating this error:
The same thing is showing up for numberFromJSON. Is the enum representation of |
The reason is that
What do you set there? Another example:
Where do we set |
I understand, do you agree that this is likely an issue in open api generator? I can open an issue there and see If I can get any traction. Until then we're unfortunately going to be stuck on 3.0.2. |
I'd say so, yes. |
I had a chance to poke around this a bit more. I looked over the swagger-codegen repo. My assumption is that a fix from them will be daunting to achieve. There are a few reasonable seeming PRs against the typescript code generation that have been sitting there unattended for a long time. The bug on swagger-codegen side that causes this to fail is that using I think there is also a bug on the tsoa side. the enum which is joined with the value as a oneOf, contains the string I will file an issue in swagger-codegen about supporting oneOf correctly. As mentioned, even If I can figure out how to fix it and make a PR, it's possible it may not be accepted. How difficult it would be to support the more straightforward |
If you could open a separate issue for this, I'd add it to the 3.0 backlog.
CBA. If someone opens a PR that handles this in a reasonable way (only affecting the SpecGenerator), I'd be potentially open for that. |
It turns out I'm not even using swagger-codegen. I was having a really hard time tracking down what was going wrong, it all make sense now. I'm actually using the openapi-generator to generate clients in each of my projects. It looks like they already have an issue tracking this there: OpenAPITools/openapi-generator#5202 It doesn't look like its been acknowledged yet. |
A couple items of note. IMO a more intuitive representation of null in openapi v3 is described here: It may be a moot point however because I see that nullable is getting deprecated in v3.1.0 and a type = "null" is being added. At the point this comes out, this seems to be the obvious choice to use. See here: https://github.com/OAI/OpenAPI-Specification/blob/v3.1.0-dev/versions/3.1.0.md#fixed-fields-20 |
@WoH I found a way to add support for nullable refs without oneOf. Using |
Bumping this thread now that the "null" type has been added to OpenAPI. An example model of:
Should generate a spec of:
|
Seconding the above bump now that the |
Sorry for not checking this for such a long time. I'm not using the So, lets say we have interface:
According to this description, this will result in REST response of format:
While
Will result in:
So, those responses will not be the same. At least this is how I and most of the devs I know will react to that kind of typing. This is how it works for example with https://github.com/typestack/class-transformer. Property marked as There's just a difference between null, which is some kind of information, and no information. |
TSOA fails on generating routes when interface includes 'null' type.
I'm submitting a ...
I confirm that I
Expected Behavior
Generate propper swagger for empty-valued patams
Current Behavior
Error is presented:
Steps to Reproduce
2.run routes generation
Context (Environment)
Version of the library: 2.5.2
Version of NodeJS: 8.15 & 10.15
The text was updated successfully, but these errors were encountered: