Generate enum class as an inner class when defined in a model
See original GitHub issueWhen an enum is defined as part of a model, the enum class generated is a standalone class.
For example, for
Model:
...
properties:
enumDefinition:
type: ...
enum:
- x
- y
an enum class named EnumDefinition
would be generated.
This would result in generation of enum classes like EnumDefinition
, EnumDefinition1
, EnumDefinition2
etc. when the same enum property name is defined across different models.
Instead of this, would it be possible to generate an enum class as an inner class of the model class, in which they are defined as a property?
For the example above, each model with enumDefinition
property would have its own inner class EnumDefinition
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Why can't I create an enum in an inner class in Java?
Eclipse gives me this compile error on StateType : The member enum StateType must be defined inside a static member type . The...
Read more >Can we define an enum inside a class in Java? - Tutorialspoint
Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.
Read more >$ref to enum creates inner class instead of using the other class
If MyEnum.json is read first, you would get a standalone type generated, and this would then be used when Base is generated. In...
Read more >Enum and inner class - Oracle Communities
Hi, Another experiment of mine ;) enum Go { ME(new Test("Test")); class Test { String s; Test(String s) { this.s=s; } } Go(Test...
Read more >Java Object Oriented Design - Java Enum Types - Java2s.com
The constants defined in an enum type are instances of that enum type. ... We can have a nested enum type declaration inside...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@koxudaxi Yes,
--use-title-as-name
is indeed quite helpful for my use case. Thanks!@cansukaynak
I think we should not use the inner class for the pydantic model. Also, the rule makes it complex to generate models.
I added a new feature to use a title as a model name. https://github.com/koxudaxi/datamodel-code-generator/pull/510
the PR is not released yet. but, if you can use the feature then you can control the enum name by the title. What do you think about it?