|
| 1 | + |
| 2 | +from collections.abc import Callable |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from attr import evolve |
| 6 | + |
| 7 | +from openapi_python_client.parser.properties.any import AnyProperty |
| 8 | +from openapi_python_client.parser.properties.enum_property import EnumProperty, ValueType |
| 9 | +from openapi_python_client.parser.properties.float import FloatProperty |
| 10 | +from openapi_python_client.parser.properties.int import IntProperty |
| 11 | +from openapi_python_client.parser.properties.list_property import ListProperty |
| 12 | +from openapi_python_client.parser.properties.protocol import PropertyProtocol |
| 13 | +from openapi_python_client.parser.properties.string import StringProperty |
| 14 | + |
| 15 | + |
| 16 | +def merge_properties(prop1: PropertyProtocol, prop2: PropertyProtocol) -> PropertyProtocol: |
| 17 | + """Attempt to create a new property that incorporates the behavior of both. |
| 18 | + |
| 19 | + This is used when merging schemas with allOf, when two schemas define a property with the same name. |
| 20 | + |
| 21 | + OpenAPI defines allOf in terms of validation behavior: the input must pass the validation rules |
| 22 | + defined in all of the listed schemas. Our task here is slightly more difficult, since we must end |
| 23 | + up with a single Property object that will be used to generate a single class property in the |
| 24 | + generated code. Due to limitations of our internal model, this may not be possible for some |
| 25 | + combinations of property attributes that OpenAPI supports (for instance, we have no way to represent |
| 26 | + a string property that must match two different regexes). |
| 27 | +
|
| 28 | + Properties can also have attributes that do not represent validation rules, such as "description" |
| 29 | + and "example". OpenAPI does not define any overriding/aggregation rules for these in allOf. The |
| 30 | + implementation here is, assuming prop1 and prop2 are in the same order that the schemas were in the |
| 31 | + allOf, any such attributes that prop2 specifies will override the ones from prop1. |
| 32 | +
|
| 33 | + Any failure is thrown as a ValueError. |
| 34 | + """ |
| 35 | + if isinstance(prop1, EnumProperty) or isinstance(prop2, EnumProperty): |
| 36 | + return _merge_with_enum(prop1, prop2) |
| 37 | + |
| 38 | + if prop1.__class__ == prop2.__class__: |
| 39 | + return _merge_same_type(prop1, prop2) |
| 40 | + |
| 41 | + if isinstance(prop1, AnyProperty) or isinstance(prop2, AnyProperty): |
| 42 | + return _merge_with_any(prop1, prop2) |
| 43 | + |
| 44 | + if _is_numeric(prop1) and _is_numeric(prop2): |
| 45 | + return _merge_numeric(prop1, prop2) |
| 46 | + |
| 47 | + raise ValueError("defined with two incompatible types") |
| 48 | + |
| 49 | + |
| 50 | +def _is_numeric(prop: PropertyProtocol) -> bool: |
| 51 | + return isinstance(prop, IntProperty) or isinstance(prop, FloatProperty) |
| 52 | + |
| 53 | + |
| 54 | +def _merge_same_type(prop1: PropertyProtocol, prop2: PropertyProtocol) -> PropertyProtocol: |
| 55 | + if prop1 == prop2: |
| 56 | + # It's always OK to redefine a property with everything exactly the same |
| 57 | + return prop1 |
| 58 | + |
| 59 | + if isinstance(prop1, StringProperty): |
| 60 | + return _merge_string(prop1, prop2) |
| 61 | + |
| 62 | + if isinstance(prop1, ListProperty): |
| 63 | + # There's no clear way to represent the intersection of two different list types. Fail in this case. |
| 64 | + if prop1.inner_property != prop2.inner_property: |
| 65 | + raise ValueError("can't redefine a list property with a different element type") |
| 66 | + |
| 67 | + # For all other property types, there aren't any special attributes that affect validation, so just |
| 68 | + # apply the rules for common attributes like "description". |
| 69 | + return _merge_common_attributes(prop1, prop2) |
| 70 | + |
| 71 | + |
| 72 | +def _merge_string(prop1: StringProperty, prop2: StringProperty) -> StringProperty: |
| 73 | + # If max_length was specified for both, the smallest value takes precedence. If only one of them |
| 74 | + # specifies it, _combine_values will pick that one. |
| 75 | + max_length: int | None = _combine_values(prop1.max_length, prop2.max_length, lambda a, b: min([a, b])) |
| 76 | + |
| 77 | + # If pattern was specified for both, we have a problem. OpenAPI has no logical objection to this; |
| 78 | + # it would just mean the value must match both of the patterns to be valid. But we have no way to |
| 79 | + # represent this in our internal model currently. |
| 80 | + pattern: str | None | ValueError = _combine_values( |
| 81 | + prop1.pattern, |
| 82 | + prop2.pattern, |
| 83 | + lambda a, b: ValueError("specified two different regex patterns") |
| 84 | + ) |
| 85 | + if isinstance(pattern, ValueError): |
| 86 | + raise pattern |
| 87 | + |
| 88 | + return _merge_common_attributes(evolve(prop1, max_length=max_length, pattern=pattern), prop2) |
| 89 | + |
| 90 | + |
| 91 | +def _merge_numeric(prop1: PropertyProtocol, prop2: PropertyProtocol) -> IntProperty: |
| 92 | + # Here, one of the properties was defined as "int" and the other was just a general number (which |
| 93 | + # we call FloatProperty). "Must be integer" is the stricter validation rule, so the result must be |
| 94 | + # an IntProperty. |
| 95 | + int_prop = prop1 if isinstance(prop1, IntProperty) else prop2 |
| 96 | + result = _merge_common_attributes(int_prop, prop1, prop2) |
| 97 | + if result.default is not None: |
| 98 | + if isinstance(result.default, float) and not result.default.is_integer(): |
| 99 | + raise ValueError(f"default value {result.default} is not valid for an integer property") |
| 100 | + return result |
| 101 | + |
| 102 | + |
| 103 | +def _merge_with_any(prop1: PropertyProtocol, prop2: PropertyProtocol) -> PropertyProtocol: |
| 104 | + # AnyProperty implies no validation rules for a value, so merging it with any other type just means |
| 105 | + # we should use the validation rules for the other type and the result should not be an AnyProperty. |
| 106 | + non_any_prop = prop2 if isinstance(prop1, AnyProperty) else prop1 |
| 107 | + return _merge_common_attributes(non_any_prop, prop1, prop2) |
| 108 | + |
| 109 | + |
| 110 | +def _merge_with_enum(prop1: PropertyProtocol, prop2: PropertyProtocol) -> EnumProperty: |
| 111 | + if isinstance(prop1, EnumProperty) and isinstance(prop2, EnumProperty): |
| 112 | + # We want the narrowest validation rules that fit both, so use whichever values list is a |
| 113 | + # subset of the other. |
| 114 | + values: dict[str, ValueType] |
| 115 | + if _values_are_subset(prop1, prop2): |
| 116 | + values = prop1.values |
| 117 | + elif _values_are_subset(prop2, prop1): |
| 118 | + values = prop2.values |
| 119 | + else: |
| 120 | + raise ValueError("can't redefine an enum property with incompatible lists of values") |
| 121 | + return _merge_common_attributes(evolve(prop1, values=values), prop2) |
| 122 | + |
| 123 | + # If enum values were specified for just one of the properties, use those. |
| 124 | + enum_prop = prop1 if isinstance(prop1, EnumProperty) else prop2 |
| 125 | + non_enum_prop = prop2 if isinstance(prop1, EnumProperty) else prop1 |
| 126 | + if ( |
| 127 | + (isinstance(non_enum_prop, IntProperty) and enum_prop.value_type is int) or |
| 128 | + (isinstance(non_enum_prop, StringProperty) and enum_prop.value_type is str) |
| 129 | + ): |
| 130 | + return _merge_common_attributes(enum_prop, prop1, prop2) |
| 131 | + raise ValueError("defined with two incompatible types") |
| 132 | + |
| 133 | + |
| 134 | +def _merge_common_attributes(base: PropertyProtocol, *extend_with: PropertyProtocol) -> PropertyProtocol: |
| 135 | + """Create a new instance based on base, overriding basic attributes with values from extend_with, in order. |
| 136 | + |
| 137 | + For "default", "description", and "example", a non-None value overrides any value from a previously |
| 138 | + specified property. The behavior is similar to using the spread operator with dictionaries, except |
| 139 | + that None means "not specified". |
| 140 | +
|
| 141 | + For "required", any True value overrides all other values (a property that was previously required |
| 142 | + cannot become optional). |
| 143 | + """ |
| 144 | + current = base |
| 145 | + for override in extend_with: |
| 146 | + current = evolve( |
| 147 | + current, |
| 148 | + required=current.required or override.required, |
| 149 | + default = override.default or current.default, |
| 150 | + description = override.description or current.description, |
| 151 | + example = override.example or current.example, |
| 152 | + ) |
| 153 | + return current |
| 154 | + |
| 155 | + |
| 156 | +def _values_are_subset(prop1: EnumProperty, prop2: EnumProperty) -> bool: |
| 157 | + return set(prop1.values.items()) <= set(prop2.values.items()) |
| 158 | + |
| 159 | + |
| 160 | +def _combine_values(value1: Any, value2: Any, combinator: Callable) -> Any: |
| 161 | + if value1 == value2: |
| 162 | + return value1 |
| 163 | + if value1 is None: |
| 164 | + return value2 |
| 165 | + if value2 is None: |
| 166 | + return value1 |
| 167 | + return combinator(value1, value2) |
0 commit comments