From 1c7a9038ac97efd8e5b3a65e0bbe690f34087c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Rami=CC=81rez=20Mondrago=CC=81n?= Date: Tue, 22 Nov 2022 13:25:06 -0600 Subject: [PATCH 1/2] fix: Avoid duplicate entries in `required` array of JSON schema helpers --- singer_sdk/typing.py | 12 ++++++------ tests/snapshots/jsonschema/required_duplicates.json | 1 - .../required_duplicates_additional_properties.json | 1 - 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/singer_sdk/typing.py b/singer_sdk/typing.py index 5d38f0abe..c67e75a1a 100644 --- a/singer_sdk/typing.py +++ b/singer_sdk/typing.py @@ -47,7 +47,7 @@ import json import sys -from typing import Any, Generic, Mapping, TypeVar, Union, cast +from typing import Any, Generic, ItemsView, Mapping, TypeVar, Union, cast import sqlalchemy from jsonschema import validators @@ -461,7 +461,7 @@ def __init__( pattern_properties: A dictionary of regex patterns to match against property names, and the schema to match against the values. """ - self.wrapped: list[Property] = list(properties) + self.wrapped: dict[str, Property] = {prop.name: prop for prop in properties} self.additional_properties = additional_properties self.pattern_properties = pattern_properties @@ -474,7 +474,7 @@ def type_dict(self) -> dict: # type: ignore # OK: @classproperty vs @property """ merged_props = {} required = [] - for w in self.wrapped: + for w in self.wrapped.values(): merged_props.update(w.to_dict()) if not w.optional: required.append(w.name) @@ -529,13 +529,13 @@ def type_dict(self) -> dict: # type: ignore # OK: @classproperty vs @property class PropertiesList(ObjectType): """Properties list. A convenience wrapper around the ObjectType class.""" - def items(self) -> list[tuple[str, Property]]: + def items(self) -> ItemsView[str, Property]: """Get wrapped properties. Returns: List of (name, property) tuples. """ - return [(p.name, p) for p in self.wrapped] + return self.wrapped.items() def append(self, property: Property) -> None: """Append a property to the property list. @@ -543,7 +543,7 @@ def append(self, property: Property) -> None: Args: property: Property to add """ - self.wrapped.append(property) + self.wrapped[property.name] = property def to_jsonschema_type( diff --git a/tests/snapshots/jsonschema/required_duplicates.json b/tests/snapshots/jsonschema/required_duplicates.json index aaa890002..5484f5247 100644 --- a/tests/snapshots/jsonschema/required_duplicates.json +++ b/tests/snapshots/jsonschema/required_duplicates.json @@ -25,7 +25,6 @@ } }, "required": [ - "email", "email", "username" ] diff --git a/tests/snapshots/jsonschema/required_duplicates_additional_properties.json b/tests/snapshots/jsonschema/required_duplicates_additional_properties.json index c433255b1..17a773ded 100644 --- a/tests/snapshots/jsonschema/required_duplicates_additional_properties.json +++ b/tests/snapshots/jsonschema/required_duplicates_additional_properties.json @@ -25,7 +25,6 @@ } }, "required": [ - "email", "email", "username" ], From 2df409ba715284d1e44cc030bc5b13f1526a2243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Rami=CC=81rez=20Mondrago=CC=81n?= Date: Wed, 7 Dec 2022 00:21:41 -0600 Subject: [PATCH 2/2] Update snapshots --- .../jsonschema/required_duplicates_no_additional_properties.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/snapshots/jsonschema/required_duplicates_no_additional_properties.json b/tests/snapshots/jsonschema/required_duplicates_no_additional_properties.json index fd65061f8..dcd44ca07 100644 --- a/tests/snapshots/jsonschema/required_duplicates_no_additional_properties.json +++ b/tests/snapshots/jsonschema/required_duplicates_no_additional_properties.json @@ -25,7 +25,6 @@ } }, "required": [ - "email", "email", "username" ],