Client and server binding loses the model --reuse-model replaced when it was moved into an _internal module

Open
#5,816 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
tooling

Research direction

Inspect the _target_binding._Binder.reused method and the DataModel.reused_reference attribute in the binder source. Run the fixtures modular.yaml, circular_imports_class_conflict.yaml, reuse_model_cross_module_users.yaml and nullable_map_value_cycle_reuse.yaml (client and server) to observe FinalModelSymbol.discriminator becoming None. Fix by reading the replaced model from DataModel.reused_reference instead of the /reuse suffix, then verify discriminator is set.

Written by the indexing model from the issue text.

Description

bug

Summary

With --reuse-model, client and server binding loses the model that reuse replaced when the replaced model was first moved into an _internal module.

Details

A duplicate model inside a module cycle is moved to _internal before reuse, so its subclass's path becomes _internal.X/reuse. _target_binding._Binder.reused strips the /reuse suffix and looks the path up among the resolver keys, which keep the schema path, so it finds nothing, while DataModel.reused_reference names the model.

This happens in 28 binder calls across the existing fixtures modular.yaml, circular_imports_class_conflict.yaml, reuse_model_cross_module_users.yaml and nullable_map_value_cycle_reuse.yaml (client and server).

Effect

FinalModelSymbol.discriminator (the replaced union's own discriminator) is None for such a model. No generated output difference has been found: in the fixtures the replaced models are not unions, and a constructed discriminated union reused inside an _internal cycle has the same discriminator as its canonical model.

Expected

The binding reads the model reuse replaced from DataModel.reused_reference, never from the /reuse path suffix.

Dominant language
Python
Stars
4k
Forks
460
Avg merge
7h 27m
Merged PRs (30d)
910

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from datamodel-code-generator/datamodel-code-generator

All issues in datamodel-code-generator/datamodel-code-generator

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.