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

Missing parts in generated Java classes #146

Closed
xtergo opened this issue Feb 7, 2014 · 32 comments
Closed

Missing parts in generated Java classes #146

xtergo opened this issue Feb 7, 2014 · 32 comments
Labels

Comments

@xtergo
Copy link

xtergo commented Feb 7, 2014

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

mySchemaMapper2 = new SchemaMapper(new RuleFactory(new JsonSchemaGenerationConfig(), new Jackson2Annotator(), new SchemaStore()), new SchemaGenerator());

        mySchemaMapper2.generate(codeModel, "Testar3", "com.domain.test", urlSourceTest3);


static class JsonSchemaGenerationConfig extends Object implements GenerationConfig {

    @Override
    public boolean isGenerateBuilders() {
        return true;
    }

    @Override
    public boolean isUsePrimitives() {
        return false;
    }

    @Override
    public Iterator<File> getSource() { // Change
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public File getTargetDirectory() { // Change
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getTargetPackage() { // Change
        return "com.domain.test";
    }

    @Override
    public char[] getPropertyWordDelimiters() {
        return "-_".toCharArray();
    }

    @Override
    public boolean isUseLongIntegers() {
        return false;
    }

    @Override
    public boolean isUseDoubleNumbers() {
        return false;
    }

    @Override
    public boolean isIncludeHashcodeAndEquals() {
        return true;
    }

    @Override
    public boolean isIncludeToString() {
        return true;
    }

    @Override
    public AnnotationStyle getAnnotationStyle() {
        return AnnotationStyle.JACKSON2;
    }

    @Override
    public Class<? extends Annotator> getCustomAnnotator() { // Change
        return null;
    }

    @Override
    public boolean isIncludeJsr303Annotations() {
        return true;
    }

    @Override
    public SourceType getSourceType() {
        return SourceType.JSONSCHEMA;
    }

    @Override
    public boolean isRemoveOldOutput() {
        return true;
    }

    @Override
    public String getOutputEncoding() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isUseJodaDates() {
        return false;
    }

}

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;

public static void main(String[] args)
{
    try {
        File myFile = new File("directory");

        mySchemaMapper.generate(codeModel, "Testar3Default", "com.domain.test", urlSourceTest3);

        mySchemaMapper2 = new SchemaMapper(new RuleFactory(new JsonSchemaGenerationConfig(), new Jackson2Annotator(), new SchemaStore()), new SchemaGenerator());
        mySchemaMapper2.generate(codeModel, "Testar3", "com.domain.test", urlSourceTest3);

        myFile.mkdirs();
        codeModel.build(myFile);
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

static class JsonSchemaGenerationConfig extends Object implements GenerationConfig {

    @Override
    public boolean isGenerateBuilders() {
        return true;
    }

    @Override
    public boolean isUsePrimitives() {
        return false;
    }

    @Override
    public Iterator<File> getSource() { // Change
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public File getTargetDirectory() { // Change
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getTargetPackage() { // Change
        return "com.domain.test";
    }

    @Override
    public char[] getPropertyWordDelimiters() {
        return "-_".toCharArray();
    }

    @Override
    public boolean isUseLongIntegers() {
        return false;
    }

    @Override
    public boolean isUseDoubleNumbers() {
        return false;
    }

    @Override
    public boolean isIncludeHashcodeAndEquals() {
        return true;
    }

    @Override
    public boolean isIncludeToString() {
        return true;
    }

    @Override
    public AnnotationStyle getAnnotationStyle() {
        return AnnotationStyle.JACKSON2;
    }

    @Override
    public Class<? extends Annotator> getCustomAnnotator() { // Change
        return null;
    }

    @Override
    public boolean isIncludeJsr303Annotations() {
        return true;
    }

    @Override
    public SourceType getSourceType() {
        return SourceType.JSONSCHEMA;
    }

    @Override
    public boolean isRemoveOldOutput() {
        return true;
    }

    @Override
    public String getOutputEncoding() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean isUseJodaDates() {
        return false;
    }

}

}

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 {

/**
 * 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);
}

}

pom.xml:

        <plugin>
          <groupId>com.googlecode.jsonschema2pojo</groupId> 
          <artifactId>jsonschema2pojo-maven-plugin</artifactId>
          <version>0.4.0</version>
          <configuration>
              <sourceDirectory>${basedir})</sourceDirectory>
              <outputDirectory>${basedir})</outputDirectory>
              <targetPackage>com.example.types</targetPackage>
              <useLongIntegers>false</useLongIntegers>
              <usePrimitives>false</usePrimitives>
              <skip>false</skip>
          </configuration>
          <executions> 
              <execution>
                  <goals>
                      <generateBuilders >true</generateBuilders>
                      <goal>generate</goal>
                  </goals>
              </execution>
          </executions>
    </plugin>
</build>
<dependencies>
    <dependency>
        <groupId>com.github.fge</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
      <groupId>org.jsonschema2pojo</groupId>
      <artifactId>jsonschema2pojo-core</artifactId>
      <version>0.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.codemodel</groupId>
        <artifactId>codemodel</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.0.1</version>
    </dependency>

@joelittlejohn
Copy link
Owner

It looks like you need to change this:

            "item": {

to this:

            "items": {

on lines 11 and 19 of your schema.

@xtergo
Copy link
Author

xtergo commented Feb 7, 2014

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);
    }

}

@joelittlejohn
Copy link
Owner

Ah, you also need to add "type": "object":

        "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"
                        },

@xtergo
Copy link
Author

xtergo commented Feb 7, 2014

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 :)

@xtergo
Copy link
Author

xtergo commented Feb 7, 2014

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?

@joelittlejohn
Copy link
Owner

"type": "object" is part of the standard. Your schema is not invalid so I doubt a tool would tell you that, it just doesn't mean what you think it means. For instance you can have additional properties in jsonschema used for your own data, so something like "item" is valid, it just doesn't apply any schema rule.

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.

@joelittlejohn
Copy link
Owner

It's draft03.

@xtergo
Copy link
Author

xtergo commented Feb 10, 2014

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"
    ]
}

@xtergo
Copy link
Author

xtergo commented Feb 10, 2014

I guess I have to wrap all entries after "type", "object" with "properties".

@joelittlejohn
Copy link
Owner

You got it.

@xtergo
Copy link
Author

xtergo commented Feb 12, 2014

Hi,

I've now succeeded to generate my Java classes with jsonschema2pojo. Very nice!
But I now wonder if there is a way to match a received message with a generated one.

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

@joelittlejohn
Copy link
Owner

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)?

@xtergo
Copy link
Author

xtergo commented Feb 12, 2014

To validate incoming messages (not using assert).

2014-02-12 15:36 GMT+01:00 Joe Littlejohn notifications@github.com:

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)?

Reply to this email directly or view it on GitHubhttps://github.com//issues/146#issuecomment-34873994
.

@joelittlejohn
Copy link
Owner

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?

@xtergo
Copy link
Author

xtergo commented Feb 12, 2014

Hi again,
Yes you are correct it wouldn't make much sense in that case. The code is for testing but I don't see how I can use "assert" in a handy/nice way. I was hoping there was some support to make one match against a prepared message (maybe a tool working with the Jackson2 classes)?
I hope my question makes sense...

@joelittlejohn
Copy link
Owner

You can do this with plain old asserts as long as you include equals and toString methods in your generated types (make sure this jsonschema2pojo option is enabled). So this will work:

assertThat(actualObject, is(equalTo(expectedObject)))

Because the objects implement a nicely formatted toString you'll get a message that shows the contents of the two objects if the assertion fails.

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

@xtergo
Copy link
Author

xtergo commented May 29, 2014

Hi again Joe,

If you have time for a more general Json (draft 3) question...
I now have a question regarding a schema that defines an array or different
schemas.
I'm confused how to generate the schema I want because I can't find
documentation about the encoding rules.
So this is the request I want to send:

"basket": [{
        "apple": {
        ...optional...
        }
},
{
        "banana": {
        ...optional and different type/schema from apple...
        }
}]

So I thought the schema should look like this:

"basket" : {
  "type" : "array",
  "items" : {
    "apple" : {         ...
      },      banana : {         ...     }  }}

but since the [ ] characters represents an array maybe the schema
should look like this:

"basket" : {
  "type" : "array",
  "items" : [      {
          "apple" :          {
               ...
          }     },     {

          "banana" :

          {
               ...
          }

     }

  ]}

or I the last schema an array of arrays?
My guess is that both schemas are valid regarding Json rules but means
different things.The first I hope is what I want and the second is
defining an array of arrays where the iner array consists of an
ordered list of objects.

Help with this would be much appreciated!

/Xter

@joelittlejohn
Copy link
Owner

Are the banana and apple properties mutually exclusive? Can an array item have both of those properties or does the array contain objects that will only have one or the other?

@joelittlejohn
Copy link
Owner

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 properties defined. You could fix this like:

"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 apple property and a banana property. You mention that you want that the banana object is a *"different type/schema from apple..." so I assume you don't want to allow an array item to have both of these properties, it should have one or the other.

So in JSON schema you can achieve this with a union type. This is where we supply more than one schema in the items part of the schema, and yes you're right that this is supplied as an array. So, here's an example of this (which I think is what you intend):

"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 items array.

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.

@xtergo
Copy link
Author

xtergo commented May 29, 2014

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.
My goal is to generate the request. But I'm not sure how to write the schema.
/Xter

Sent from my iPad

On 29 maj 2014, at 17:09, Joe Littlejohn notifications@github.com wrote:

Are the banana and apple properties mutually exclusive? Can an array item have both of those properties or does the array contain objects that will only have one or the other?


Reply to this email directly or view it on GitHub.

@joelittlejohn
Copy link
Owner

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.

@xtergo
Copy link
Author

xtergo commented May 30, 2014

Yes, intresting information. But I still have problems with the encoding. I guess I need to understand it in smaller steps.

Example schema:
"basket" : { "type" : "array", "items" : {
When encoding this, the last { will become a [ right?
/Xter

Sent from my iPad

On 29 maj 2014, at 23:21, Joe Littlejohn notifications@github.com wrote:

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.


Reply to this email directly or view it on GitHub.

@joelittlejohn
Copy link
Owner

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 items in this case would be an array of schemas (so { would become [).

@joelittlejohn
Copy link
Owner

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.

@xtergo
Copy link
Author

xtergo commented Jun 2, 2014

Hmm.. I don't really agree. The only reference to a union that the standard
specifies is for the attribute "type".
So even if your example in a way makes some sense I don't think it follows
the standard. I think it's confusing/wrong with the "[" character after
items in a schema, but when its encoded it makes sense.
So the last example where the "items" attribute is defining an uinon should
be changed... if you ask me. To the following:
"basket" : {
"type" : "array",
"items" : {
"type" : [
{ "type" : "object", "properties" : { "apple" : { ... }}},
{ "type" : "object", "properties" : { "banana" : { ... }}}
]
}
}

2014-05-30 11:20 GMT+02:00 Joe Littlejohn notifications@github.com:

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.


Reply to this email directly or view it on GitHub
#146 (comment)
.

@joelittlejohn
Copy link
Owner

From the JSON Schema validation spec, section 5.3.1.1:

The value of "items" MUST be either an object or an array. If it is an object, this object MUST be a valid JSON Schema. If it is an array, items of this array MUST be objects, and each of these objects MUST be a valid JSON Schema.

There's no need to use:

"items" : {
    "type" : [ ...

it's equivalent to:

"items" : [ ...

@xtergo
Copy link
Author

xtergo commented Jun 2, 2014

ok, so the standard states that its ok to use [] after items. But for me
this syntax:
"items:" : [ ... ]

defines a tuple and that is not same as a union. So I don't think these to
things are equivalent.

2014-06-02 10:45 GMT+02:00 Joe Littlejohn notifications@github.com:

From the JSON Schema validation spec, section 5.3.1.1:

The value of "items" MUST be either an object or an array. If it is an
object, this object MUST be a valid JSON Schema. If it is an array, items
of this array MUST be objects, and each of these objects MUST be a valid
JSON Schema.

There's no need to use:

"items" : {
"type" : [ ...

it's equivalent to:

"items" : [ ...


Reply to this email directly or view it on GitHub
#146 (comment)
.

@joelittlejohn
Copy link
Owner

Ah yes, you're right! Sorry, I had completely forgotten the tuple feature. I agree with your example above, the appropriate schema is "items": { "type" : [ ...

@xtergo
Copy link
Author

xtergo commented Jun 2, 2014

ok, great. Then I think I've understood the standard.
Thanks allot for your feedback!
/Xter

2014-06-02 14:31 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 "items": {
"type" : [ ...


Reply to this email directly or view it on GitHub
#146 (comment)
.

@xtergo
Copy link
Author

xtergo commented Sep 3, 2014

Btw. I can't find the "enum" type in the standard draft4 (
https://tools.ietf.org/html/draft-zyp-json-schema-04). But the available
online tools accepts it for draft 4. Do you know if it still is allowed to
use it or should one use "oneOf"?

/Xter

2014-06-02 14:51 GMT+02:00 Christer Jansson xtergo@gmail.com:

ok, great. Then I think I've understood the standard.
Thanks allot for your feedback!
/Xter

2014-06-02 14:31 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 "items": {
"type" : [ ...


Reply to this email directly or view it on GitHub
#146 (comment)
.

@joelittlejohn
Copy link
Owner

The validation rules got split off into another spec for draft04, I think
you're looking for this document:

http://json-schema.org/latest/json-schema-validation.html
On 3 Sep 2014 17:01, "xtergo" notifications@github.com wrote:

Btw. I can't find the "enum" type in the standard draft4 (
https://tools.ietf.org/html/draft-zyp-json-schema-04). But the available
online tools accepts it for draft 4. Do you know if it still is allowed to
use it or should one use "oneOf"?

/Xter

2014-06-02 14:51 GMT+02:00 Christer Jansson xtergo@gmail.com:

ok, great. Then I think I've understood the standard.
Thanks allot for your feedback!
/Xter

2014-06-02 14:31 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 "items": {
"type" : [ ...


Reply to this email directly or view it on GitHub
<
https://github.com/joelittlejohn/jsonschema2pojo/issues/146#issuecomment-44831283>

.


Reply to this email directly or view it on GitHub
#146 (comment)
.

@xtergo
Copy link
Author

xtergo commented Sep 3, 2014

Aha. That makes sense. Thanks allot!
/Xter

Sent from my iPad

On 3 sep 2014, at 21:55, Joe Littlejohn notifications@github.com wrote:

The validation rules got split off into another spec for draft04, I think
you're looking for this document:

http://json-schema.org/latest/json-schema-validation.html
On 3 Sep 2014 17:01, "xtergo" notifications@github.com wrote:

Btw. I can't find the "enum" type in the standard draft4 (
https://tools.ietf.org/html/draft-zyp-json-schema-04). But the available
online tools accepts it for draft 4. Do you know if it still is allowed to
use it or should one use "oneOf"?

/Xter

2014-06-02 14:51 GMT+02:00 Christer Jansson xtergo@gmail.com:

ok, great. Then I think I've understood the standard.
Thanks allot for your feedback!
/Xter

2014-06-02 14:31 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 "items": {
"type" : [ ...


Reply to this email directly or view it on GitHub
<
https://github.com/joelittlejohn/jsonschema2pojo/issues/146#issuecomment-44831283>

.


Reply to this email directly or view it on GitHub
#146 (comment)
.


Reply to this email directly or view it on GitHub.

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

No branches or pull requests

2 participants