learningProgrammer
learningProgrammer

Reputation: 69

JSON Schema references duplicating files instead of using the one passed as reference

I am starting to use the JSON Schema to java pojo in one of the projects I am working on (org.jsonschema2pojo). I have sucessfully referenced one schema inside the other one and I can see the same info is there. However, the json schema where I use the reference instead of referncing the pojo of the original one creates a new file with the exact same information. Example:

Json Schema A:

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema#",
      "$id": "https://example.com/schemas/A",
      "description": "Some Json Schema",
      "type": "object",
      "properties": {
        "genericInfo": {
          "type": "string",
          "description": "Some generic info"
        }
      }
      "required": ["genericInfo"]
    }

Json Schema B :

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema#",
      "$id": "https://example.com/schemas/B",
      "description": "Some Json Schema B",
      "type": "object",
      "properties": {
        "referenceToA": {
          "$ref": "../jsonSchemaA.json"
         }
       }
    }

My POJO of B has the information of A but instead of referencing the same file it creates a new file with the same name + information. Is there a way to stop this from happening? Is this a known Issue? Since I need to reference schemas between them but use common pojos in a lot of them I would need to always cast it to the original one if I can´t solve this issue.

Thanks in advance

Upvotes: -1

Views: 121

Answers (2)

learningProgrammer
learningProgrammer

Reputation: 69

I was able to find a workaround to this issue. I was unable to reference the schemas without them being compiled in different directories as I intended. However, by defining different execution phases on the pom.xml I was able to reference the compiled java classes directly inside the schemas that I wanted.

On each one of the execution phases I specified the dourceDirectory + outputDirectory and generating first the classes to be referenced. Then I reference the path of each java class inside the json schemas I want to ahve a reference to this type by referencing it like this:

"yourReference" : {
  "type" : "object",
  "javaType" : "location.of.class.Name"
}

Hope this helps

Upvotes: 1

Jeremy Fiel
Jeremy Fiel

Reputation: 3254

You are creating a new object schema with the reference inside of it. If you just want to reference the A schema, no need to define properties again.

EDIT:

If you want to compose a larger schema while referencing other reusable schemas, you can use the allOf keyword. Unfortunately, the package you are using seems to only support up to draft-03, which is very old and doesn't support the allOf syntax. I would recommend finding another package to work with.

Schema B

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schemas/B",
  "description": "Some Json Schema B",
  "allOf": [
    {"$ref": "../jsonSchemaA.json"},
    {
      "type": "object",
      "properties": {
        "another_prop": {"type": "string"}
      }
    }
  ]
}

If you really, really want to stick with this package, you can try using the draft-03 syntax. No guarantee it will work.


{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "id": "https://example.com/schemas/B",
  "description": "Some Json Schema B",
  "extends": {
    "$ref": "../jsonSchemaA.json"
  },
  "type": "object",
  "properties": {
    "another_prop": {"type": "string"}
  }
}

Upvotes: 0

Related Questions