[REQ][Typescript] Allow uniontypes for type-mappings
Maybe this already is possible and I just don't know how to use it. Here's my problem / idea:
By default openapigen uses string for "type": "string", "format": "date-time" models, what is technically totally correct. I know I can change the type to e.g. date using --type-mappings=DateTime=Date but I'd rather use --type-mappings=DateTime="Date|String". In my opinion this is the technically nices solution since I can easyly say prop=newDate(prop) without needing to cast to any or create a new model.
The only issue is: when using --type-mappings=DateTime="Date|String" all my models get a import { Date|String } from './date|String'; that need to be removed later on. Am I just using it wrong? Or aren't union types supported yet?
I was thinking of this same say solution for some kind of api masking with a time date stamp.
https://egghead.io/blog/rails-graphql-typescript-react-apollo
I have been looking at DSL coding And the internal and external functions
Hey @macjohnny, sorry for disturbing you this way, but you were pretty helpful in other CodeGen-related questions. Can you maybe help here? Do you know if this already is possible and I'm just using the wrong syntax?
@angelaki unfortunately I am not aware of any way to use union types in type mappings. however, an easy workaround is simply to post-process the generated source files with a script of yours to remove the wrong import lines. alternatively, you could implement some logic in the codegen to detect this case and split the type to generate correct imports
@macjohnny Getting back to this issue since it pretty annoys me again right now. Too bad I'm absolutely not used to the templating system of OpenAPICodeGen, but wouldn't union type solve this everlasting conflict of Date being String on the pipe solve once and for all?
Just assuming Date is simple wrong, since the values are strings. But not being able to pass a Date to the objects variable / as a method parameter ist quite annoying. Imgo Date | string is the best goto solution for generated models. Would it be a big deal to make OpenAPICodeGen capable of this solution (what way ever?). By a bool parameter, ability of setting union mappings (best solution I guess) or what ever?
Sure I'm just about to write my own Post-Processor. But I guess this solution could be so super usefull for everyone using CodeGen!
Sorry for bumping, but any chance this could make it to the latest build? @macjohnny are you familiar with the generators templating engine? I still hope that this is no big deal for someone knowing the project's source code - and would be such a great solution!
@macjohnny coming back to this issue since it really annoyes me again and I maybe got a good solution:
First of all I noticed, that --type-mappings=DateTime='Date|string' and --type-mappings=DateTime='Date | string' behave different:
The first one generates
import { Date|string } from './date|string';
prop: Date|string | null;
while the later one generate
import { string } from './modelString';
import { Date } from './modelDate';
prop: Date | string | null;
Setting --type-mappings=DateTime='Date | string' --import-mappings='Date | string'='' (or --type-mappings=DateTime='Date|string' --import-mappings='Date|string'='') the generated output looks like
import { string } from '';
import { Date } from '';
prop: Date | string | null;
So we're alomost there! Would it be possible (guess even I can do this, almost not knowing the code) to just skip imports with an empty mapping? Or could this mess with other implementations? And: why does the later one obviously tell both of them appart but still tries to import "Data" and "string"? I thought CodeGen knows there no need to import them.
Never the less, just skipping empty one would do the job. Would a PR be welcome?
@angelaki yes a PR is always welcome :)
@macjohnny just to make sure: I could just remove all import-mappings with an empty value? At least any hint where to start searching?
@wing328 is there a general way to „disable“ imports, e.g. by setting the import mapping to an empty string as suggested above?
@wing328 sorry for bumping, but news would be great 👍
--type-mappings=DateTime="Date|String"
what about also adding "Date|String" as a primitive type via the option --language-specific-primitives? i think that should skip the import.
--language-specific-primitives
Yay! Adding both --type-mappings=DateTime="Date | string" --language-specific-primitives="Date | string" totally does the job! Thank you so much!