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

Allow schema title to be used as class name (new useTitleAsClassname config option) #908

Closed
jrehwaldt opened this issue Sep 7, 2018 · 6 comments
Milestone

Comments

@jrehwaldt
Copy link
Contributor

jrehwaldt commented Sep 7, 2018

When having schemas where certain field names are repeated with different implementations the code currently generates unique class names such as Image__1, etc. I would like to have a configuration to use the title as class name instead.

So that

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "SchemaTest body",
  "description": "Use title as class name.",
  "type": "object",
  "properties": {
    "image": {
      "type": "object",
      "title": "MyImage"
    }
  }
}

would result in

public class SchemaTest {

    private MyImage image;

    public SchemaTest() {
    }

    public SchemaTest(MyImage image) {
        super();
        this.image = image;
    }

    public MyImage getImage() {
        return image;
    }
}
public class MyImage{
}

Thank you for your great library. I am using it successfully and really like the RuleFactory design.

@joelittlejohn
Copy link
Owner

This has been asked for before. Interesting that in your design you expect "SchemaTest body" to become SchemaTest. Was that a typo or do you expect jsonschema2pojo to do some transformation/sanitization here?

@jrehwaldt
Copy link
Contributor Author

In my implementation I only use the title for sub-hierarchies and stick with the filename otherwise. Not a deal breaker for me in general.

I would capitalize all first letters and strip spaces. Next illegal characters get replaced with underscore and that’s it.

@jrehwaldt
Copy link
Contributor Author

I am willing to contribute PRs for all my requests if there’s a chance for approval.

@joelittlejohn
Copy link
Owner

Happy to accept a PR for this, as long as it is behind a configuration option, say, useTitleAsClassname. The default must be false.

jrehwaldt added a commit to jrehwaldt/jsonschema2pojo that referenced this issue Sep 14, 2018
Each word in a title is capitalized and whitespaces stripped.

Fixes jsonschema2pojo/joelittlejohngh-908
@jrehwaldt
Copy link
Contributor Author

Just saw your comment now. If you prefer a boolean property I can change it.

I implemented it as part of NameHelper for now. It seeemed the best place to me as it also works for enums this way.

jrehwaldt added a commit to jrehwaldt/jsonschema2pojo that referenced this issue Sep 14, 2018
Each word in a title is capitalized and whitespaces stripped.

Fixes jsonschema2pojo/joelittlejohngh-908
jrehwaldt added a commit to jrehwaldt/jsonschema2pojo that referenced this issue Sep 14, 2018
Each word in a title is capitalized and whitespaces stripped.

Fixes jsonschema2pojo/joelittlejohngh-908
@joelittlejohn joelittlejohn added this to the 1.0.0-beta1 milestone Sep 17, 2018
@joelittlejohn joelittlejohn changed the title Allow schema title to be used as class name Allow schema title to be used as class name (new useTitleAsClassname config option) Sep 17, 2018
@jrehwaldt
Copy link
Contributor Author

Thank you very much! It was fun working on this one.

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