Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long data type changed to integer #160

Open
ajr3-gen opened this issue Jan 26, 2024 · 3 comments
Open

Long data type changed to integer #160

ajr3-gen opened this issue Jan 26, 2024 · 3 comments

Comments

@ajr3-gen
Copy link

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
Copy link
Member

cowtowncoder commented Jan 26, 2024

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

saasquatch/json-schema-inferrer#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
Copy link
Author

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants