Honestly I wouldn't try, this is a bug in codegen (for all language really but we've only got issues for 
TypeScript and 
Python)
I happened to look into this last week thinking it might be simple. It is horrifically complicated, breaks just about everything in all the code generators.