Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

additionalProperties java.util.Map is not generated by default when embedding this library and extending DefaultGenerationConfig #398

Closed
shivamsharma opened this issue Aug 15, 2015 · 6 comments
Milestone

Comments

@shivamsharma
Copy link

Json file is :

 {
        "type" : "object",
        "additionalProperties" : {
            "type" : "number"
        }
}

the code generated is :

package com.cognitree;

import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.apache.commons.lang.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
public class Device {


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}

But expected is :

 public class MyObject {

        private java.util.Map<String, Double> additionalProperties = new java.util.HashMap<String, Double>();

        @org.codehaus.jackson.annotate.JsonAnyGetter
        public java.util.Map<String, Double> getAdditionalProperties() {
            return this.additionalProperties;
        }

        @org.codehaus.jackson.annotate.JsonAnySetter
        public void setAdditionalProperties(String name, Double value) {
            this.additionalProperties.put(name, value);
        }

    }

as per documentation https://github.com/joelittlejohn/jsonschema2pojo/wiki/Reference#additionalproperties

@santhosh-tekuri
Copy link

you need to explicitly enable this feature:

RuleFactory ruleFactory = new RuleFactory();
ruleFactory.setGenerationConfig(new DefaultGenerationConfig(){
    @Override
    public boolean isIncludeAdditionalProperties(){
        return true;
    }
});
SchemaMapper schemaMapper = new SchemaMapper(ruleFactory, new SchemaGenerator());

by default this is disabled

@brentryan
Copy link

You need to remove additionalproperties from your json schema and by default it will include this in your generated class.

@shivamsharma
Copy link
Author

@brentryan I have tried removing "additionalProperties" from json but it was not working....

@brentryan
Copy link

does your plugin configuration look like this? What version are you using?

<plugin>
                <groupId>org.jsonschema2pojo</groupId>
                <artifactId>jsonschema2pojo-maven-plugin</artifactId>
                <version>0.4.14</version>
                <configuration>
                    <skip>false</skip>
                    <excludes>
                        <exclude>*.js</exclude>
                    </excludes>
                    <includeJsr303Annotations>true</includeJsr303Annotations>
                    <useLongIntegers>true</useLongIntegers>
                    <sourceDirectory>${basedir}/schema</sourceDirectory>
                    <outputDirectory>${basedir}/target/generated-sources</outputDirectory>
                    <targetPackage>com.model</targetPackage>
                </configuration>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

@joelittlejohn
Copy link
Owner

@shivamsharma You don't mention how you're using jsonschema2pojo, either via Maven, Ant, Gradle or embedded in your project. I'm going to assume though that you're using this embedded in your project (using the API directly) because I've just noticed the following bug:

This should be returning true by default but it is returning false in the v0.4.14. You can work around this using the method that @santhosh-tekuri mentions above, but I'll fix this now.

@shivamsharma
Copy link
Author

I tried using both by maven & embedding code. Yeah I am using the code suggested by @santhosh-tekuri. Hope you will fix the bug as soon as possible.

@joelittlejohn joelittlejohn added this to the 0.4.15 milestone Aug 17, 2015
@joelittlejohn joelittlejohn changed the title java.util.Map is not generated for property additionalProperties java.util.Map is not generated by default when embedding this library and extending DefaultGenerationConfig Sep 1, 2015
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants