Skip to content

Commit 579f5c9

Browse files
Fix/nested schema fields (#102)
* Fix generating schema's for types with array properties When the type of the array is a built-in type we should generate a `fields.List()` with a regular field as argument instead of referencing a non-existing schema. * Regenerate * Fix isort on project Should fix in codegen/main.py#105 since it doesn't seem to do isort... Co-authored-by: David Weterings <d.weterings@labdigital.nl>
1 parent ee27768 commit 579f5c9

File tree

7 files changed

+27
-69
lines changed

7 files changed

+27
-69
lines changed

codegen/generate_schema.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,25 @@ def _get_property_field(self, prop):
370370
if prop.type.name == "array":
371371
assert prop.items, f"The array property {prop.name} has no items"
372372
assert prop.items_types
373+
item_type = prop.items_types[0]
373374

374375
# TODO: We for now assume that the items are all subclasses of
375376
# the first item. We shouldn't do that :-)
376-
if prop.items_types[0].discriminator:
377-
node.args.append(
378-
self._create_discriminator_field(prop.items_types[0])
377+
if item_type.discriminator:
378+
field = self._create_discriminator_field(item_type)
379+
elif item_type.name in FIELD_TYPES:
380+
field = ast.Call(
381+
func=ast.Name(id=FIELD_TYPES[item_type.name]),
382+
args=[],
383+
keywords=[
384+
ast.keyword(
385+
arg="allow_none", value=ast.NameConstant(True, kind=None)
386+
)
387+
],
379388
)
380389
else:
381-
node.args.append(self._create_nested_field(prop.items_types[0]))
390+
field = self._create_nested_field(item_type)
391+
node.args.append(field)
382392
return node
383393

384394
# Dict Field

src/commercetools/_schemas/_common.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,12 +769,7 @@ class GeoJsonPointSchema(GeoJsonSchema):
769769
"""Marshmallow schema for :class:`commercetools.types.GeoJsonPoint`."""
770770

771771
coordinates = marshmallow.fields.List(
772-
helpers.LazyNestedField(
773-
nested="commercetools._schemas.None.numberSchema",
774-
unknown=marshmallow.EXCLUDE,
775-
allow_none=True,
776-
),
777-
allow_none=True,
772+
marshmallow.fields.Integer(allow_none=True), allow_none=True
778773
)
779774

780775
class Meta:

src/commercetools/_schemas/_discount_code.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,7 @@ class DiscountCodeDraftSchema(marshmallow.Schema):
6666
missing=None,
6767
)
6868
groups = marshmallow.fields.List(
69-
helpers.LazyNestedField(
70-
nested="commercetools._schemas.None.stringSchema",
71-
unknown=marshmallow.EXCLUDE,
72-
allow_none=True,
73-
),
74-
allow_none=True,
75-
missing=None,
69+
marshmallow.fields.String(allow_none=True), allow_none=True, missing=None
7670
)
7771
valid_from = marshmallow.fields.DateTime(
7872
allow_none=True, missing=None, data_key="validFrom"

src/commercetools/_schemas/_error.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,7 @@ class InvalidFieldErrorSchema(ErrorObjectSchema):
517517
field = marshmallow.fields.String(allow_none=True)
518518
invalid_value = marshmallow.fields.Raw(allow_none=True, data_key="invalidValue")
519519
allowed_values = marshmallow.fields.List(
520-
helpers.LazyNestedField(
521-
nested="commercetools._schemas.None.anySchema",
522-
unknown=marshmallow.EXCLUDE,
523-
allow_none=True,
524-
),
520+
marshmallow.fields.Raw(allow_none=True),
525521
allow_none=True,
526522
missing=None,
527523
data_key="allowedValues",

src/commercetools/_schemas/_graph_ql.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ class GraphQLErrorSchema(marshmallow.Schema):
4141
allow_none=True,
4242
)
4343
path = marshmallow.fields.List(
44-
helpers.LazyNestedField(
45-
nested="commercetools._schemas.None.anySchema",
46-
unknown=marshmallow.EXCLUDE,
47-
allow_none=True,
48-
),
49-
allow_none=True,
44+
marshmallow.fields.Raw(allow_none=True), allow_none=True
5045
)
5146

5247
class Meta:

src/commercetools/_schemas/_message.py

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,11 +2840,7 @@ class ProductDeletedMessagePayloadSchema(MessagePayloadSchema):
28402840
"""Marshmallow schema for :class:`commercetools.types.ProductDeletedMessagePayload`."""
28412841

28422842
removed_image_urls = marshmallow.fields.List(
2843-
helpers.LazyNestedField(
2844-
nested="commercetools._schemas.None.stringSchema",
2845-
unknown=marshmallow.EXCLUDE,
2846-
allow_none=True,
2847-
),
2843+
marshmallow.fields.String(allow_none=True),
28482844
allow_none=True,
28492845
data_key="removedImageUrls",
28502846
)
@@ -2868,11 +2864,7 @@ class ProductDeletedMessageSchema(MessageSchema):
28682864
"""Marshmallow schema for :class:`commercetools.types.ProductDeletedMessage`."""
28692865

28702866
removed_image_urls = marshmallow.fields.List(
2871-
helpers.LazyNestedField(
2872-
nested="commercetools._schemas.None.stringSchema",
2873-
unknown=marshmallow.EXCLUDE,
2874-
allow_none=True,
2875-
),
2867+
marshmallow.fields.String(allow_none=True),
28762868
allow_none=True,
28772869
data_key="removedImageUrls",
28782870
)
@@ -3028,11 +3020,7 @@ class ProductPublishedMessagePayloadSchema(MessagePayloadSchema):
30283020
"""Marshmallow schema for :class:`commercetools.types.ProductPublishedMessagePayload`."""
30293021

30303022
removed_image_urls = marshmallow.fields.List(
3031-
helpers.LazyNestedField(
3032-
nested="commercetools._schemas.None.anySchema",
3033-
unknown=marshmallow.EXCLUDE,
3034-
allow_none=True,
3035-
),
3023+
marshmallow.fields.Raw(allow_none=True),
30363024
allow_none=True,
30373025
data_key="removedImageUrls",
30383026
)
@@ -3057,11 +3045,7 @@ class ProductPublishedMessageSchema(MessageSchema):
30573045
"""Marshmallow schema for :class:`commercetools.types.ProductPublishedMessage`."""
30583046

30593047
removed_image_urls = marshmallow.fields.List(
3060-
helpers.LazyNestedField(
3061-
nested="commercetools._schemas.None.anySchema",
3062-
unknown=marshmallow.EXCLUDE,
3063-
allow_none=True,
3064-
),
3048+
marshmallow.fields.Raw(allow_none=True),
30653049
allow_none=True,
30663050
data_key="removedImageUrls",
30673051
)
@@ -3124,11 +3108,7 @@ class ProductRevertedStagedChangesMessagePayloadSchema(MessagePayloadSchema):
31243108
"""Marshmallow schema for :class:`commercetools.types.ProductRevertedStagedChangesMessagePayload`."""
31253109

31263110
removed_image_urls = marshmallow.fields.List(
3127-
helpers.LazyNestedField(
3128-
nested="commercetools._schemas.None.stringSchema",
3129-
unknown=marshmallow.EXCLUDE,
3130-
allow_none=True,
3131-
),
3111+
marshmallow.fields.String(allow_none=True),
31323112
allow_none=True,
31333113
data_key="removedImageUrls",
31343114
)
@@ -3146,11 +3126,7 @@ class ProductRevertedStagedChangesMessageSchema(MessageSchema):
31463126
"""Marshmallow schema for :class:`commercetools.types.ProductRevertedStagedChangesMessage`."""
31473127

31483128
removed_image_urls = marshmallow.fields.List(
3149-
helpers.LazyNestedField(
3150-
nested="commercetools._schemas.None.stringSchema",
3151-
unknown=marshmallow.EXCLUDE,
3152-
allow_none=True,
3153-
),
3129+
marshmallow.fields.String(allow_none=True),
31543130
allow_none=True,
31553131
data_key="removedImageUrls",
31563132
)
@@ -3263,11 +3239,7 @@ class ProductVariantDeletedMessagePayloadSchema(MessagePayloadSchema):
32633239
allow_none=True,
32643240
)
32653241
removed_image_urls = marshmallow.fields.List(
3266-
helpers.LazyNestedField(
3267-
nested="commercetools._schemas.None.stringSchema",
3268-
unknown=marshmallow.EXCLUDE,
3269-
allow_none=True,
3270-
),
3242+
marshmallow.fields.String(allow_none=True),
32713243
allow_none=True,
32723244
data_key="removedImageUrls",
32733245
)
@@ -3290,11 +3262,7 @@ class ProductVariantDeletedMessageSchema(MessageSchema):
32903262
allow_none=True,
32913263
)
32923264
removed_image_urls = marshmallow.fields.List(
3293-
helpers.LazyNestedField(
3294-
nested="commercetools._schemas.None.stringSchema",
3295-
unknown=marshmallow.EXCLUDE,
3296-
allow_none=True,
3297-
),
3265+
marshmallow.fields.String(allow_none=True),
32983266
allow_none=True,
32993267
data_key="removedImageUrls",
33003268
)

src/commercetools/services/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import List
22

33
from commercetools import types
4-
from commercetools._schemas._project import ProjectUpdateSchema, ProjectSchema
4+
from commercetools._schemas._project import ProjectSchema, ProjectUpdateSchema
55
from commercetools.services import abstract
66

77
__all__ = ["ProjectService"]

0 commit comments

Comments
 (0)