joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.22k stars 1.65k forks source link

NPE when enabling includeDynamicAccessors #666

Open ben-manes opened 7 years ago

ben-manes commented 7 years ago

I was curious to see what the includeDynamicAccessors flag outputted. When I added it the task failed with,

Caused by: java.lang.NullPointerException
        at com.sun.codemodel.JInvocation.generate(JInvocation.java:176)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JInvocation.state(JInvocation.java:198)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
        at com.sun.codemodel.JBlock.generate(JBlock.java:436)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JConditional.state(JConditional.java:115)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
        at com.sun.codemodel.JBlock.generate(JBlock.java:436)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JConditional.state(JConditional.java:115)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
        at com.sun.codemodel.JBlock.generate(JBlock.java:436)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JConditional.state(JConditional.java:117)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
        at com.sun.codemodel.JBlock.generate(JBlock.java:436)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JConditional.state(JConditional.java:117)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
        at com.sun.codemodel.JBlock.generate(JBlock.java:436)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JConditional.state(JConditional.java:117)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
        at com.sun.codemodel.JBlock.generate(JBlock.java:436)
        at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
        at com.sun.codemodel.JBlock.state(JBlock.java:464)
        at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
        at com.sun.codemodel.JMethod.declare(JMethod.java:460)
        at com.sun.codemodel.JFormatter.d(JFormatter.java:376)
        at com.sun.codemodel.JDefinedClass.declareBody(JDefinedClass.java:815)
        at com.sun.codemodel.JDefinedClass.declare(JDefinedClass.java:788)
        at com.sun.codemodel.JFormatter.d(JFormatter.java:376)
        at com.sun.codemodel.JFormatter.write(JFormatter.java:406)
        at com.sun.codemodel.JPackage.build(JPackage.java:438)
        at com.sun.codemodel.JCodeModel.build(JCodeModel.java:311)
        at org.jsonschema2pojo.Jsonschema2Pojo.generate(Jsonschema2Pojo.java:82)
        at org.jsonschema2pojo.Jsonschema2Pojo$generate.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
        at org.jsonschema2pojo.gradle.GenerateJsonSchemaJavaTask.generate(GenerateJsonSchemaJavaTask.groovy:65)
joelittlejohn commented 7 years ago

@ben-manes do you have a simple schema that produces the problem?

ben-manes commented 7 years ago

Unfortunately I don't. This ran on a few dozen schemas, which have dependencies on each other. Taking it a few random ones and running it on the web tool doesn't fail. So I'm not sure how to provide a meaningful error.

joelittlejohn commented 7 years ago

Okay, no problem. It may be possible to simply understand this one using the code and your stack trace. @ctrimble might have some ideas here.

ctrimble commented 7 years ago

Thanks @ben-manes for report this. I will see if I can put together a test case to reproduce it.

ctrimble commented 7 years ago

I am having a hard time creating a test that makes this fail. @ben-manes to you have any other configuration options set?

ben-manes commented 7 years ago

Sorry that I can't be more descriptive. There's nothing confidential about the schemas, so I can probably wrap them into a project for you and email it across.

jsonSchema2Pojo {
  removeOldOutput = false
  useCommonsLang3 = true
  generateBuilders = true
  includeConstructors = true
  includeJsr303Annotations = true
  propertyWordDelimiters = ['_'] as char[]
  targetPackage = '....'
  source = files("${sourceSets.main.output.resourcesDir}/json")
}
ctrimble commented 7 years ago

The schemas would be very helpful.

ben-manes commented 7 years ago

Sent to your gmail. Thanks!

ctrimble commented 7 years ago

So, dumped all of your schemas into an integration test, with your configuration, and I can get the generated output to compile. Could this be due to removeOldOutput = false?

ben-manes commented 7 years ago

Changing that flag doesn't fix it locally. You ran it using the gradle build file? That should be reproducible unless there's a surprising environmental problem.

ctrimble commented 7 years ago

I dumped it into an IT test in maven to start. I will see what Gradle is producing.

ctrimble commented 7 years ago

I can recreate it, so this should be enough info.

ben-manes commented 5 years ago

I just ran into this again and was about to report it anew. :)

I also noticed that the feature doesn't take effect unless I enable includeDynamicAccessors and either includeDynamicGetters or includeDynamicSetters. The description isn't clear that this has to be enabled as a gate before a subsetting is evaluated, which might be more clearly worded.

ctrimble commented 5 years ago

@ben-manes I am sorry this issue was never resolved. I will see if I can dig up any work I did and throw together a PR with the schemas and test procedures to recreate the issue.

ben-manes commented 5 years ago

Oh it’s not a big deal. I will happily map by hand and that’s probably better, since I rarely need this. Just amused since I completely forgot I hit it before.

On Wed, Jun 12, 2019 at 12:02 AM Christian Trimble notifications@github.com wrote:

@ben-manes https://github.com/ben-manes I am sorry this issue was never resolved. I will see if I can dig up any work I did and throw together a PR with the schemas and test procedures to recreate the issue.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/joelittlejohn/jsonschema2pojo/issues/666?email_source=notifications&email_token=AAC4N5TVCU2K7BL2TJU4QALP2CNOTA5CNFSM4CZ7JF5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXPOLGQ#issuecomment-501147034, or mute the thread https://github.com/notifications/unsubscribe-auth/AAC4N5XWCPARVUS5GJ7FAHTP2CNOTANCNFSM4CZ7JF5A .

ctrimble commented 5 years ago

It looks like I don't have the tests mentioned in this issue.

ben-manes commented 5 years ago

Resent the original attachment, which was a failing Gradle build. I hope that helps, but you can also ignore this again as it is not blocking me.

mrkarthik commented 4 years ago

@ctrimble @joelittlejohn I am also seeing the same error, able to reproduce it with this schema. I am using the maven plugin to generate the POJOs

Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "myObj": {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string"
        },
        "bar": {
          "type": "integer"
        },
        "baz": {
          "type": "boolean"
        }
      }
    }
  },
  "properties": {
    "test": {
      "$ref": "#/definitions/myObj"
    }
  }
}

Maven Plugin:

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>1.0.2</version>
    <executions>
        <execution>
            <configuration>
                <annotationStyle>jackson2</annotationStyle>
                <generateBuilders>true</generateBuilders>
                <includeAdditionalProperties>true</includeAdditionalProperties>
                <includeConstructors>false</includeConstructors>
                <includeDynamicAccessors>true</includeDynamicAccessors>
                <includeDynamicGetters>true</includeDynamicGetters>
                <includeGetters>true</includeGetters>
                <includeHashcodeAndEquals>true</includeHashcodeAndEquals>
                <includeJsr303Annotations>true</includeJsr303Annotations>
                <includeJsr305Annotations>true</includeJsr305Annotations>
                <includeSetters>false</includeSetters>
                <includeToString>true</includeToString>
                <inclusionLevel>NON_NULL</inclusionLevel>
                <initializeCollections>false</initializeCollections>
                <useInnerClassBuilders>true</useInnerClassBuilders>
            </configuration>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
        </execution>
    </executions>
</plugin>