I have some json schema that I try to convert to pojo classes using jsonschema2pojo.
Unfortunately, I get some duplicated classes generated with an additional __1 postfix on the classname.
You can test this at https://www.jsonschema2pojo.org/.
Add this example and hit Preview:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"something": {
"type": "object",
"properties": {}
},
"other": {
"type": "object",
"properties": {
"physical": {
"$ref": "#/properties/something"
}
}
}
}
}
I get the classes Something and Something__1. They have the same code (except the class name).
I found other questions, where someone commented that one can change some ObjectRule and RuleFactory, but I don't want to patch the library.
Is there something with my schema or is this a bug?
jung,
The below code might be working for you, I am facing the same issue for a week and walked through the lots of docs and references available.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"other": {
"type": "object",
"properties": {
"physical": {
"existingJavaType":"com.example.Something" // package name with class name
}
}
},
"something": {
"type": "object"
}
}
}
Refer this
Related
I'm building a custom generator to generate TypeScript/Angular models for an angular application. For a starting point, I copied the code from https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java
I'm trying to figure out how to prevent the generator from generating "AllOf" and from extending models from "AllOf" models.
The generator already generates a model with everything it needs without needing to extend from *AllOf.
I've been able to modify the .java file to prevent it from importing classes that with with "AllOf", but I can't find any documentation or examples to restrict extending from classes that end with "AllOf"
Am I missing something? It seems like there should be a way to tell the generator to just not import or create "AllOf" classes.
// what I get:
import { ValidatePassword } from './validate-password.model';
export interface ChangePassword extends ChangePasswordAllOf, ValidatePassword {
...
}
// what I want
import { ValidatePasswordModel } from './validate-password.model';
export interface ChangePassword extends ValidatePassword {
...
}
Here's my modelGeneric.mustache template:
export interface {{classname}}{{#allOf}}{{#-first}} extends {{/-first}}{{{.}}}{{^-last}}, {{/-last}}{{/allOf}} { {{>modelGenericAdditionalProperties}}
{{#vars}}
{{#description}}
/**
* {{{.}}}
*/
{{/description}}
{{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/vars}}
}{{>modelGenericEnums}}
Here's the relevant schema sample:
...
"ChangePassword": {
"allOf": [
{
"$ref": "#/components/schemas/ValidatePassword"
},
{
"type": "object",
"additionalProperties": false,
"required": [
"OldPassword"
],
"properties": {
"OldPassword": {
"title": "Current password",
"type": "string",
"minLength": 1
}
}
}
]
},
...
According to this: https://github.com/OpenAPITools/openapi-generator/issues/3100
Could you try:
"ChangePassword": {
"type": "object",
"additionalProperties": false,
"required": [
"OldPassword"
],
"properties": {
"OldPassword": {
"title": "Current password",
"type": "string",
"minLength": 1
}
},
"allOf": [{
"$ref": "#/components/schemas/ValidatePassword"
}]
}
using 4.3.1 of open api generator and trying to get the java code out of the json.
In the json file I have the following (changed for example):
"xComponents": {
"type": "array",
"title": "This is a title",
"items": {
"oneOf": [
{
"$ref": "xComponent.AAA.schema.json"
},
{
"$ref": "xComponent.BBB.schema.json"
},
{
"$ref": "xComponent.CCC.schema.json"
},
{
"$ref": "xComponent.DDD.schema.json"
}
],
"type": "object"
},
"minItems": 1
}
It generates this list wierd class name that cannot get build:
private List<**OneOfxComponentAAASchemaJsonxComponentBBBSchemaJsonxComponentCCCSchemaJsonxComponentDDDSchemaJson**> xComponents =
new ArrayList<**OneOfxComponentAAASchemaJsonxComponentBBBSchemaJsonxComponentCCCSchemaJsonxComponentDDDSchemaJson**>();
Whats the correct way to deal with oneOf? what im I doing wrong (either with the json file or with the open api generator)?
I am trying to validate json with json schema, problem is i have created different json schema files for complex object. I need to include in to main schema using ref tag. and trying to validate my json using everit lib. schema got loaded but when i trying to validate my sample json it is not validating inner schema object.
InnerObject.json
{
"$id": "http://example.com/example.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"typeCode": {
"$id": "/properties/typeCode",
"type": "string"
},
"description": {
"$id": "/properties/description",
"type": "string"
},
"expDate": {
"$id": "/properties/expDate",
"type": "string"
},
"issuingCountry": {
"$id": "/properties/issuingCountry",
"type": "string"
}
},
"required": [
"typeCode",
"description",
"expDate",
"issuingCountry"
]
}
OuterObject.JSON
{
"$id": "http://example.com/example.json",
"type": "object",
"definitions": {
},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"firstName": {
"$id": "/properties/firstName",
"type": "string"
},
"lastName": {
"$id": "/properties/lastName",
"type": "string"
},
"innerObject": {
"$id": "#item",
"type": "object",
"$ref": "file://com/mypack/innerObject.json"
}
},
"required": [
"firstName",
"lastName",
"innerObject"
]
}
Both files are inside my project src/main/resource
I have json validator class which is use to test my schema.
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
public class JSONSchemaValidator {
public static void main(String[] args) {
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaValidator.class
.getResourceAsStream("/com/schema/outer.schema.json")));
JSONObject outerJson = new JSONObject();
JSONObject innerJson = new JSONObject();
innerJson.put("expDate", "expDate");
innerJson.put("typeCode", "typeCode");
outerJson.put("firstName", "firstName");
outerJson.put("lastName", "lastName");
outerJson.put("innerObject", innerJson);
Schema schema = SchemaLoader.load(jsonSchema);
System.out.println(schema.getDescription());
schema.validate(outerJson);
System.out.println("done");
}
}
Its validating 1st level using schema but not for inner level. can any one suggest what is did wrong, so that its not validating the json.
Sample JSON which i am trying to validate is :
{"firstName":"firstName","lastName":"lastName","innerObject":{"expDate":"expDate","typeCode":"typeCode"}}
It should thrown an error as "typeCode" "description","expDate",issuingCountry" 4 fields are mandetory and in input i am passing only two. so for remaining two it should thrown an error.
Just provide inner json file in outer json with ref tag.
for example :
{
"$id": "http://example.com/example.json",
"type": "object",
"definitions": {
},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"firstName": {
"$id": "/properties/firstName",
"type": "string"
},
"lastName": {
"$id": "/properties/lastName",
"type": "string"
},
"innerObject": {
"$ref": "innerObject.json"
}
},
"required": [
"firstName",
"lastName",
"innerObject"
]
}
In Java code you need to set resolutionScope, you need to provide path of your inner json.
String path="file:" + Paths.get("").toUri().getPath() + "PATH_OF_YOUR_JSONSchema";
System.out.println("older"+ path);
if (path.contains(" ")) {
path=path.replaceAll(" ", "%20");
}
SchemaLoader sl=SchemaLoader.builder().schemaJson(jsonSchema).resolutionScope(path ).build();
Schema schema=sl.load().build();
schema.validate(yourJsonObject);
"recordOne": {
"properties": {
"id": {
"type": "integer"
},
"recordtwo": {
"properties": {
"propertyone": {
"type": "integer"
},
"propertytwo":{
"type":"date"
}
}
}
},
"_parent": {
"type": "parentrecord"
}
}
Here i am trying to access recordtwo properties like "doc.recordtwo.propertyone"
i am getting this exception :
'Elasticsearch exception [type=illegal_argument_exception, reason=No
field found for [recordtwo] in mapping with types [recordOne]]'
Though i am able to access id. i.e.
"doc.id"
I tried below and it works.
"doc['recordtwo.propertyone']"
Hi I'm using the JSON Schema evaluator in version 2.2.6 to validate my server responses. These responses can contain single objects of type A, B or C, but also composite objects, e.g., D containing an array of A objects. To reuse the schema definitions of each object, I started to describe all entities in the same file as described here. Now my problem is, that I have to reference one of those single objects when validating the response.
Here is my (not) SWE.
JSON schema file:
{
"id":"#root",
"properties": {
"objecta": {
"type": "object",
"id":"#objecta",
"properties": {
"attribute1": {"type": "integer"},
"attribute2": {"type": "null"},
},
"required": ["attribute1", "attribute2"]
},
"objectb": {
"type": "object",
"id":"#objectb",
"properties": {
"attribute1": {"type": "integer"},
"attribute2": {
"type": "array",
"items": {
"$ref": "#/objecta"
}
}
}
},
"required": ["attribute1", "attribute2"]
},
}
}
Now I want to validate a server response containing object B. For this, I tried the following:
public class SchemeValidator {
public static void main(String[] args) {
String jsonData = pseudoCodeFileLoad("exampleresponse/objectb.txt");
final File jsonSchemaFile = new File("resources/jsonschemes/completescheme.json");
final URI uri = jsonSchemaFile.toURI();
ProcessingReport report = null;
try {
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(uri.toString() + "#objectb");
JsonNode data = JsonLoader.fromString(jsonData);
report = schema.validate(data);
} catch (JsonParseException jpex) {
// ... handle parsing errors etc.
}
}
}
The problem is that the scheme is not loaded correctly. I either get no error (even for invalid responses) or I get fatal: illegalJsonRef as the scheme seems to be empty. How can I use the schema of object b in my Java code? Thank you!!
It looks like your $ref is incorrect. It needs to be a relative reference from the base of the JSON Schema file (see here).
So your JSON schema would become:
{
"id":"#root",
"properties": {
"objecta": {
"type": "object",
"id":"#objecta",
"properties": {
"attribute1": {"type": "integer"},
"attribute2": {"type": "null"},
},
"required": ["attribute1", "attribute2"]
},
"objectb": {
"type": "object",
"id":"#objectb",
"properties": {
"attribute1": {"type": "integer"},
"attribute2": {
"type": "array",
"items": {
"$ref": "#/properties/objecta"
}
}
}
},
"required": ["attribute1", "attribute2"]
},
}
}
I've added '/properties' to your $ref. It's operating like XPath to the definition of the object in the schema file.