Skip to content

Commit ffa7238

Browse files
authored
feat: add node schemas to swagger definitions for mdw (#1862)
1 parent 0e223fe commit ffa7238

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ data
5151
/node_sdk/output.json
5252

5353
/priv/static/swagger/swagger_v3.*
54+
/docs/swagger_v3/node_oas3.yaml

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ RUN mix compile
9797
COPY config/runtime.exs config/
9898

9999
# Generate swagger V3 file
100+
RUN cp /home/aeternity/node/local/lib/aehttp-$NODE_VERSION/priv/oas3.yaml docs/swagger_v3/node_oas3.yaml
100101
RUN mix run --no-start -e 'IO.puts(Mix.Project.config[:version])' >AEMDW_VERSION
101102
ARG PATH_PREFIX
102103
RUN scripts/swagger-docs.py

docs/swagger_v3/transactions.spec.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ schemas:
3737
type: string
3838
tx:
3939
description: The transaction
40-
# TODO: define properties
41-
type: object
40+
$ref: '#/components/schemas/Tx'
4241
example:
4342
block_hash: mh_ufiYLdN8am8fBxMnb6xq2K4MQKo4eFSCF5bgixq4EzKMtDUXP
4443
block_height: 1

scripts/do.sh

+9
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ case $1 in
4848

4949
"docker-sdk")
5050
docker-compose -f docker-compose-dev.yml run --rm node_sdk /bin/bash
51+
;;
52+
53+
"generate-swagger")
54+
docker compose -f docker-compose-dev.yml up ae_mdw -d
55+
docker compose -f docker-compose-dev.yml exec ae_mdw bash -c "mkdir -p /app/swagger_v3; cp -f /home/aeternity/node/local/lib/aehttp-*/priv/oas3.yaml /app/swagger_v3/node_oas3.yaml"
56+
docker compose -f docker-compose-dev.yml cp ae_mdw:/app/swagger_v3/ docs/
57+
docker compose -f docker-compose-dev.yml down ae_mdw
58+
scripts/swagger-docs.py
59+
;;
5160
esac

scripts/swagger-docs.py

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
from glob import glob
77
from pathlib import Path
8+
import re
89

910
SWAGGER_DOCS_DIR = 'docs/swagger_v3/'
1011
MDW_VERSION_FILE = 'AEMDW_VERSION'
@@ -14,6 +15,7 @@
1415
# Read YAML file
1516
schemas = {}
1617
paths = {}
18+
node_schemas = None
1719
swagger = None
1820
mdw_version = None
1921

@@ -23,6 +25,10 @@
2325
with open(MDW_VERSION_FILE) as mdw_version_file:
2426
mdw_version = mdw_version_file.read().strip()
2527

28+
with open(os.path.join(SWAGGER_DOCS_DIR, 'node_oas3.yaml')) as node_stream:
29+
node_oas3 = yaml.safe_load(node_stream)
30+
node_schemas = node_oas3['components']['schemas']
31+
2632
for filepath in Path(SWAGGER_DOCS_DIR).glob("*.spec.yaml"):
2733
with open(filepath, 'r') as stream:
2834
data_loaded = yaml.safe_load(stream)
@@ -36,6 +42,28 @@
3642
swagger['info']['version'] = mdw_version
3743
swagger['servers'][0]['url'] = PATH_PREFIX
3844

45+
old_swagger_schema_len = len(swagger['components']['schemas'])
46+
47+
while True:
48+
swagger_str = json.dumps(swagger, indent=2)
49+
cleaned_refs = re.findall(r'(?m)"\$ref": "#/components/schemas/(\w+)"', swagger_str)
50+
51+
missing_refs = []
52+
for ref in cleaned_refs:
53+
if ref not in swagger['components']['schemas']:
54+
missing_refs.append(ref)
55+
56+
if missing_refs == []:
57+
break
58+
59+
for missing_ref in missing_refs:
60+
node_schema = node_schemas[missing_ref]
61+
swagger['components']['schemas'][missing_ref] = node_schema
62+
63+
swagger_schema_len = len(swagger['components']['schemas'])
64+
65+
print(f"Added {swagger_schema_len - old_swagger_schema_len} missing schemas")
66+
3967
with open(os.path.join(SWAGGER_OUTPUT_DIR, "swagger_v3.json"), 'w') as jsonfile:
4068
json.dump(swagger, jsonfile, indent=2)
4169

0 commit comments

Comments
 (0)