You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After looking deeper into the code while providing an alias mapping dictionary like so: { 'model_field_name': 'field_alias_name' }
I do not get the correct model outputs, It will only add the alias property to the model if the model_field_name and the field_alias_name values match.
I found this function that seems to handle the field_name and alias parameters while parsing the json objects:
def parse_object_fields(
self, obj: JsonSchemaObject, path: List[str], module_name: Optional[str] = None
) -> List[DataModelFieldBase]:
properties: Dict[str, Union[JsonSchemaObject, bool]] = (
{} if obj.properties is None else obj.properties
)
requires: Set[str] = {*()} if obj.required is None else {*obj.required}
fields: List[DataModelFieldBase] = []
exclude_field_names: Set[str] = set()
for original_field_name, field in properties.items():
field_name, alias = self.model_resolver.get_valid_field_name_and_alias(
original_field_name, exclude_field_names
)
The values seem to be set in reverse, The alias is set to the field_name and the field_name is what the alias should be.
So with the context above, Am I using the library incorrectly when attempting to add the alias mapping dictionary?
I have tried reversing the key and value when providing the mapping data and in that case or the generation errors out and tries to make the alias the field name in the generated Pydantic model class.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
After looking deeper into the code while providing an alias mapping dictionary like so:
{ 'model_field_name': 'field_alias_name' }
I do not get the correct model outputs, It will only add the alias property to the model if the
model_field_name
and thefield_alias_name
values match.I found this function that seems to handle the field_name and alias parameters while parsing the json objects:
Function Class:
FieldNameResolver
Function file path: Line 271 -> #https://github.com/koxudaxi/datamodel-code-generator/blob/main/datamodel_code_generator/reference.py
It first checks to see if the
field_name
is a key in the objectsaliases
variable, and if so returns a tuple like so:return self.aliases[field_name], field_name
This implies that the tuple contains the alias from the mapping alias dictionary, and the field_name checked in the function.
So when the above function is called here:
File Path: Line 896 -> #https://github.com/koxudaxi/datamodel-code-generator/blob/main/datamodel_code_generator/parser/jsonschema.py
The values seem to be set in reverse, The alias is set to the field_name and the field_name is what the alias should be.
So with the context above, Am I using the library incorrectly when attempting to add the alias mapping dictionary?
I have tried reversing the key and value when providing the mapping data and in that case or the generation errors out and tries to make the alias the field name in the generated Pydantic model class.
Beta Was this translation helpful? Give feedback.
All reactions