Replace reference object type with another schema path #436
-
Hello, I am having trouble generating a specific schema. Let's say I have the following classes: package org.example.geo: public class Region {
private int region_id;
private String region_name;
} and public class Student {
private int student_id;
private String student_name;
private Region region;
} What I am trying to achieve are the following schemas: {
"type": "object",
"properties": {
"region_id": {
"type" : "int"
},
"region_name": {
"type": "String"
}
}
} This will be saved under org.example.geo.region {
"type": "object",
"properties": {
"student_id": {
"type" : "int"
},
"student_name": {
"type": "String"
},
"region": {
"type": "org.example.geo.region"
}
}
} The part I am failing to do is generating region's type as "org.example.geo.region". Any help or clue is greatly appreciated! Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @PintOfBitter, what you're proposing is strictly speaking an invalid JSON schema. There are only a handful of supported values for the Since others have asked for similar things, there is already an example for introducing external references here: ExternalRefPackageExample Your target schema file for the {
"type": "object",
"properties": {
"student_id": {
"type" : "integer"
},
"student_name": {
"type": "string"
},
"region": {
"$ref": "./org/example/geo/region"
}
}
} However, for completeness' sake: technically you can override anything – including the configBuilder.forTypesInGeneral()
.withTypeAttributeOverride((node, scope, context) -> {
if (scope.getType().getErasedType() == Region.class) {
node.put(context.getKeyword(SchemaKeyword.TAG_TYPE), "org.example.geo.region");
}
}); |
Beta Was this translation helpful? Give feedback.
Hi @PintOfBitter,
what you're proposing is strictly speaking an invalid JSON schema. There are only a handful of supported values for the
type
keyword. https://json-schema.org/understanding-json-schema/reference/typeIt's not meant to be overridden with custom references to external schemas. The
$ref
keyword supports that kind of thing.Since others have asked for similar things, there is already an example for introducing external references here: ExternalRefPackageExample
If you search for it here on the repository, you'll see the few issues/discussions raised on that topic.
Your target schema file for the
student
would then more look like this: