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 annotate models with @JsonIgnoreProperties(ignoreUnknown = true) #433

Closed amreladawy closed 4 years ago

amreladawy commented 4 years ago

If there are unknown json properties, deserialisation fails.

jpbelang commented 4 years ago

Are you using raml defined types ? Have you tried with additional properties ?

amreladawy commented 4 years ago

Yes, I am using raml defined types. I just would like to have way to annotate the generated model in order to ignore any unknown properties in the server response json. I forgot to say that I am using it for REST client.

jpbelang commented 4 years ago

Ok, you are generating the model and using it in a rest client. Are you generating the jackson 2 annotations ?

amreladawy commented 4 years ago

Here is my plugin config details.


            <plugin>
                <groupId>org.raml.jaxrs</groupId>
                <artifactId>raml-to-jaxrs-maven-plugin</artifactId>
                <version>${raml-jaxrs-version}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.raml.jaxrs</groupId>
                        <artifactId>jaxrs-code-generator</artifactId>
                        <version>${raml-jaxrs-version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <ramlFile>${basedir}/src/main/resources/</ramlFile>
                    <includes>
                        <include>candidate-api/candidate.raml</include>
                        <include>order-api/order-api-proxy.raml</include>
                    </includes>
                    <resourcePackage>com.resources</resourcePackage>
                    <modelPackage>com.model</modelPackage>
                    <supportPackage>com.support</supportPackage>
                    <generateTypesWith>
                    </generateTypesWith>
                    <jsonMapperConfiguration>
                        <!-- <CustomAnnotator>@JsonIgnoreProperties(ignoreUnknown = true)</CustomAnnotator>-->
                    </jsonMapperConfiguration>
                </configuration>
            </plugin>
jpbelang commented 4 years ago

You should activate the plugins. Here, I've activated jackson2, jsr303 and equalsAndHashCode.

                    <modelPackage>example.model</modelPackage>
                    <resourcePackage>example.types</resourcePackage>
                    <supportPackage>example.support</supportPackage>
                    <generateTypesWith>
                        <value>jackson2</value>
                        <value>jsr303</value>
                        <value>equalsAndHashCode</value>
                    </generateTypesWith>
amreladawy commented 4 years ago

I have done this, but what should I except?

I can close this as I disabled the feature DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES on the ObjectMapper instead of annotating models with the JsonIgnoreProperties

jpbelang commented 4 years ago

If the code generated has the JsonAnyGetter and JsonAnySetter annotation, that should pick up any extra properties in there.

So jackson should be happy.

amreladawy commented 4 years ago

Those additionalProperties properties were added to the model. I am going to close this, as I can ignore additional unknown properties by customising the mapper

val target = ClientBuilder.newClient().register(
        ContextResolver {
            ObjectMapper()
                .enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature())
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
          .registerModule(Jdk8Module()).registerModule(ParameterNamesModule()).registerModule(GuavaModule())
        }
    ).target(url)