File size: 8,782 Bytes
6c50d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from __future__ import annotations

import itertools
from dataclasses import dataclass, field, asdict
from typing import (
    Any,
    Dict,
    Iterable,
    List,
    Optional,
    Tuple,
    TypeAlias,
    Union,
    TYPE_CHECKING,
)

import requests

if TYPE_CHECKING:
    from autoresttest.graph.generate_graph import OperationEdge


def to_dict_helper(item: Any) -> Any:
    """Convert nested dataclasses, dictionaries, and iterables into serialisable structures."""
    if hasattr(item, "to_dict"):
        return item.to_dict()
    if isinstance(item, dict):
        converted = {}
        for key, value in item.items():
            if value is None:
                continue
            converted_key = (
                "|".join("" if part is None else str(part) for part in key)
                if isinstance(key, tuple)
                else key
            )
            converted[converted_key] = to_dict_helper(value)
        return {key: value for key, value in converted.items() if value is not None}
    if isinstance(item, Iterable) and not isinstance(item, (str, bytes)):
        converted = [to_dict_helper(element) for element in item]
        return [value for value in converted if value is not None]
    return item


# Key used for identifying parameters uniquely by (name, in)
ParameterKey: TypeAlias = tuple[str, str | None]


def is_parameter_key(value: Any) -> bool:
    """
    Check if a value is essentially a ParameterKey (tuple of length 2 with str, str|None).
    """
    if not isinstance(value, tuple) or len(value) != 2:
        return False
    name, in_value = value
    return isinstance(name, str) and (in_value is None or isinstance(in_value, str))


@dataclass
class ResponseProperties:
    """Stores the properties of an HTTP response."""

    status_code: int = -1
    description: Optional[str] = None
    content: Dict[str, "SchemaProperties"] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        return to_dict_helper(asdict(self))


@dataclass
class SchemaProperties:
    """Represents schema properties for parameters or request bodies."""

    type: Optional[str] = None
    format: Optional[str] = None
    description: Optional[str] = None
    items: Optional["SchemaProperties"] = None
    properties: Optional[Dict[str, "SchemaProperties"]] = None
    required: List[str] = field(default_factory=list)
    default: Optional[Union[str, int, float, bool, List, Dict]] = None
    enum: List[str] = field(default_factory=list)
    minimum: Optional[int] = None
    maximum: Optional[int] = None
    min_length: Optional[int] = None
    max_length: Optional[int] = None
    pattern: Optional[str] = None
    max_items: Optional[int] = None
    min_items: Optional[int] = None
    unique_items: Optional[bool] = None
    additional_properties: Union[bool, "SchemaProperties", None] = True
    nullable: Optional[bool] = None
    read_only: Optional[bool] = None
    write_only: Optional[bool] = None
    example: Optional[Union[str, int, float, bool, List, Dict]] = None
    examples: List[Optional[Union[str, int, float, bool, List, Dict]]] = field(
        default_factory=list
    )

    def to_dict(self) -> Dict[str, Any]:
        return to_dict_helper(asdict(self))


@dataclass
class ParameterProperties:
    """Stores the properties of an OpenAPI parameter."""

    name: str = ""
    in_value: Optional[str] = None
    description: Optional[str] = None
    required: Optional[bool] = None
    deprecated: Optional[bool] = None
    allow_empty_value: Optional[bool] = None
    style: Optional[str] = None
    explode: Optional[bool] = None
    allow_reserved: Optional[bool] = None
    schema: Optional[SchemaProperties] = None
    example: Optional[Union[str, int, float, bool, List, Dict]] = None
    examples: List[Optional[Union[str, int, float, bool, List, Dict]]] = field(
        default_factory=list
    )
    content: Dict[str, SchemaProperties] = field(default_factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        return to_dict_helper(asdict(self))


@dataclass
class OperationProperties:
    """Stores the properties of an OpenAPI operation."""

    operation_id: str = ""
    endpoint_path: str = ""
    http_method: str = ""
    summary: Optional[str] = None
    parameters: Dict[ParameterKey, ParameterProperties] = field(default_factory=dict)
    request_body: Optional[Dict[str, SchemaProperties]] = None
    responses: Optional[Dict[str, ResponseProperties]] = None

    def to_dict(self) -> Dict[str, Any]:
        return to_dict_helper(asdict(self))


@dataclass
class RequestData:
    """Encapsulates the request information derived from an operation."""

    endpoint_path: str
    http_method: str
    parameters: Dict[ParameterKey, Any] | None
    request_body: Dict[str, Any] | None
    operation_properties: OperationProperties
    requirements: Optional["RequestRequirements"] = None


@dataclass
class RequestRequirements:
    """Captures dependencies required to satisfy a request edge."""

    edge: "OperationEdge"
    parameter_requirements: Dict[ParameterKey, Any] = field(default_factory=dict)
    request_body_requirements: Dict[str, Any] = field(default_factory=dict)

    def generate_combinations(self) -> List["RequestRequirements"]:
        combinations: List[RequestRequirements] = []

        param_combinations: List[Dict[ParameterKey, Any]] = []
        if self.parameter_requirements:
            for i in range(1, len(self.parameter_requirements) + 1):
                for subset in itertools.combinations(
                    self.parameter_requirements.keys(), i
                ):
                    param_combinations.append(
                        {key: self.parameter_requirements[key] for key in subset}
                    )

        body_combinations: List[Dict[str, Any]] = []
        if self.request_body_requirements:
            for i in range(1, len(self.request_body_requirements) + 1):
                for subset in itertools.combinations(
                    self.request_body_requirements.keys(), i
                ):
                    body_combinations.append(
                        {key: self.request_body_requirements[key] for key in subset}
                    )

        if not param_combinations and not body_combinations:
            return [self]

        if not param_combinations:
            for body_combination in body_combinations:
                combinations.append(
                    RequestRequirements(
                        edge=self.edge,
                        parameter_requirements={},
                        request_body_requirements=body_combination,
                    )
                )
            return combinations

        if not body_combinations:
            for param_combination in param_combinations:
                combinations.append(
                    RequestRequirements(
                        edge=self.edge,
                        parameter_requirements=param_combination,
                        request_body_requirements={},
                    )
                )
            return combinations

        for param_combination in param_combinations:
            for body_combination in body_combinations:
                combinations.append(
                    RequestRequirements(
                        edge=self.edge,
                        parameter_requirements=param_combination,
                        request_body_requirements=body_combination,
                    )
                )

        combinations.sort(
            key=lambda req: len(req.parameter_requirements)
            + len(req.request_body_requirements),
            reverse=True,
        )
        return combinations


@dataclass
class RequestResponse:
    """Pairs a request with its observed response."""

    request: RequestData
    response: requests.Response
    response_text: str


@dataclass
class ValueAction:
    """Represents an action taken by the value agent."""

    param_mappings: Optional[Dict[ParameterKey, Any]]
    body_mappings: Optional[Dict[str, Any]]


@dataclass
class SimilarityValue:
    """Captures similarity metadata when comparing operations."""

    dependent_val: Union[str, ParameterKey] = ""
    in_value: str = ""
    similarity: float = 0.0

    def to_dict(self) -> Dict[str, Any]:
        return {
            "response": self.dependent_val,
            "in_value": self.in_value,
            "similarity": self.similarity,
        }


__all__ = [
    "to_dict_helper",
    "ResponseProperties",
    "SchemaProperties",
    "ParameterProperties",
    "ParameterKey",
    "is_parameter_key",
    "OperationProperties",
    "RequestData",
    "RequestRequirements",
    "RequestResponse",
    "ValueAction",
    "SimilarityValue",
]