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
Missing parts in generated Java classes #146
Comments
It looks like you need to change this:
to this:
on lines 11 and 19 of your schema. |
Didn't change anything for me: schema: {
"type": "object",
"properties": {
"carId": {
"description": "Unique Id for the car",
"type": "string"
},
"features": {
"description": "Features for the car",
"type": "array",
"items": {
"featureId": {
"description": "Unique id for the feature.",
"type": "string"
},
"things": {
"type": "array",
"description": "List of things this feature contains",
"items": {
"thingId": {
"description": "The unique identifier for a thing.",
"type": "string"
},
"parts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"partId ": {
"description": "The unique identifier for a part",
"type": "string"
},
"cost": {
"description": "The cost for this thing",
"type": "number"
},
"noOfDecimals": {
"description": "Number of decimal places",
"type": "number"
}
}
}
}
}
}
},
"required": [
"featureId",
"things"
]
}
},
"required": [
"carId",
"features"
]
} Generated class: package com.domain.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"carId",
"features"
})
public class Testar3 {
/**
* Unique Id for the car
*
*/
@JsonProperty("carId")
private String carId;
/**
* Features for the car
*
*/
@JsonProperty("features")
private List<Object> features = new ArrayList<Object>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
* Unique Id for the car
*
*/
@JsonProperty("carId")
public String getCarId() {
return carId;
}
/**
* Unique Id for the car
*
*/
@JsonProperty("carId")
public void setCarId(String carId) {
this.carId = carId;
}
public Testar3 withCarId(String carId) {
this.carId = carId;
return this;
}
/**
* Features for the car
*
*/
@JsonProperty("features")
public List<Object> getFeatures() {
return features;
}
/**
* Features for the car
*
*/
@JsonProperty("features")
public void setFeatures(List<Object> features) {
this.features = features;
}
public Testar3 withFeatures(List<Object> features) {
this.features = features;
return this;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object other) {
return EqualsBuilder.reflectionEquals(this, other);
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
} |
Ah, you also need to add "features": {
"description": "Features for the car",
"type": "array",
"items": {
"type": "object", /* <----------- here */
"featureId": {
"description": "Unique id for the feature.",
"type": "string"
},
"things": {
"type": "array",
"description": "List of things this feature contains",
"items": {
"type": "object", /* <----------- and here */
"thingId": {
"description": "The unique identifier for a thing.",
"type": "string"
}, |
ok, great now it works. A Feature.java class was generated. Hurra! So I have some question now. Could I have validated this in an easy way? Is there for instance an online tool or something that could have detected this? I guess this is part of the standard to have to include ""type": "object" or is that mandatory for this tool? Thanks for your insanely fast response :) |
After some googeling... maybe this depends on which draft the json shema is built on?! If so is jasonshcema2pojo hard coded to a specific version of can it be configured? |
I know there are some online json-schema generators/tools so you could have a go with those. It's worth some time googling I think. |
It's draft03. |
I just continued with this and realized that Feature.class was generated but it basically just contained "private Map<String, Object> additionalProperties = new HashMap<String, Object>();" I guess I would have expected more generated classes. I also guess I need to modify my schema in some way to get these classes... but don't know how. All parts now have a type defined. {
"type": "object",
"properties": {
"carId": {
"description": "Unique Id for the car",
"type": "string"
},
"features": {
"description": "Features for the car",
"type": "array",
"items": {
"type": "object",
"featureId": {
"description": "Unique id for the feature.",
"type": "string"
},
"things": {
"type": "array",
"description": "List of things this feature contains",
"items": {
"type": "object",
"thingId": {
"description": "The unique identifier for a thing.",
"type": "string"
},
"parts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"partId ": {
"description": "The unique identifier for a part",
"type": "string"
},
"cost": {
"description": "The cost for this thing",
"type": "number"
},
"noOfDecimals": {
"description": "Number of decimal places",
"type": "number"
}
}
}
}
}
}
},
"required": [
"featureId",
"things"
]
}
},
"required": [
"carId",
"features"
]
} |
I guess I have to wrap all entries after "type", "object" with "properties". |
You got it. |
Hi, I've now succeeded to generate my Java classes with jsonschema2pojo. Very nice! e.g. result = match(receivedMsg, expectedMsg); So basically I'd like to make an instance of the auto-generated java message where I specifically say that I'd like check some specific values or just check that some parameters exist, some parameters don't exist (and some parameters can have whatever value). Maybe you know how this can be handled? /Xter |
Are you thinking about this from a testing point of view (assert that received == expected) or are you attempting to validate incoming messages (required values, constrained values, etc)? |
To validate incoming messages (not using assert). 2014-02-12 15:36 GMT+01:00 Joe Littlejohn notifications@github.com:
|
I'm not sure why you would be generating an expected message in that case. Wouldn't it be more a case of checking for required fields and validating some constraints? You probably don't have an expected message to match with, right? |
Hi again, |
You can do this with plain old asserts as long as you include assertThat(actualObject, is(equalTo(expectedObject))) Because the objects implement a nicely formatted If you want something that shows specifically which properties don't match then you'll need to use a utility that compares Java bean properties intelligently. This is a general problem, not specific to jsonschema2pojo. This link might help: http://stackoverflow.com/questions/1741041/assert-that-two-java-beans-are-equivalent |
Hi again Joe, If you have time for a more general Json (draft 3) question...
So I thought the schema should look like this:
but since the [ ] characters represents an array maybe the schema
or I the last schema an array of arrays? Help with this would be much appreciated! /Xter |
Are the |
You first example that looks like this: "basket" : {
"type" : "array",
"items" : {
"apple" : { ... },
"banana" : { ... }
}
} is not valid (well, to be precise, it's not doing what you are intending). Here you are providing a schema for items (the values within the basket array) but that schema has no "basket" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"apple" : { ... },
"banana" : { ... }
}
}
} Of course this schema implies that a single object in the array may have both an So in JSON schema you can achieve this with a union type. This is where we supply more than one schema in the "basket" : {
"type" : "array",
"items" : [
{
"type" : "object",
"properties" : {
"apple" : { ... },
}
},
{
"type" : "object",
"properties" : {
"banana" : { ... }
}
}
]
} Here we're saying that the objects inside the basket array will be valid according to one of the schemas present in the Unfortunately, jsonschema2pojo doesn't have any support for union types. In theory we could create a hierarchy of types here (Apply and Banana could both implement the interface BasketItem) however Java binding libraries cannot understand which type to instantiate when reading the JSON data without additional type hints being added inside the data itself. |
The basket i a list. It was maybe a bad example of me to call it a basket. The apple and banana are of different types (schemas) and they might occur zero or many times in any order. Sent from my iPad
|
Yes, I understand that the basket is a JSON array. Did you see the comment above? It should help you understand how to create a schema. |
Yes, intresting information. But I still have problems with the encoding. I guess I need to understand it in smaller steps. Example schema: Sent from my iPad
|
If you want to imply that the array can contain a mix of values where there are multiple, distinct schemas that might apply to the object, then yes you need a union type. The value of |
This is exactly as I've shown in the examples above, but I'll repeat for clarity. Either you merge everything together and create one schema: "basket" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"apple" : { ... },
"banana" : { ... }
}
}
} Or you offer multiple schemas that an item could conform to: "basket" : {
"type" : "array",
"items" : [
{
"type" : "object",
"properties" : {
"apple" : { ... },
}
},
{
"type" : "object",
"properties" : {
"banana" : { ... }
}
}
]
} jsonschema2pojo does not support the latter, but it's a perfectly good JSON schema. |
Hmm.. I don't really agree. The only reference to a union that the standard 2014-05-30 11:20 GMT+02:00 Joe Littlejohn notifications@github.com:
|
From the JSON Schema validation spec, section 5.3.1.1:
There's no need to use:
it's equivalent to:
|
ok, so the standard states that its ok to use [] after items. But for me defines a tuple and that is not same as a union. So I don't think these to 2014-06-02 10:45 GMT+02:00 Joe Littlejohn notifications@github.com:
|
Ah yes, you're right! Sorry, I had completely forgotten the tuple feature. I agree with your example above, the appropriate schema is |
ok, great. Then I think I've understood the standard. 2014-06-02 14:31 GMT+02:00 Joe Littlejohn notifications@github.com:
|
Btw. I can't find the "enum" type in the standard draft4 ( /Xter 2014-06-02 14:51 GMT+02:00 Christer Jansson xtergo@gmail.com:
|
The validation rules got split off into another spec for draft04, I think http://json-schema.org/latest/json-schema-validation.html
|
Aha. That makes sense. Thanks allot! Sent from my iPad
|
What steps will reproduce the problem?
Generate Java classes from a JSON schema
What is the expected output?
I expect that all entries in the JSON schema would be possible to get and set via the auto generated Java code.
What do you see instead?
Only Java methods to a certain level are generated. It ends with reference to a list of Objects:
private List features = new ArrayList();
How can I for instance set the cost for a specific part?
What version of the tool are you using? On what Java version? (On what
version of Maven/Ant if applicable?)
Java 7, Eclipse, maven with pom
testar3.schema.json:
{
"type": "object",
"properties": {
"carId": {
"description": "Unique Id for the car",
"type": "string"
},
"features": {
"description": "Features for the car",
"type": "array",
"item": {
"featureId": {
"description": "Unique id for the feature.",
"type": "string"
},
"things": {
"type": "array",
"description": "List of things this feature contains",
"item": {
"thingId": {
"description": "The unique identifier for a thing.",
"type": "string"
},
"parts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"partId ": {
"description": "The unique identifier for a part",
"type": "string"
},
"cost": {
"description": "The cost for this thing",
"type": "number"
},
"noOfDecimals": {
"description": "Number of decimal places",
"type": "number"
}
}
}
}
}
}
},
"required": [
"featureId",
"things"
]
}
},
"required": [
"carId",
"features"
]
}
code:
public class GenerateJavaClassesFromJsonSchema3 {
private static final String TEST3_JSON_SCHEMA = "testar3.schema.json";
static URL urlSourceTest3 = GenerateJavaClassesFromJsonSchema3.class.getResource(TEST3_JSON_SCHEMA);
static JCodeModel codeModel = new JCodeModel();
static SchemaMapper mySchemaMapper = new SchemaMapper();
static SchemaMapper mySchemaMapper2;
}
result (Testar3.java):
package com.domain.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"carId",
"features"
})
public class Testar3 {
}
pom.xml:
The text was updated successfully, but these errors were encountered: