mulesoft-labs / raml-for-jax-rs

This project is all about two way transformation of JAX-RS-annotated Java code to RAML API description and back.
Other
296 stars 181 forks source link

How to change JsonInclude.Include.NOT_NULL to JsonInclude.Include.ALWAYS #416

Open sravanthik07 opened 4 years ago

sravanthik07 commented 4 years ago

Hi, I have requirement to print 'null' values in json response. For that the generated java classes should @JsonInclude(JsonInclude.Include.ALWAYS). I tried to get some help inn searching across the git but I found "NOT_NULL" is hard coded in "JacksonBasicExtension". Please help us to find the solution. image

jpbelang commented 4 years ago

You may be going about this the wrong way. Should I do what you want, you would be breaking the RAML spec: Your declaration should be

types:
   mytype:
      billingAccountId:  string | nil

I'll check with the parser to make sure. Otherwise, there are two ways to do this: 1) Redefine the contents of the jackson plugin by having a META-INF/ramltopojo-plugin.properties file with your own plugin definition like:

myown.jackson2=\
            myown.jacksonfix.JacksonBasicExtension,\
            org.raml.ramltopojo.extensions.jackson2.JacksonDiscriminatorInheritanceTypeExtension,\
            org.raml.ramltopojo.extensions.jackson2.JacksonScalarTypeSerialization,\
            org.raml.ramltopojo.extensions.jackson2.JacksonUnionExtension,\
            org.raml.ramltopojo.extensions.jackson2.JacksonEnumExtension,\
            org.raml.ramltopojo.extensions.jackson2.JacksonAdditionalProperties

The myown.jacksonfix.JacksonBasicExtension would contain the fixed JacksonBasicExtension code.

2) You could write you own plugin that could fix the annotation so that you get what you want.

I'll get back to you on thursday.

sravanthik07 commented 4 years ago

Hi JP Belanger, Thank you for the quick response. I tried this option - billingAccountId: string | nil Exception on maven build : Error generating Java classes from: null: can't fetch type named string | nil

I will try the other approach's too.

jpbelang commented 4 years ago

Yes, the string|nil fix was recently fixed. try the current snapshot version from the mulesoft maven repository.

I'm going to be releasing it next week.

sravanthik07 commented 4 years ago

Ok sure. I will check on it. We have another use case to solve. Here is the screenshot:

image

Any way that RAML can add schema to the XML payload? How to get rid of wrappers in xml payload?

sravanthik07 commented 4 years ago

Hi @jpbelang , Any clues on adding schema to XML payload?

jpbelang commented 4 years ago

Sorry, got Christmassed.

Looking now.

jpbelang commented 4 years ago

I 'm pretty sure I support this:

https://github.com/raml-org/raml-spec/blob/master/versions/raml-10/raml-10.md#xml-serialization-of-type-instances

sravanthik07 commented 4 years ago

I think, this will help, but i have to construct the response payload based on the ACCEPT.MEDIATYPE from Request headers. For example, if ACCEPT is json, the following code should give me JSON payload. But since, the Person if having xml attribute, will json works here? types: Person: properties: name: type: string xml: attribute: true # serialize it as an XML attribute name: "fullname" # attribute should be called fullname addresses: type: Address[] xml: wrapped: true # serialize it into its own ... XML element

jpbelang commented 4 years ago

The annotations are on the data objects. The serialization should depend on the incoming HTTP headers.