Skip to content

Commit

Permalink
JSON Binding use fields local name
Browse files Browse the repository at this point in the history
Closes #389
  • Loading branch information
tefra committed Jan 20, 2021
1 parent 2fead20 commit ce5ef22
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion tests/fixtures/defxmlschema/chapter01.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"number": 557,
"size": 10,
"eff_date": "2001-04-02"
"effDate": "2001-04-02"
}
10 changes: 5 additions & 5 deletions tests/fixtures/defxmlschema/chapter08.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"dress_size": [
"dressSize": [
"06"
],
"medium_dress_size": [
"mediumDressSize": [
"12"
],
"small_dress_size": [
"smallDressSize": [
"6"
],
"smlx_size": [
"smlxSize": [
"extra large"
],
"xsmlx_size": [
"xsmlxSize": [
"extra small"
]
}
8 changes: 4 additions & 4 deletions tests/fixtures/defxmlschema/chapter10.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
12,
"medium"
],
"small_size": [
"smallSize": [
"6",
"small"
],
"international_size": [
"internationalSize": [
12,
24,
"large"
],
"available_sizes": [
"availableSizes": [
[
10,
"large",
2
]
],
"applicable_sizes": [
"applicableSizes": [
"small medium large"
]
}
4 changes: 2 additions & 2 deletions tests/fixtures/defxmlschema/chapter12.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
]
}
],
"eff_date": "2002-04-02",
"effDate": "2002-04-02",
"other_attributes": {}
},
{
Expand All @@ -44,7 +44,7 @@
"value": "red"
}
],
"eff_date": "1900-01-01",
"effDate": "1900-01-01",
"other_attributes": {
"{http://example.org/oth}custom": "12"
}
Expand Down
12 changes: 6 additions & 6 deletions tests/fixtures/defxmlschema/chapter13.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"hat": [
{
"routing_num": 123456,
"routingNum": 123456,
"number": 557,
"name": "Ten-Gallon Hat",
"description": "This is a great hat!",
"eff_date": "2002-04-02",
"effDate": "2002-04-02",
"lang": "en-US"
}
],
Expand All @@ -14,19 +14,19 @@
"number": 557,
"name": "Ten-Gallon Hat",
"description": null,
"routing_num": 123,
"routingNum": 123,
"lang": null,
"eff_date": "2002-04-02"
"effDate": "2002-04-02"
}
],
"shirt": [
{
"number": 557,
"name": "Short-Sleeved Linen Blouse",
"description": null,
"routing_num": 124,
"routingNum": 124,
"lang": null,
"eff_date": "2002-04-02",
"effDate": "2002-04-02",
"size": [
{
"value": 6,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/defxmlschema/chapter15.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
},
"id": "P557",
"version": "100",
"eff_date": null
"effDate": null
}
22 changes: 11 additions & 11 deletions tests/fixtures/primer/order.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"ship_to": {
"shipTo": {
"name": "Alice Smith",
"street": "123 Maple Street",
"city": "Mill Valley",
"state": "CA",
"zip": "90952",
"country": "US"
},
"bill_to": {
"billTo": {
"name": "Robert Smith",
"street": "8 Oak Avenue",
"city": "Old Town",
Expand All @@ -19,22 +19,22 @@
"items": {
"item": [
{
"product_name": "Lawnmower",
"productName": "Lawnmower",
"quantity": 1,
"usprice": "148.95",
"USPrice": "148.95",
"comment": "Confirm this is electric",
"ship_date": null,
"part_num": "872-AA"
"shipDate": null,
"partNum": "872-AA"
},
{
"product_name": "Baby Monitor",
"productName": "Baby Monitor",
"quantity": 1,
"usprice": "39.98",
"USPrice": "39.98",
"comment": null,
"ship_date": "1999-05-21",
"part_num": "926-AA"
"shipDate": "1999-05-21",
"partNum": "926-AA"
}
]
},
"order_date": "1999-10-20"
"orderDate": "1999-10-20"
}
2 changes: 1 addition & 1 deletion tests/formats/dataclass/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_send_with_dict_params(self, mock_post):
mock_post.return_value = response.encode()

client = Client.from_service(CalculatorSoapAdd)
params = {"body": {"add": {"int_a": 3, "int_b": 4}}}
params = {"Body": {"Add": {"intA": 3, "intB": 4}}}

result = client.send(params, headers={"User-Agent": "xsdata"})

Expand Down
4 changes: 4 additions & 0 deletions tests/formats/dataclass/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ def test_init_with_xml_type(self):

self.assertEqual("Unknown xml type `xsdata`", str(cm.exception))

def test_property_lname(self):
var = XmlVar(name="a", qname="{B}A")
self.assertEqual("A", var.lname)

def test_property_clazz(self):
var = XmlVar(name="foo", qname="foo")
self.assertIsNone(var.clazz)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_client(self, mock_most):
config = Config.from_service(CalculatorSoapAdd)
serializer = XmlSerializer(config=SerializerConfig(pretty_print=True))
client = Client(config=config, serializer=serializer)
result = client.send({"body": {"add": {"int_a": 1, "int_b": 3}}})
result = client.send({"Body": {"Add": {"intA": 1, "intB": 3}}})

self.assertIsInstance(result, CalculatorSoapAddOutput)

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_hello_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_client(self, mock_most):
config = Config.from_service(HelloGetHelloAsString)
serializer = XmlSerializer(config=SerializerConfig(pretty_print=True))
client = Client(config=config, serializer=serializer)
result = client.send({"body": {"get_hello_as_string": {"arg0": "chris"}}})
result = client.send({"Body": {"getHelloAsString": {"arg0": "chris"}}})

self.assertIsInstance(result, HelloGetHelloAsString.output)

Expand All @@ -70,7 +70,7 @@ def test_client_with_soap_fault(self, mock_most):
config = Config.from_service(HelloGetHelloAsString)
serializer = XmlSerializer(config=SerializerConfig(pretty_print=True))
client = Client(config=config, serializer=serializer)
result = client.send({"body": {"get_hello_as_string": {"arg0": "chris"}}})
result = client.send({"Body": {"getHelloAsString": {"arg0": "chris"}}})

self.assertIsInstance(result, HelloGetHelloAsString.output)

Expand All @@ -90,5 +90,5 @@ def test_live(self):
config = Config.from_service(HelloGetHelloAsString)
serializer = XmlSerializer(config=SerializerConfig(pretty_print=True))
client = Client(config=config, serializer=serializer)
result = client.send({"body": {"get_hello_as_string": {"arg0": "chris"}}})
result = client.send({"Body": {"getHelloAsString": {"arg0": "chris"}}})
print(result)
6 changes: 6 additions & 0 deletions xsdata/formats/dataclass/models/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def __post_init__(self, xml_type: Optional[str]):
elif xml_type:
raise XmlContextError(f"Unknown xml type `{xml_type}`")

@property
def lname(self) -> str:
"""Local name."""
_, name = split_qname(self.qname)
return name

@property
def clazz(self) -> Optional[Type]:
"""Return the first type if field is bound to a dataclass."""
Expand Down
6 changes: 3 additions & 3 deletions xsdata/formats/dataclass/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import warnings
from dataclasses import dataclass
from dataclasses import field
from dataclasses import fields
from dataclasses import is_dataclass
from typing import Any
from typing import Dict
Expand Down Expand Up @@ -79,7 +78,7 @@ def bind_dataclass(self, data: Dict, clazz: Type[T]) -> T:
"""Recursively build the given model from the input dict data."""
params = {}
for var in self.context.build(clazz).vars:
value = data.get(var.name)
value = data.get(var.lname)

if value is None or not var.init:
continue
Expand Down Expand Up @@ -181,7 +180,8 @@ def bind_choice_dataclass(self, value: Dict, var: XmlVar) -> Any:
keys = set(value.keys())
for choice in var.choices:
if choice.clazz:
attrs = {f.name for f in fields(choice.clazz)}
meta = self.context.build(choice.clazz)
attrs = {var.lname for var in meta.vars}
if attrs == keys:
return self.bind_value(choice, value)

Expand Down
2 changes: 1 addition & 1 deletion xsdata/formats/dataclass/serializers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def convert(self, obj: Any, var: Optional[XmlVar] = None) -> Any:
if var is None or is_dataclass(obj):
return self.dict_factory(
[
(var.name, self.convert(getattr(obj, var.name), var))
(var.lname, self.convert(getattr(obj, var.name), var))
for var in self.context.build(obj.__class__).vars
]
)
Expand Down

0 comments on commit ce5ef22

Please sign in to comment.