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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import json
import logging
import os
import re
from pathlib import Path
from typing import Any, Dict, List, Set

from prance import ResolvingParser, ValidationError as PranceValidationError
from openapi_spec_validator.validation.exceptions import (
    ValidationError as OASValidationError,  # type: ignore[attr-defined]
    OpenAPIValidationError,
)

from autoresttest.config import get_config
from autoresttest.models import (
    OperationProperties,
    ParameterKey,
    ParameterProperties,
    ResponseProperties,
    SchemaProperties,
    to_dict_helper,
)


def json_spec_output(output_directory: Path, file_name: str, spec: Dict):
    """
    Create a testing JSON file from the specification parsing output.
    """
    output_file = output_directory / file_name
    with output_file.open("w", encoding="utf-8") as file:
        json.dump(spec, file, ensure_ascii=False, indent=4)


CONFIG = get_config()


def default_recursive_limit_handler(
    limit: int, parsed_url: Any, recursions: tuple[str, ...] = ()
) -> dict[str, Any]:
    """
    Prance invokes this handler when a circular/self-reference exceeds the recursion_limit.

    Parameters:
        limit: The configured recursion_limit value.
        parsed_url: The parsed URL of the reference that triggered the limit (contains the $ref path).
        recursions: Tuple of reference URLs that form the circular chain.

    Returns a placeholder schema to substitute for the circular reference.
    """
    # Extract the reference name from the parsed URL fragment (e.g., "#/components/schemas/Node" -> "Node")
    ref_name = "self-reference"
    if parsed_url and hasattr(parsed_url, "fragment") and parsed_url.fragment:
        ref_name = parsed_url.fragment.split("/")[-1]

    return {
        "type": "object",
        "title": f"Circular Reference: {ref_name}",
        "description": f"circular_self_reference:{ref_name}",
        "properties": {},
    }


class LenientResolvingParser(ResolvingParser):
    """
    ResolvingParser constructor calls:
    1. RefResolver.resolve_references()
    2. BaseParser._validate(self) -> openapi-spec-validator
    Override _validate to provide error on incorrect schemas instead of crashing
    """

    def _validate(self):
        try:
            super()._validate()
        except (
            PranceValidationError,
            OASValidationError,
            OpenAPIValidationError,
        ) as exc:
            logging.warning(
                "OpenAPI validation failed for %s; continuing with unvalidated "
                "specs. Error: %s",
                getattr(self, "url", None),
                exc,
            )
            self.valid = False


