question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate enum class as an inner class when defined in a model

See original GitHub issue

When 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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
cansukaynakcommented, Sep 3, 2021

@koxudaxi Yes, --use-title-as-name is indeed quite helpful for my use case. Thanks!

0reactions
koxudaxicommented, Aug 26, 2021

@cansukaynak

With the inner class option, I would like to use the same enum name, but a different set of values in each enum definition.

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found