-
Notifications
You must be signed in to change notification settings - Fork 49
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
Enum names are not preserved #30
Enum names are not preserved #30
Comments
This is most likely a duplicate of #15 |
+1. This would be nice considering I'm using enum names in backend and I would prefer not to use just the enum values in my frontend (which are nearly meaningless without the names). |
@robbieaverill I don't think this is a duplicate of #15 Currently it works like this, it converts:
To:
Which means we can't use
We could use |
Apparently this is possible starting from json-schema-to-typescript v14.1+. Just add the |
I'm adding my solution to this problem as well, since I also needed enum keys to be persisted during the generation: Using the class A(enum.Enum):
A = 'value_a'
B = 'value_b' Then generates export enum A {
value_a = 'value_a'
value_b = 'value_b'
} But I needed the following export enum A {
A = 'value_a'
B = 'value_b'
} So I threw together a short script to find and replace those enums in the generated file. Note that this only works without the import enum
import importlib
import os
import inspect
import re
from pydantic2ts import generate_typescript_defs
# Module of the file containing schema to generate in TS
schema_module = 'api.src.schemas'
# Output TS file path
models_file_path = './frontend/src/models.d.ts'
# Directory containing all Python schemas
schemas_dir = './api/src'
# Module containing all Python schemas
schemas_module = 'api.src'
generate_typescript_defs(schema_module, models_file_path, (),
'json2ts --enableConstEnums false '
'--unreachableDefinitions true --strictIndexSignatures true')
enums = {}
for file_name in os.listdir(schemas_dir):
if file_name.endswith(".py") and file_name != "__init__.py":
module_name = file_name[:-3] # Remove the '.py' extension
module = importlib.import_module(f'{schemas_module}.{module_name}')
# Find all Enum subclasses in the module
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj) and issubclass(obj, enum.Enum):
enums[name] = obj
with open(models_file_path, 'r') as models_file:
models = models_file.read()
for name, obj in enums.items():
new_enum_values = ",\n ".join([f'{member.name} = "{member.value}"' for member in obj])
new_enum = f'export enum {name} {{\n {new_enum_values}\n}}'
models = re.sub(rf'export type {name} [^;]*;', new_enum, models, flags=re.S | re.M)
with open(models_file_path, 'w') as models_file:
models_file.write(models) |
Enums which are part of a pydantic model are translated to a type, losing relevant and useful information that could be kept if they were translated to enums in typescript too
The text was updated successfully, but these errors were encountered: