Skip to content

Commit

Permalink
fix: new properties are ignored whith allOf using $ref (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Denis Blanchette <dblanchette@coveo.com>
  • Loading branch information
declement and dblanchette authored Aug 30, 2024
1 parent 8e5481d commit 7049af7
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions json_schema_for_humans/schema/schema_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,26 @@ def examples(self) -> List[str]:

return [example.literal_str for example in possible_examples.array_items if example.literal_str]

def _merge_node(self, other: "SchemaNode"):
merged_node = copy.copy(self)
merged_node.keywords = {k: copy.copy(v) for k, v in self.keywords.items()}
merged_node.array_items = [copy.copy(i) for i in self.array_items]
merged_node.properties = {k: copy.copy(v) for k, v in self.properties.items()}

merged_node.keywords.update({k: copy.copy(v) for k, v in other.keywords.items()})
merged_node.array_items += [copy.copy(i) for i in other.array_items]

# Merge node properties, ignoring conflicts
for k, v in other.properties.items():
if k in merged_node.properties:
merged_node.properties[k] = merged_node.properties[k]._merge_node(v)
else:
merged_node.properties[k] = v

merged_node.literal = self.literal or other.literal

return merged_node

@property
def refers_to_merged(self) -> Optional["SchemaNode"]:
"""The referenced node, with values from the current node merged in"""
Expand All @@ -299,16 +319,7 @@ def refers_to_merged(self) -> Optional["SchemaNode"]:
if not self.refers_to:
return None

merged_node = copy.copy(self.refers_to)
merged_node.keywords = {k: copy.copy(v) for k, v in self.refers_to.keywords.items()}
merged_node.array_items = [copy.copy(i) for i in self.refers_to.array_items]

merged_node.keywords.update({k: copy.copy(v) for k, v in self.keywords.items()})
merged_node.array_items += [copy.copy(i) for i in self.array_items]

merged_node.literal = self.literal or self.refers_to.literal

return merged_node
return self.refers_to._merge_node(self)

def get_keyword(self, keyword: SchemaKeyword) -> Optional["SchemaNode"]:
"""Get the value of a keyword if present, and it is not a property (to avoid conflicts with properties being
Expand Down

0 comments on commit 7049af7

Please sign in to comment.