FasterXML / jackson-module-jsonSchema

Module for generating JSON Schema (v3) definitions from POJOs
371 stars 135 forks source link

JsonNullable and Optional type generate wrong json schema #157

Closed MelleD closed 1 year ago

MelleD commented 1 year ago

I have follow DTO

import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import javax.validation.Valid;
import org.openapitools.jackson.nullable.JsonNullable;
import com.fasterxml.jackson.annotation.JsonProperty;

public class TestDto {

   @JsonProperty( "id" )
   private Long id;

   @JsonProperty( "type" )
   private JsonNullable<String> type = JsonNullable.undefined();

   @JsonProperty( "nameDtos" )
   @Valid
   private Set<NameDto> nameDtos = new LinkedHashSet<>();

   @JsonProperty( "labels" )
   @Valid
   private JsonNullable<Set<String>> labels = JsonNullable.undefined();

   @JsonProperty( "test" )
   @Valid
   private Optional<String> test;

   public Long getId() {
      return id;
   }

   public void setId( final Long id ) {
      this.id = id;
   }

   public TestDto id( final Long id ) {
      setId( id );
      return this;
   }

   public JsonNullable<String> getType() {
      return type;
   }

   public void setType( final JsonNullable<String> type ) {
      this.type = type;
   }

   public Set<NameDto> getNameDtos() {
      return nameDtos;
   }

   public void setNameDtos( final Set<NameDto> nameDtos ) {
      this.nameDtos = nameDtos;
   }

   public JsonNullable<Set<String>> getLabels() {
      return labels;
   }

   public void setLabels( final JsonNullable<Set<String>> labels ) {
      this.labels = labels;
   }

   public Optional<String> getTest() {
      return test;
   }

   public void setTest( final Optional<String> test ) {
      this.test = test;
   }
}

This generated follow json schema

{
  "type": "object",
  "id": "urn:jsonschema:model:TestDto",
  "properties": {
    "id": {
      "type": "integer"
    },
    "type": {
      "type": "object",
      "id": "urn:jsonschema:org:openapitools:jackson:nullable:JsonNullable",
      "properties": {
        "present": {
          "type": "boolean"
        }
      }
    },
    "nameDtos": {
      "type": "array",
      "items": {
        "type": "object",
        "id": "urn:jsonschema:NameDto",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "labels": {
      "type": "object",
      "id": "urn:jsonschema:org:openapitools:jackson:nullable:JsonNullable>",
      "properties": {
        "present": {
          "type": "boolean"
        }
      }
    },
    "test": {
      "type": "object",
      "id": "urn:jsonschema:java:util:Optional",
      "properties": {
        "empty": {
          "type": "boolean"
        },
        "present": {
          "type": "boolean"
        }
      }
    }
  }

I would expect JsonNullable or Optional not to contain present and empty but the generic classes, when I register the modules.JsonNullable module see here (similar) to optional.

    final ObjectMapper mapper = new ObjectMapper();
     mapper.registerModule( new JsonNullableModule() );
     mapper.registerModule(new Jdk8Module());
pjfanning commented 1 year ago

openapitools wrote that JsonNullableModule for their openapisupport - maybe you should use their tools to gen openapi instead of using these deprecated jackson module

MelleD commented 1 year ago

openapitools wrote that JsonNullableModule for their openapisupport

No they use it for JsonMergePatch https://www.rfc-editor.org/rfc/rfc7386, but for Optional it is the same issue

maybe you should use their tools to gen openapi instead of using these deprecated jackson module

Why this jackson module deprecated?

pjfanning commented 1 year ago

https://github.com/FasterXML/jackson-module-jsonSchema#future-plans-lack-thereof

MelleD commented 1 year ago

https://github.com/FasterXML/jackson-module-jsonSchema#future-plans-lack-thereof

Oh ok thanks then yes you are right have to look into open api generator

cowtowncoder commented 1 year ago

FWTW we can still merge contributed fixes, esp, ones for JsonSerializers of types which are outside of JSON Schema module. But @pjfanning is right, this module is not actively maintained by anyone (has no owner) and only supports older version of JSON Schema spec.