You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a .NET API method that returns an array of type instances that all derive from WidgetBase. By default, when I generate an Open API spec using NSwag, it only reflects the existence of WidgetBase (but not WidgetA or WidgetB which are WidgetBase-derived concrete types returned in the array). Of course then when I generate client code using npx @rtk-query/codegen-openapi [my config file] I only get a WidgetBase type generated in Typescript.
I tried using a "KnownTypesAttribute" for each of the concrete types (e.g. WidgetA and WidgetB) on the type that contains the array of WidgetBase. The types then showed up in my Open API spec but no types were generated for WidgetA or WidgetB by RTK Query.
My super hacky solution is to create a dummy API method that just returns a single sample WidgetA and another that returns a single sample WidgetB. Then RTK query creates client side Typescript WidgetA and WidgetB types (in addition to WidgetBase and the two dummy endpoints on the API). I can then cast the various items in the WidgetBase array to WidgetA or WidgetB and all is fine...
...except that is really ugly. I might end up with 20+ different widgets and don't want all those dummy API methods. Is there some standard way to set up my Open API definition (ideally as generated by NSwag) so RTK Query will know about WidgetBase-derived types even if they are not explicitly exposed on the API?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a .NET API method that returns an array of type instances that all derive from WidgetBase. By default, when I generate an Open API spec using NSwag, it only reflects the existence of WidgetBase (but not WidgetA or WidgetB which are WidgetBase-derived concrete types returned in the array). Of course then when I generate client code using
npx @rtk-query/codegen-openapi [my config file]
I only get a WidgetBase type generated in Typescript.I tried using a "KnownTypesAttribute" for each of the concrete types (e.g. WidgetA and WidgetB) on the type that contains the array of WidgetBase. The types then showed up in my Open API spec but no types were generated for WidgetA or WidgetB by RTK Query.
My super hacky solution is to create a dummy API method that just returns a single sample WidgetA and another that returns a single sample WidgetB. Then RTK query creates client side Typescript WidgetA and WidgetB types (in addition to WidgetBase and the two dummy endpoints on the API). I can then cast the various items in the WidgetBase array to WidgetA or WidgetB and all is fine...
...except that is really ugly. I might end up with 20+ different widgets and don't want all those dummy API methods. Is there some standard way to set up my Open API definition (ideally as generated by NSwag) so RTK Query will know about WidgetBase-derived types even if they are not explicitly exposed on the API?
Beta Was this translation helpful? Give feedback.
All reactions