kokuwaio/micronaut-openapi-codegen

Discriminator field is being duplicated

Closed this issue · 3 comments

When using an openapi spec with a discriminator similar to this

 Model:
      type: object
      discriminator:
        propertyName: modelType
      properties:
        modelType:
          type: String
      required:
        - modelType

The following class is generated

@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.MicronautCodegen", date = "2020-09-07T18:32:03.031189-03:00")
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "modelType", visible = true)
@com.fasterxml.jackson.annotation.JsonSubTypes({
.....
})
public abstract class Model {

	public static final String JSON_PROPERTY_NODE_TYPE = "modelType";

	@com.fasterxml.jackson.annotation.JsonProperty(JSON_PROPERTY_NODE_TYPE)
	@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS)
	private String modelType = null;
....

This generates duplicated entries in the json

{
    "modelType": "TYPE_A",
    "modelType": "TYPE_A",
...

even when the property is set to null:

{
    "modelType": "TYPE_A",
    "modelType": null,
...

Expected behavior: this field is not duplicated.

Ideas:
Either include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY could become include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXISTING_PROPERTY or com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS could be replaced by com.fasterxml.jackson.annotation.JsonInclude.Include.NOT_NULL.

The first proposal would force the user to set this value by himself. The second would force the user to never set this value.

I prefer 2. I'll implement a fix.

@stephanschnabel Another idea (my preferred one) is to mimic what happens if you define a discriminator but not a property with the same name, like this

MyModel:
      type: object
      required:
        - type
      discriminator:
        propertyName: type
      properties:
        id:
          type: string

The generated model is

@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include = com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@com.fasterxml.jackson.annotation.JsonSubTypes({
	.....
})
@io.micronaut.core.annotation.Introspected
public abstract class MyModel {

	public static final String JSON_PROPERTY_ID = "id";

	@com.fasterxml.jackson.annotation.JsonProperty(JSON_PROPERTY_ID)
	@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
	private java.lang.String id;
.... // no reference to type

I've create a pr:

  • discriminator property is not generated (only via jackson type mapping)
  • abstract class has abstract getter for discriminator
  • every subtype implements getter with his discriminator
  • test for string / enum discriminator