Repository: marcosschroh/dataclasses-avroschema

Issue:
Nested metadata not respected
**Describe the bug**
If I override a `schema_name` attribute for a class that's used as a field, that schema_name isn't respected.

**To Reproduce**
```python
from dataclasses_avroschema import AvroModel
from dataclasses import dataclass

@dataclass
class MyClass(AvroModel):
    field_1: str
    class Meta:
        schema_name = "custom_class" # <-- this is not respected
class MySecondClass(AvroModel):
    field_2: MyClass
    class Meta:
        schema_name = "custom_name"
        
MySecondClass.avro_schema_to_python()
```

This outputs

```python
{'type': 'record',
 'name': 'custom_name',
 'fields': [{'name': 'field_2',
   'type': {'type': 'record',
    'name': 'MyClass', # <-- this line is wrong
    'fields': [{'name': 'field_1', 'type': 'string'}],}}],
}
```

**Expected behavior**

I would expect
```python
{'type': 'record',
 'name': 'custom_name',
 'fields': [{'name': 'field_2',
   'type': {'type': 'record',
    'name': 'custom_class', # This is the important line
    'fields': [{'name': 'field_1', 'type': 'string'}],}}],
}
```


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh) Report
> Merging [#172](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh) (a39e8ad) into [master](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/commit/d88afb8df16039cf86fc0598e2d4c0431613987a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh) (d88afb8) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master     #172   +/-   ##
=======================================
  Coverage   99.65%   99.65%           
=======================================
  Files          10       10           
  Lines         869      869           
  Branches      151      151           
=======================================
  Hits          866      866           
  Misses          2        2           
  Partials        1        1           
```


| [Impacted Files](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh) | Coverage Δ | |
|---|---|---|
| [dataclasses\_avroschema/fields.py](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh#diff-ZGF0YWNsYXNzZXNfYXZyb3NjaGVtYS9maWVsZHMucHk=) | `99.56% <100.00%> (ø)` | |
| [dataclasses\_avroschema/utils.py](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh#diff-ZGF0YWNsYXNzZXNfYXZyb3NjaGVtYS91dGlscy5weQ==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh). Last update [d88afb8...a39e8ad](https://codecov.io/gh/marcosschroh/dataclasses-avroschema/pull/172?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Marcos+Schroh).


Patch:
diff --git a/dataclasses_avroschema/fields.py b/dataclasses_avroschema/fields.py
index bb973af..8ec1397 100644
--- a/dataclasses_avroschema/fields.py
+++ b/dataclasses_avroschema/fields.py
@@ -685,8 +685,16 @@ class UUIDField(LogicalTypeField):
 @dataclasses.dataclass
 class RecordField(BaseField):
     def get_avro_type(self) -> typing.Union[typing.List, typing.Dict]:
-        alias = self.parent.metadata.get_alias(self.name) or self.model_metadata.get_alias(self.name)  # type: ignore
-        name = alias or self.type.__name__
+        meta = getattr(self.type, "Meta", None)
+        metadata = utils.SchemaMetadata.create(meta)
+
+        alias = self.parent.metadata.get_alias_nested_items(self.name) or metadata.get_alias_nested_items(self.name)  # type: ignore  # noqa E501
+
+        # The priority for the schema name
+        # 1. Check if the schema_name is present in the Meta class of own model
+        # 2. Check if exists an alias_nested_items in parent class or Meta class of own model
+        # 3. Use the default class Name (self.type.__name__)
+        name = metadata.schema_name or alias or self.type.__name__
 
         if not self.exist_type():
             user_defined_type = utils.UserDefinedType(name=name, type=self.type)
@@ -697,9 +705,6 @@ class RecordField(BaseField):
             record_type = self.type.avro_schema_to_python(root=self.parent)
             record_type["name"] = name
         else:
-            meta = getattr(self.type, "Meta", None)
-            metadata = utils.SchemaMetadata.create(meta)
-
             if metadata.namespace is None:
                 raise NameSpaceRequiredException(field_type=self.type, field_name=self.name)
             record_type = f"{metadata.namespace}.{name}"
diff --git a/dataclasses_avroschema/utils.py b/dataclasses_avroschema/utils.py
index 606cb63..0d3722f 100644
--- a/dataclasses_avroschema/utils.py
+++ b/dataclasses_avroschema/utils.py
@@ -90,7 +90,7 @@ class SchemaMetadata:
             alias_nested_items=getattr(klass, "alias_nested_items", {}),
         )
 
-    def get_alias(self, name: str) -> typing.Optional[str]:
+    def get_alias_nested_items(self, name: str) -> typing.Optional[str]:
         return self.alias_nested_items.get(name)
 
 


Test patch:
diff --git a/tests/schemas/test_schema.py b/tests/schemas/test_schema.py
index b9252ee..d1f6c20 100644
--- a/tests/schemas/test_schema.py
+++ b/tests/schemas/test_schema.py
@@ -151,6 +151,24 @@ def test_get_fields():
     assert Parent.fake()
 
 
+def test_schema_name_from_relationship():
+    @dataclass
+    class MyClass(AvroModel):
+        field_1: str
+
+        class Meta:
+            schema_name = "custom_class"
+
+    class MySecondClass(AvroModel):
+        field_2: MyClass
+
+        class Meta:
+            schema_name = "custom_name"
+
+    schema = MySecondClass.avro_schema_to_python()
+    assert schema["fields"][0]["type"]["name"] == "custom_class"
+
+
 def test_validate():
     @dataclass
     class User(AvroModel):