Repository: koxudaxi/datamodel-code-generator

Issue:
Enum models swapped with a reused model are still generated
**Describe the bug**
When using `--reuse-model`, a cached version of a similar model might be used. In these cases, for enums, the references to the original model are swapped to a reference to the cached model. However, the original models are still getting generated, even though they are not used anywhere anymore (as references to them have been swapped to the cached model).

**To Reproduce**

Example schema:
```yaml
openapi: 3.0.0
info:
  version: 0.0.1
  title: Foo API
paths:
  /foo:
    get:
      responses:
        "200":
          description: Getting foo
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Foo"
  /bar:
    get:
      responses:
        "200":
          description: Getting bar
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Bar"
components:
  schemas:
    Foo:
      properties:
        my_enum:
          type: string
          description: very interesting description
          enum:
            - VALUE_A
            - VALUE_B
            - VALUE_C
    Bar:
      properties:
        my_enum:
          type: string
          description: very interesting description
          enum:
            - VALUE_A
            - VALUE_B
            - VALUE_C
```

Used commandline:
```
$ datamodel-codegen --input duplicate-enums.yaml --output duplicate-enums.py --reuse-model
```

This will generate the following file:
```python
class MyEnum(Enum):
    VALUE_A = 'VALUE_A'
    VALUE_B = 'VALUE_B'
    VALUE_C = 'VALUE_C'

class Foo(BaseModel):
    my_enum: Optional[MyEnum] = Field(None, description='very interesting description')

class MyEnum1(Enum):
    VALUE_A = 'VALUE_A'
    VALUE_B = 'VALUE_B'
    VALUE_C = 'VALUE_C'

class Bar(Foo):
    pass
```

**Expected behavior**
Enum models that have been swapped by a model that is reused should not be generated, as they are not used anymore.

The output should look like this:
```python
class MyEnum(Enum):
    VALUE_A = 'VALUE_A'
    VALUE_B = 'VALUE_B'
    VALUE_C = 'VALUE_C'

class Foo(BaseModel):
    my_enum: Optional[MyEnum] = Field(None, description='very interesting description')

class Bar(Foo):
    pass
```

**Version:**
 - OS: Gentoo
 - Python version: 3.7.10, 3.8.9
 - datamodel-code-generator version: 0.11.5

Let me know if this isn't clear enough


Discussion:

Resolving patch:
diff --git a/datamodel_code_generator/parser/base.py b/datamodel_code_generator/parser/base.py
--- a/datamodel_code_generator/parser/base.py
+++ b/datamodel_code_generator/parser/base.py
@@ -474,7 +474,6 @@ def parse(
                         model.reference.name = generated_name
 
         for module, models in module_models:
-
             init = False
             if module:
                 parent = (*module[:-1], '__init__.py')
@@ -525,6 +524,7 @@ def parse(
 
             if self.reuse_model:
                 model_cache: Dict[Tuple[str, ...], Reference] = {}
+                duplicates = []
                 for model in models:
                     model_key = tuple(
                         to_hashable(v)
@@ -540,10 +540,10 @@ def parse(
                             for child in model.reference.children[:]:
                                 # child is resolved data_type by reference
                                 data_model = get_most_of_parent(child)
-
                                 # TODO: replace reference in all modules
                                 if data_model in models:  # pragma: no cover
                                     child.replace_reference(cached_model_reference)
+                            duplicates.append(model)
                         else:
                             index = models.index(model)
                             inherited_model = model.__class__(
@@ -568,6 +568,9 @@ def parse(
                     else:
                         model_cache[model_key] = model.reference
 
+                for duplicate in duplicates:
+                    models.remove(duplicate)
+
             if self.set_default_enum_member:
                 for model in models:
                     for model_field in model.fields:


Test patch: