|
6 | 6 | from pydantic_xml.element import XmlElementReader, XmlElementWriter |
7 | 7 | from pydantic_xml.serializers.serializer import SearchMode, Serializer, encode_primitive |
8 | 8 | from pydantic_xml.typedefs import EntityLocation, NsMap |
9 | | -from pydantic_xml.utils import QName, merge_nsmaps |
| 9 | +from pydantic_xml.utils import QName, merge_nsmaps, select_ns |
10 | 10 |
|
11 | 11 | PrimitiveTypeSchema = Union[ |
12 | 12 | pcs.NoneSchema, |
@@ -67,10 +67,16 @@ class AttributeSerializer(Serializer): |
67 | 67 | def from_core_schema(cls, schema: PrimitiveTypeSchema, ctx: Serializer.Context) -> 'AttributeSerializer': |
68 | 68 | namespaced_attrs = ctx.namespaced_attrs |
69 | 69 | name = ctx.entity_path or ctx.field_alias or ctx.field_name |
70 | | - ns = ctx.entity_ns or (ctx.parent_ns if namespaced_attrs else None) |
| 70 | + ns = select_ns(ctx.entity_ns, ctx.parent_ns if namespaced_attrs else None) |
71 | 71 | nsmap = merge_nsmaps(ctx.entity_nsmap, ctx.parent_nsmap) |
72 | 72 | computed = ctx.field_computed |
73 | 73 |
|
| 74 | + if ns == '': |
| 75 | + raise errors.ModelFieldError( |
| 76 | + ctx.model_name, |
| 77 | + ctx.field_name, |
| 78 | + "attributes with default namespace are forbidden", |
| 79 | + ) |
74 | 80 | if name is None: |
75 | 81 | raise errors.ModelFieldError(ctx.model_name, ctx.field_name, "entity name is not provided") |
76 | 82 |
|
@@ -113,7 +119,7 @@ class ElementSerializer(TextSerializer): |
113 | 119 | @classmethod |
114 | 120 | def from_core_schema(cls, schema: PrimitiveTypeSchema, ctx: Serializer.Context) -> 'ElementSerializer': |
115 | 121 | name = ctx.entity_path or ctx.field_alias or ctx.field_name |
116 | | - ns = ctx.entity_ns or ctx.parent_ns |
| 122 | + ns = select_ns(ctx.entity_ns, ctx.parent_ns) |
117 | 123 | nsmap = merge_nsmaps(ctx.entity_nsmap, ctx.parent_nsmap) |
118 | 124 | search_mode = ctx.search_mode |
119 | 125 | computed = ctx.field_computed |
|
0 commit comments