| 1 | # Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # This source code is licensed under the terms described in the LICENSE file in |
| 5 | # the root directory of this source tree. |
| 6 | |
| 7 | import json |
| 8 | import typing |
| 9 | from pathlib import Path |
| 10 | from typing import TextIO |
| 11 | |
| 12 | from ..strong_typing.schema import object_to_json, StrictJsonType |
| 13 | |
| 14 | from .generator import Generator |
| 15 | from .options import Options |
| 16 | from .specification import Document |
| 17 | |
| 18 | |
| 19 | THIS_DIR = Path(__file__).parent |
| 20 | |
| 21 | |
| 22 | class Specification: |
| 23 | document: Document |
| 24 | |
| 25 | def __init__(self, endpoint: type, options: Options): |
| 26 | generator = Generator(endpoint, options) |
| 27 | self.document = generator.generate() |
| 28 | |
| 29 | def get_json(self) -> StrictJsonType: |
| 30 | """ |
| 31 | Returns the OpenAPI specification as a Python data type (e.g. `dict` for an object, `list` for an array). |
| 32 | |
| 33 | The result can be serialized to a JSON string with `json.dump` or `json.dumps`. |
| 34 | """ |
| 35 | |
| 36 | json_doc = typing.cast(StrictJsonType, object_to_json(self.document)) |
| 37 | |
| 38 | if isinstance(json_doc, dict): |
| 39 | # rename vendor-specific properties |
| 40 | tag_groups = json_doc.pop("tagGroups", None) |
| 41 | if tag_groups: |
| 42 | json_doc["x-tagGroups"] = tag_groups |
| 43 | tags = json_doc.get("tags") |
| 44 | if tags and isinstance(tags, list): |
| 45 | for tag in tags: |
| 46 | if not isinstance(tag, dict): |
| 47 | continue |
| 48 | |
| 49 | display_name = tag.pop("displayName", None) |
| 50 | if display_name: |
| 51 | tag["x-displayName"] = display_name |
| 52 | |
| 53 | return json_doc |
| 54 | |
| 55 | def get_json_string(self, pretty_print: bool = False) -> str: |
| 56 | """ |
| 57 | Returns the OpenAPI specification as a JSON string. |
| 58 | |
| 59 | :param pretty_print: Whether to use line indents to beautify the output. |
| 60 | """ |
| 61 | |
| 62 | json_doc = self.get_json() |
| 63 | if pretty_print: |
| 64 | return json.dumps( |
| 65 | json_doc, check_circular=False, ensure_ascii=False, indent=4 |
| 66 | ) |
| 67 | else: |
| 68 | return json.dumps( |
| 69 | json_doc, |
| 70 | check_circular=False, |
| 71 | ensure_ascii=False, |
| 72 | separators=(",", ":"), |
| 73 | ) |
| 74 | |
| 75 | def write_json(self, f: TextIO, pretty_print: bool = False) -> None: |
| 76 | """ |
| 77 | Writes the OpenAPI specification to a file as a JSON string. |
| 78 | |
| 79 | :param pretty_print: Whether to use line indents to beautify the output. |
| 80 | """ |
| 81 | |
| 82 | json_doc = self.get_json() |
| 83 | if pretty_print: |
| 84 | json.dump( |
| 85 | json_doc, |
| 86 | f, |
| 87 | check_circular=False, |
| 88 | ensure_ascii=False, |
| 89 | indent=4, |
| 90 | ) |
| 91 | else: |
| 92 | json.dump( |
| 93 | json_doc, |
| 94 | f, |
| 95 | check_circular=False, |
| 96 | ensure_ascii=False, |
| 97 | separators=(",", ":"), |
| 98 | ) |
| 99 | |
| 100 | def write_html(self, f: TextIO, pretty_print: bool = False) -> None: |
| 101 | """ |
| 102 | Creates a stand-alone HTML page for the OpenAPI specification with ReDoc. |
| 103 | |
| 104 | :param pretty_print: Whether to use line indents to beautify the JSON string in the HTML file. |
| 105 | """ |
| 106 | |
| 107 | path = THIS_DIR / "template.html" |
| 108 | with path.open(encoding="utf-8", errors="strict") as html_template_file: |
| 109 | html_template = html_template_file.read() |
| 110 | |
| 111 | html = html_template.replace( |
| 112 | "{ /* OPENAPI_SPECIFICATION */ }", |
| 113 | self.get_json_string(pretty_print=pretty_print), |
| 114 | ) |
| 115 | |
| 116 | f.write(html) |