Last active
April 10, 2023 21:42
-
-
Save zeddee/630c0829bf85c2aeb48a7a6de5c81cea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Copyright 2023 zed@shootbird.work | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
""" | |
from pprint import pprint | |
from pathlib import Path | |
import json | |
from typing import Any, Dict, List, Optional | |
from textwrap import dedent | |
EXPAND = False | |
def load_schemas(apispec_filepath: Path) -> Dict[str, any]: | |
with apispec_filepath.open("r") as fp: | |
buf = fp.read() | |
data = json.loads(buf) | |
fp.close() | |
components = data["components"] | |
_ = components["responses"] | |
_ = components["securitySchemes"] | |
schemas = components["schemas"] | |
return schemas | |
def _introspect_properties( | |
_root_obj: Dict[str, Any], | |
_props: Dict[str, Any], | |
_required_fields: Optional[List[str]]=[] | |
) -> Dict[str, Any]: | |
_post_props: Dict[str, Any] = dict() | |
for _key in _props.keys(): | |
_key_out = _key | |
if _key == '$ref' and EXPAND: | |
# Resolve JSON schema reference | |
_ref_object = _props[_key].split("/")[-1] | |
for k,v in _introspect_properties(_root_obj, _root_obj[_ref_object]["properties"]).items(): | |
_post_props.update({k:v}) | |
elif isinstance(_props.get(_key), dict): | |
# Recurse | |
_post_props.update({_key_out: _introspect_properties(_root_obj, _props[_key], _props.get("required", []))}) | |
else: | |
# Passthrough | |
_post_props.update({_key_out: _props.get(_key)}) | |
if isinstance(_post_props.get(_key_out), dict): | |
_post_props[_key_out].update({"required": ["Yes" if _key in _required_fields else "No"][0]}) | |
return _post_props | |
def main(): | |
schemas = load_schemas(Path("data/v2.json")) | |
for _obj in schemas.keys(): | |
if _obj == "Producer": | |
print(dedent(f""" | |
============= {_obj} ================ | |
""")) | |
pprint(_introspect_properties( | |
schemas, | |
schemas[_obj]["properties"], | |
schemas[_obj].get("required",[]), | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment