Thursday, 23 December 2021

Assing JsonIgnore on generated files in openapi

I am used to dabbling a bit with .xjb files, in order to get the generated Java files from the XSDs that I want.

I am not soo familiar with how this works when dealing with REST Services, YAML files and openapi.

But I needed it at work, because somehow the FAIL_ON_UNKNOWN_PROPERTIES was set to true, when it shouldn't be.

So a colleague gave me this little solution for in the pom.xml:

<additionalModelTypeAnnotations><![CDATA[ @com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown \u003D true)]]></additionalModelTypeAnnotations>

And I could add this to the "configOptions" of the "configuration" of the "openapi-generator-maven-plugin".

And this worked.

However!

This completely failed to explain why the property was set to true. From what I could gather the generated ObjectMapper provider sets this explicitly to false.

public class JSON implements ContextResolver<ObjectMapper> {
  private ObjectMapper mapper = new ObjectMapper();

  public JSON() {
    this.mapper.setSerializationInclusion(Include.NON_NULL);
    this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    this.mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    this.mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    this.mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
    this.mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
    this.mapper.setDateFormat(new RFC3339DateFormat());
    JsonNullableModule jnm = new JsonNullableModule();
    this.mapper.registerModule(jnm);
    this.mapper.registerModule(new JavaTimeModule());
  }

  public void setDateFormat(DateFormat dateFormat) {
    this.mapper.setDateFormat(dateFormat);
  }

  public ObjectMapper getContext(Class<?> type) {
    return this.mapper;
  }
}

So what was going on?

Luckily a colleague of mine put on his debugging gloves, and started looking into it.

Well, it turns out there was a ContextResolver (like the one above) defined as a @Provider, causing it to be discovered first by the JAX-RS runtime.

Once one is discovered, it will use that one.

And of course it didn't have this setting set to false.

It took a while to find a solution. Neither @Priority nor anything else seemed to have any effect.

In the end, I saw no other option, than to put extra logic in the ContextResolver with the @Provider on it, to not provide an ObjectMapper, if it was any of the objects that concerned my neck of the woods.

Conclusion

CDI is great, and I'm all for it.

The Advantage is that your components in your software are loosely coupled, and this makes things easier.

The Disadvantage, as found out now, is that sometimes things get Injected that you do not expect, and you have a Dickens of a time, finding out where they come from.

References

Openapi Generator Docs - Templating
https://openapi-generator.tech/docs/templating

No comments:

Post a Comment