FasterXML / jackson-module-jsonSchema

Module for generating JSON Schema (v3) definitions from POJOs
370 stars 136 forks source link

Long data type changed to integer #160

Open ajr3-gen opened 9 months ago

ajr3-gen commented 9 months ago

This was mentioned in an older issue but I want to bring it up again: the JsonSchema generator does not differentiate between longs and ints. Both types end up as "integer" in the schema. It is said that when data is retrieved, it will be treated as long or int based on its size and that may be so. But when one examines the JsonSchema object, everything is "integer". You can see this from the code shown in the Readme for this package. Here is a very simple demonstration of it; generating a schema from an object with an int, a long, and a Long simply represents them all as "integer".

Mapped object: {
  "type" : "object",
  "id" : "urn:jsonschema:org:example:Demonstrator",
  "properties" : {
    "myId" : {
      "type" : "string"
    },
    "myInteger" : {
      "type" : "integer"
    },
    "myLongPrimitive" : {
      "type" : "integer"
    },
    "myLongObject" : {
      "type" : "integer"
    }
  }
}

Is there any way to preserve the original type information for the properties?

cowtowncoder commented 9 months ago

My recollection may be wrong, but I thought JSON Schema didn't support all Java native types? See f.ex:

https://github.com/saasquatch/json-schema-inferrer/issues/17

and from there:

https://json-schema.org/understanding-json-schema/reference/type

If so, choices are number (more generic) or int for integral numeric types. I don't see a suggestion for separate "long" type, or modifier. This sort of makes sense given how JSON format itself does not really have anything to denote magnitude of integer (or, floating-point for that matter) types.

ajr3-gen commented 9 months ago

I guess I'm trying to do more with the schema than it's designed for. I'd like to generate a JsonSchema from an object and then be able to exactly describe the original object from that schema. But if the JsonSchema spec uses a single type to represent multiple Java types, then it is losing some of the source information so you really can't do the reverse conversion.

Based on a suggestion I found somewhere, annotations offer a potential workaround. If my source object is defined with an annotation like this:

public class Demonstrator {
    @JsonPropertyDescription("long")
    private Long myLong;
    // ... (plus getter and setter) ...
}

then the output includes the description:

Mapped object: {
  "type" : "object",
  "id" : "urn:jsonschema:org:example:Demonstrator",
  "properties" : {
    "myLong" : {
      "type" : "integer",
      "description" : "long"
    }
  }
}

So while this is not a general solution, it would help in cases where I have control over the original class.

cowtowncoder commented 9 months ago

Yes, I understand why it would be useful, just pointing out limitations.

I probably should also mention that this module is deprecated and no longer actively developed -- there are better, maintained alternatives in existence. We still accept PRs for bug fixes and improvements tho if anyone has time to try tackling issues. But there is no active development planned otherwise and module will not be brought along to Jackson 3.0.