The Spring backend that I use uses Jackson to deserialize the JSON into Java objects. In order to do it, it requires that some kind of a @class
field to be present at every level of a nested JSON object in the request, which are added by JsonTypeInfo
.
The TypeScript frontend uses clients generated by the typescript-node
OpenAPI Client Generator, which creates interfaces for each class that the backend exposes through the endpoints.
I would like the TypeScript client to be able to send the @class
parameters in its requests as well for deserialization. This would have been possible with something like reflection where one can recursively call object.getClassName()
but this does not seem to be possible on interfaces in TypeScript.
While I can implement type guards or a static Map<Type,string>
in the frontend through which I can retrieve the @class
value for any given instance, I also do not like that solution as this is not dynamic – potentially a new version of the generated client may make the statically implemented type guard functions or map out-of date.
Is there a possibility to have these auto-generated and shipped with the generated OpenAPI client as well, where maybe each interface also has an auto-generated type guard? I see from this answer that there are some solutions, but has anyone actually implemented this to OpenAPI generated client, or whether they have parameters to allow this generation to also take place?