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

Support direct class inheritance with 'extendsJavaType', without using 'extends' #402

Closed
ahanin opened this issue Aug 20, 2015 · 8 comments
Milestone

Comments

@ahanin
Copy link
Contributor

ahanin commented Aug 20, 2015

I found javaInterfaces rule useful, but, unfortunately, it doesn't meet all my needs. When generating POJOs I sometimes want them to inherit already existing class, that is not generated from schema, therefore extends is not enough.

The solution to this could be a new extendsJavaClass rule to compliment javaInterfaces, which would take a java type to extends. Generics should be supported too. This way, another feature, is maybe required - reference a class being generated. extends and extendsJavaClass must be mutually exclusive.

Example (article.schema):

{ "type": "object",
  "extendsJavaClass": "tld.example.Pageable<${this}>" }

${this} (or any other placeholder) would have to be replace by the actual class name being generated, e.g.:

public class ArticlePageable extends Pageable<Article> {
  // ...
}
ahanin pushed a commit to ahanin/jsonschema2pojo that referenced this issue Aug 20, 2015
@ahanin
Copy link
Contributor Author

ahanin commented Aug 20, 2015

The proposed change is reflected in a pull request: #403

ahanin pushed a commit to ahanin/jsonschema2pojo that referenced this issue Aug 21, 2015
joelittlejohn added a commit that referenced this issue Aug 21, 2015
#402 support of existing java class inheritance
@joelittlejohn joelittlejohn changed the title Support class inheritance with 'extendsJavaType' Support direct class inheritance with 'extendsJavaType' Aug 21, 2015
@joelittlejohn joelittlejohn changed the title Support direct class inheritance with 'extendsJavaType' Support direct class inheritance with 'extendsJavaType', without using 'extends' Aug 21, 2015
@joelittlejohn joelittlejohn added this to the 0.4.15 milestone Aug 21, 2015
@brentryan
Copy link

You can use extends with a javaType field and it works fine with existing classes. Did you try this?

@ahanin
Copy link
Contributor Author

ahanin commented Aug 26, 2015

I tried with the latest snapshot and it didn't work for the type. Can you
give an example?
On Aug 25, 2015 11:36 PM, "brentryan" [email protected] wrote:

You can use extends with a javaType field and it works fine with existing
classes. Did you try this?


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

@joelittlejohn
Copy link
Owner

@ahanin an example of what @brentryan is talking about would be:

{
  "type" : "object",
  "extends" : {
    "type" : "object",
    "javaType" : "com.example.MyExistingType"
  }
}

It's a bit of a workaround and is a bit more intrusive in the schema than you were hoping. I was happy to merge as people have asked about this kind of feature in the past, and it's possible to implement this as a nicely isolated extension property.

@ahanin
Copy link
Contributor Author

ahanin commented Aug 26, 2015

Ah, nice. Good to know. I am perhaps documentation blind :)
On Aug 26, 2015 5:07 PM, "Joe Littlejohn" [email protected] wrote:

@ahanin https://github.com/ahanin an example of what @brentryan
https://github.com/brentryan is talking about would be:

{
"type" : "object",
"extends" : {
"type" : "object",
"javaType" : "com.example.MyExistingType"
}
}

It's a bit of a workaround and is a bit more intrusive in the schema than
you were hoping. I was happy to merge as people have asked about this kind
of feature in the past, and it's possible to implement this as a nicely
isolated extension property.


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

@anhquande
Copy link

anhquande commented Mar 2, 2017

It works only when using maven jsonschema2pojo:generate.
But when i use "maven clean install", the existing class is changed (generated) by the plugin. That is what it is not supposed to do.

Updated: adding phase in the pom.xml will solve the problem

                <executions>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>
                            compile
                        </phase>
                    </execution>
                </executions>```

@ahanin
Copy link
Contributor Author

ahanin commented Mar 2, 2017

@anhquande would you create a relevant issue?

@evonsdesigns
Copy link

If you reference a class from one of the dependencies, like where the custom annotator would live, you don't need to add phase.

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

No branches or pull requests

5 participants