Skip to content

Instantly share code, notes, and snippets.

@wfng92
Created March 14, 2021 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wfng92/df9d147f8ffb9b76f4eee027513eb00a to your computer and use it in GitHub Desktop.
Save wfng92/df9d147f8ffb9b76f4eee027513eb00a to your computer and use it in GitHub Desktop.
def add_examples(openapi_schema: dict, docs_dir):
path_key = 'paths'
code_key = 'x-codeSamples'
for folder in os.listdir(docs_dir):
base_path = os.path.join(docs_dir, folder)
files = [f for f in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, f))]
for f in files:
parts = f.split('-')
if len(parts) >= 2:
route = '/' + '/'.join(parts[:-1])
method = parts[-1].split('.')[0]
print(f'[{path_key}][{route}][{method}][{code_key}]')
if route in openapi_schema[path_key]:
if code_key not in openapi_schema[path_key][route][method]:
openapi_schema[path_key][route][method].update({code_key: []})
openapi_schema[path_key][route][method][code_key].append({
'lang': lable_lang_mapping[folder],
'source': open(os.path.join(base_path, f), "r").read(),
'label': folder,
})
else:
print(f'Error in adding examples code to openapi {f}')
return openapi_schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment