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

Extra class file generated when using javaType #365

Closed
zambro opened this issue May 28, 2015 · 2 comments
Closed

Extra class file generated when using javaType #365

zambro opened this issue May 28, 2015 · 2 comments

Comments

@zambro
Copy link

zambro commented May 28, 2015

As an extension of the closed issue #361, I tried using javaType in my parent class Device.json. This worked as expected in creating the Device.java and Spec.java file. But the class files are messed up.

I got

target/java-gen/mypackage
-->Device.java (Has a class variable 'components' of type 'Spec')
-->Spec.java

target/classes/mypackage
-->Device.class
-->Spec.class
-->Spec_.class (This is not referenced in any class)

My Device.json:

  {
      "$schema": "http://json-schema.org/draft-03/hyper-schema",
      "additionalProperties": false,
   "id": "device:v1",
   "name": "device",
   "properties": {
       "components": {
           "javaType": "mypackage.Spec"
           },
       "usage": {
           "javaType": "mypackage.Spec"
           }
   }
  },
  "required": true,
  "title": "Device",
  "type": "object"
  }

Initially I didn't mind the extra Spec_.class, but for some weird reason I am not able to use the getter and setter in the Spec.class (Although there are public getter and setters in Spec and Spec_). However I am able to use getter and setter in Spec_.class. Now I am wondering if Spec.class was not generated properly.

This problem is only in the class file generated and not in the Device.java and Spec.java files.

Is this a bug or am I missing something in reference to javaType?

@joelittlejohn
Copy link
Owner

javaType can be used to reuse existing classes from the classpath, but in this case you're instructing the plugin to generate two new classes but have the same FQCN for both. The plugin can't do this, so it attempts to disambiguate the names using an underscore.

What should be done here is:

device.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "device:v1",
    "name": "device",
    "properties": {
        "components": {
            "$ref": "spec.json"
            },
        "usage": {
            "$ref": "spec.json"
            }
    }
}

spec.json

{
    "javaType" : "mypackage.Spec",
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "spec:v1",
    "name": "spec",
    "properties": {
        "content": {
            "description" : "Content",
            "type": "string",
            "required": false
       }
    },
    "required": true,
    "title": "spec",
    "type": "object"
}

Now we have two properties on device, both referring to the same type, and the type they are referring to has a single, universal FQCN.

@zambro
Copy link
Author

zambro commented May 28, 2015

@joelittlejohn Thank you for your fast answer and detailed explanation.

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

No branches or pull requests

2 participants