class SpecificationParser:
    """
    Class to parse a specification file and return a dictionary of all the operations and their properties.
    """

    def __init__(self, spec_path=None, spec_name=None):
        self.spec_path = spec_path
        self.spec_name = spec_name
        if spec_path is None:
            raise ValueError("No specification path provided.")

        self.resolving_parser = self._build_resolving_parser(spec_path)
        if self.resolving_parser.specification is None:
            raise ValueError("An error occurred during Prance specification parsing.")

        self.directory_path = (
            "specs/aratrl-openapi/"  # DEPRECATED - NOT USED IN EXECUTION
        )
        self.all_specs = {}

    def _build_resolving_parser(self, spec_path):
        parser_cls = (
            ResolvingParser if CONFIG.strict_validation else LenientResolvingParser
        )
        return parser_cls(
            spec_path,
            backend="openapi-spec-validator",  # AutoRestTest supports OAS 3.x
            strict=True,
            recursion_limit=CONFIG.recursion_limit,
            recursion_limit_handler=default_recursive_limit_handler,
        )

    def get_api_url(self) -> str:
        """
        Extract the server URL from the specification file.
        """
        spec = self.resolving_parser.specification
        if spec is None:
            raise ValueError("Specification not initialized successfully.")
        servers = spec.get("servers")
        if not servers:
            raise ValueError("No servers defined in the OpenAPI specification.")
        first_server = servers[0]
        if not isinstance(first_server, dict):
            raise ValueError("Invalid server definition in the OpenAPI specification.")
        url = first_server.get("url")
        if not url:
            raise ValueError("Server URL is missing in the OpenAPI specification.")
        return url

    def get_api_title(self) -> str | None:
        """
        Extract the title of the API from the specification file.
        """
        spec = self.resolving_parser.specification
        if spec is None:
            return None
        info = spec.get("info")
        if not isinstance(info, dict):
            return None
        title = info.get("title")
        return title if isinstance(title, str) else None

    def process_parameter_object_properties(
        self, properties: Dict | None
    ) -> Dict[str, SchemaProperties] | None:
        """
        Process the properties of a parameter of type object to return a dictionary of all the properties and their
        corresponding parameter values.
        """
        if properties is None or not isinstance(properties, dict):
            return None
        object_properties = {}
        for name, values in properties.items():
            object_properties.setdefault(
                name, self.process_parameter_schema(values)
            )  # check if this is correct, or if it should be process_parameter
        # Filter out None values from nested schema processing
        return {k: v for k, v in object_properties.items() if v is not None}

    def _infer_schema_type(self, schema: Dict) -> str | None:
        """Infer schema type from context when not explicitly provided."""
        if schema.get("type"):
            return schema.get("type")

        # Array indicators
        if schema.get("items") is not None:
            return "array"

        # Object indicators
        if schema.get("properties") is not None:
            return "object"
        if schema.get("additionalProperties") is not None:
            return "object"

        # Infer from enum values
        enum = schema.get("enum")
        if enum and len(enum) > 0:
            first_val = enum[0]
            if isinstance(first_val, bool):
                return "boolean"
            if isinstance(first_val, int):
                return "integer"
            if isinstance(first_val, float):
                return "number"
            if isinstance(first_val, str):
                return "string"

        # Numeric constraint indicators
        if any(
            schema.get(k) is not None
            for k in ("minimum", "maximum", "multipleOf", "exclusiveMinimum", "exclusiveMaximum")
        ):
            return "number"

        # String constraint indicators
        if any(schema.get(k) is not None for k in ("minLength", "maxLength", "pattern")):
            return "string"

        return None

    def process_parameter_schema(
        self, schema: Dict | None, description: str | None = None
    ) -> SchemaProperties | None:
        """
        Process the schema of a parameter to return a ValueProperties object
        """
        if not schema or not isinstance(schema, dict):
            return None

        value_properties = SchemaProperties(
            type=self._infer_schema_type(schema),
            format=schema.get("format"),
            description=schema.get("description") if not description else description,
            items=self.process_parameter_schema(
                schema.get("items")
            ),  # recursively process items
            properties=self.process_parameter_object_properties(
                schema.get("properties")
            ),
            required=schema.get("required") or [],
            default=schema.get("default"),
            enum=schema.get("enum") or [],
            minimum=schema.get("minimum"),
            maximum=schema.get("maximum"),
            min_length=schema.get("minLength"),
            max_length=schema.get("maxLength"),
            pattern=schema.get("pattern"),
            max_items=schema.get("maxItems"),
            min_items=schema.get("minItems"),
            unique_items=schema.get("uniqueItems"),
            additional_properties=schema.get("additionalProperties"),
            nullable=schema.get("nullable"),
            read_only=schema.get("readOnly"),
            write_only=schema.get("writeOnly"),
            example=schema.get("example"),
            examples=schema.get("examples") or [],
        )
        return value_properties

    def process_parameter(self, parameter: Any) -> ParameterProperties:
        """
        Process an individual parameter to return a ParameterProperties object.
        """
        if not isinstance(parameter, dict):
            return ParameterProperties()
        parameter_properties = ParameterProperties(
            name=parameter.get("name") or "",
            in_value=parameter.get("in"),
            description=parameter.get("description"),
            required=parameter.get("required"),
            deprecated=parameter.get("deprecated"),
            allow_empty_value=parameter.get("allowEmptyValue"),
            style=parameter.get("style"),
            explode=parameter.get("explode"),
            allow_reserved=parameter.get("allowReserved"),
        )
        if parameter.get("schema"):
            parameter_properties.schema = self.process_parameter_schema(
                parameter.get("schema")
            )
        return parameter_properties

    def process_parameters(
        self, parameter_list
    ) -> Dict[ParameterKey, ParameterProperties]:
        """
        Process the parameters list to return a Dictionary with all its properties and values.
        """
        parameters = {}
        if parameter_list:
            for parameter in parameter_list:
                parameter_properties = self.process_parameter(parameter)
                if not parameter_properties.name:
                    continue
                key = (parameter_properties.name, parameter_properties.in_value)
                parameters[key] = parameter_properties
        return parameters

    def process_request_body(self, request_body) -> Dict[str, SchemaProperties]:
        """
        Process the request body to return a Dictionary with mime type and its properties and values.
        """

        request_body_properties = {}
        content = request_body.get("content")
        description = request_body.get("description")
        if content:
            for mime_type, mime_details in content.items():
                # if we need to check required list, do it here
                schema = mime_details.get("schema")
                if schema:
                    request_body_properties[mime_type] = self.process_parameter_schema(
                        schema, description
                    )

        return request_body_properties

    def process_responses(self, responses) -> Dict[str, ResponseProperties]:
        """
        Process the responses to return a Dictionary with status code and its properties and values.
        """
        response_properties = {}
        for status_code, response_details in responses.items():
            response_properties.setdefault(
                status_code,
                ResponseProperties(
                    status_code=status_code,
                    description=response_details.get("description"),
                ),
            )
            content = response_details.get("content")
            if content:
                for mime_type, mime_details in content.items():
                    # if we need to check required list, do it here
                    schema = mime_details.get("schema")
                    if schema:
                        response_properties[status_code].content[mime_type] = (
                            self.process_parameter_schema(schema)
                        )
        return response_properties

    def process_operation_details(
        self,
        http_method: str,
        endpoint_path: str,
        operation_details: Dict,
        operation_id: str,
        merged_parameters: List[Dict],
    ) -> OperationProperties:
        """
        Process the parameters and request body details within a given operation to return as OperationProperties object.
        """
        operation_properties = OperationProperties(
            operation_id=operation_id,
            endpoint_path=endpoint_path,
            http_method=http_method,
            summary=operation_details.get("summary"),
        )
        if merged_parameters:
            operation_properties.parameters = self.process_parameters(
                parameter_list=merged_parameters
            )

        if operation_details.get("requestBody"):
            operation_properties.request_body = self.process_request_body(
                request_body=operation_details.get("requestBody")
            )

        if operation_details.get("responses"):
            operation_properties.responses = self.process_responses(
                responses=operation_details.get("responses")
            )

        # maybe add security details?

        return operation_properties

    def _normalize_endpoint_path(self, endpoint_path: str) -> str:
        """
        Convert a path template to a safe string segment for fallback operationIds.
        """
        normalized = endpoint_path.replace("{", "").replace("}", "")
        normalized = re.sub(r"[^a-zA-Z0-9]+", "_", normalized).strip("_")
        return normalized or "root"

    def _resolve_operation_id(
        self,
        http_method: str,
        endpoint_path: str,
        operation_details: Dict,
        seen_ids: Set[str],
    ) -> str:
        """
        Use provided operationId when available; otherwise construct a unique, deterministic fallback.
        If a duplicate is detected, append an incrementing suffix to keep IDs unique.
        """
        provided_id = operation_details.get("operationId")
        candidate = provided_id if provided_id else None

        if not candidate:
            normalized_path = self._normalize_endpoint_path(endpoint_path)
            candidate = f"{http_method.lower()}_{normalized_path}"

        base_candidate = candidate
        suffix = 1
        while candidate in seen_ids:
            candidate = f"{base_candidate}_{suffix}"
            suffix += 1

        if provided_id and candidate != provided_id:
            logging.warning(
                "Duplicate operationId '%s' for %s %s; using '%s' to avoid collision.",
                provided_id,
                http_method,
                endpoint_path,
                candidate,
            )

        return candidate

    def _merge_parameters(
        self, path_parameters: List[Dict], operation_parameters: List[Dict]
    ) -> List[Dict]:
        """
        Merge path-level and operation-level parameters, letting operation-level definitions override
        matching path-level parameters (by (name, in)).
        """
        merged: Dict[ParameterKey, Dict] = {}
        for raw_param in path_parameters or []:
            if isinstance(raw_param, dict):
                name = raw_param.get("name")
                if name is not None:
                    merged[(name, raw_param.get("in"))] = raw_param
        for raw_param in operation_parameters or []:
            if isinstance(raw_param, dict):
                name = raw_param.get("name")
                if name is not None:
                    merged[(name, raw_param.get("in"))] = raw_param
        return list(merged.values())

    def parse_specification(self) -> Dict[str, OperationProperties]:
        """
        Parse the specification file to return a dictionary of all the operations and their properties.

        The key of the dictionary is the operationId and the value is an OperationProperties object.
        """
        supported_methods = {"get", "post", "put", "delete", "head", "options", "patch"}
        operation_collection = {}
        seen_ids: Set[str] = set()
        spec = self.resolving_parser.specification
        if spec is None:
            return operation_collection
        spec_paths = spec.get("paths", {})
        if not isinstance(spec_paths, dict):
            spec_paths = {}
        for endpoint_path, endpoint_details in spec_paths.items():
            if not isinstance(endpoint_details, dict):
                continue
            path_level_parameters = endpoint_details.get("parameters", [])
            for http_method, operation_details in endpoint_details.items():
                if http_method in supported_methods:
                    merged_parameters = self._merge_parameters(
                        path_level_parameters, operation_details.get("parameters", [])
                    )
                    operation_id = self._resolve_operation_id(
                        http_method, endpoint_path, operation_details, seen_ids
                    )
                    seen_ids.add(operation_id)
                    operation_properties = self.process_operation_details(
                        http_method,
                        endpoint_path,
                        operation_details,
                        operation_id,
                        merged_parameters,
                    )
                    operation_collection[operation_id] = operation_properties
        return operation_collection

    def parse_all_specifications(self) -> dict[str, dict[str, OperationProperties]]:
        """
        Parse all the specification files in the directory to return a dictionary of all the operations and their properties.
        """
        for file_name in os.listdir(self.directory_path):
            print("Specification parsing for file: ", file_name)
            self.spec_path = os.path.join(self.directory_path, file_name)
            self.resolving_parser = self._build_resolving_parser(self.spec_path)
            self.all_specs[file_name] = self.parse_specification()
            # print("Output: " + str(output))

        return self.all_specs

    def all_json_spec_output(self):
        """
        Create a testing JSON file from the specification parsing output.
        """
        all_specs = {
            file_name: to_dict_helper(spec)
            for file_name, spec in self.all_specs.items()
        }

        output_directory = Path("./testing_output")
        output_directory.mkdir(parents=True, exist_ok=True)

        for file_name, spec in all_specs.items():
            json_spec_output(output_directory, file_name, spec)

    def single_json_spec_output(self):
        output_directory = Path("./testing_output")
        output_directory.mkdir(parents=True, exist_ok=True)
        output = self.parse_specification()
        file_name = self.spec_name if self.spec_name else "spec_output.json"
        json_spec_output(output_directory, file_name, to_dict_helper(output))


if __name__ == "__main__":
    # testing
    # spec_path = os.path.normpath("../../aratrl-openapi/project.yaml")
    spec_path = os.path.normpath("kafka-rest.yaml")
    spec_parser = SpecificationParser(spec_name="project", spec_path=spec_path)
    spec_parser.parse_specification()
    print(spec_parser.parse_specification())
    # spec_parser.single_json_spec_output()
    # spec_parser.parse_all_specifications()
    # spec_parser.all_json_spec_output()

    # spec_parser = SpecificationParser(spec_path="../specs/original/oas/genome-nexus.yaml", spec_name="genome-nexus")
    # spec_parser.single_json_spec_output()

    # output = spec_parser.parse_specification()
    # print(output["fetchPostTranslationalModificationsByPtmFilterPOST"])
    # print(output["endpoint-add-tracks-to-playlist"])
    # print(output["endpoint-get-playlist"])
    # print(output["endpoint-remove-tracks-playlist"])