forked from NHSDigital/NRLF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger.sh
More file actions
executable file
·195 lines (178 loc) · 7.33 KB
/
swagger.sh
File metadata and controls
executable file
·195 lines (178 loc) · 7.33 KB
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
#!/bin/bash
function _swagger_help() {
echo
echo "swagger.sh <command> [options]"
echo
echo "commands:"
echo " help - this help screen"
echo " generate <options> - generate all swagger and models or producer/consumer if specified"
echo " generate-swagger <options> - generate all swagger or producer/consumer if specified"
echo " generate-model <options> - generate all models or producer/consumer if specified"
echo " merge <options> - generates the record-locator/<type>.yml file"
echo
}
function _get_generator() {
mkdir ./tools
curl https://repo1.maven.org/maven2/com/ibm/fhir/fhir-swagger-generator/4.11.1/fhir-swagger-generator-4.11.1-cli.jar --output ./tools/fhir-swagger-generator-4.11.1-cli.jar
}
function _generate_consumer_from_fhir() {
rm ./swagger/consumer.yaml
touch ./swagger/consumer.yaml
java -jar ./tools/fhir-swagger-generator-4.11.1-cli.jar "DocumentReference(read,search)"
yq -P ./openapi/DocumentReference-openapi.json > ./swagger/consumer.yaml
rm -rf ./openapi/
}
function _generate_producer_from_fhir() {
rm ./swagger/producer.yaml
touch ./swagger/producer.yaml
java -jar ./tools/fhir-swagger-generator-4.11.1-cli.jar "DocumentReference(read,search,create,update,delete)"
yq -P ./openapi/DocumentReference-openapi.json > ./swagger/producer.yaml
rm -rf ./openapi/
}
function _generate_from_fhir() {
_get_generator
_generate_consumer_from_fhir
_generate_producer_from_fhir
rm -rf ./tools/
}
function _generate_producer_model() {
if [ ! -d "./layer/nrlf/nrlf/producer/fhir/r4" ]; then
mkdir -p ./layer/nrlf/nrlf/producer/fhir/r4
fi
datamodel-codegen --input ./api/producer/swagger.yaml --input-file-type openapi --output ./layer/nrlf/nrlf/producer/fhir/r4/model.py --use-annotated --enum-field-as-literal all --use-double-quotes
datamodel-codegen --input ./api/producer/swagger.yaml --input-file-type openapi --output ./layer/nrlf/nrlf/producer/fhir/r4/strict_model.py --strict-types {str,bytes,int,float,bool} --use-annotated --enum-field-as-literal all --use-double-quotes
}
function _generate_consumer_model() {
if [ ! -d "./layer/nrlf/nrlf/consumer/fhir/r4" ]; then
mkdir -p ./layer/nrlf/nrlf/consumer/fhir/r4
fi
datamodel-codegen --input ./api/consumer/swagger.yaml --input-file-type openapi --output ./layer/nrlf/nrlf/consumer/fhir/r4/model.py --use-annotated --enum-field-as-literal all --use-double-quotes
}
function _swagger() {
local command=$1
local type=$2
case $command in
"generate")
if [[ -z "$type" ]]; then
_generate_from_fhir
(
cd scripts
python3 -c "import swagger_generator as sg; sg.entry()"
)
_generate_consumer_model
_generate_producer_model
else
_get_generator
if [ $type = 'consumer' ]; then
_generate_consumer_from_fhir
(
cd scripts
python3 -c "import swagger_generator as sg; sg.entry('$type')"
)
_generate_consumer_model
elif [ $type = 'producer' ]; then
_generate_producer_from_fhir
(
cd scripts
python3 -c "import swagger_generator as sg; sg.entry('$type')"
)
_generate_producer_model
else
rm -rf ./tools/
_nrlf_commands_help && return 1
fi
rm -rf ./tools/
fi
;;
"generate-swagger")
if [[ -z "$type" ]]; then
_generate_from_fhir
(
cd scripts
python3 -c "import swagger_generator as sg; sg.entry()"
)
else
_get_generator
if [ $type = 'consumer' ]; then
_generate_consumer_from_fhir
elif [ $type = 'producer' ]; then
_generate_producer_from_fhir
else
rm -rf ./tools/
_nrlf_commands_help && return 1
fi
rm -rf ./tools/
(
cd scripts
python3 -c "import swagger_generator as sg; sg.entry('$type')"
)
fi
;;
"generate-model")
if [[ -z "$type" ]]; then
_generate_consumer_model
_generate_producer_model
else
if [ $type = 'consumer' ]; then
_generate_consumer_model
elif [ $type = 'producer' ]; then
_generate_producer_model
else
_nrlf_commands_help && return 1
fi
fi
;;
"merge")
allowed_types="producer consumer"
local type=$2
if [[ " ${allowed_types[*]} " =~ " $2 " ]]; then
set -x
cat ./swagger/${type}.yaml |
# Remove commented lines
grep -v "^\s*#" |
# Replace snake case terms, which are invalid in ApiGateway
yq 'with(.components.schemas; with_entries(.key |= sub("_","")))' |
yq '(.. | select(has("$ref")).$ref) |= sub("_","")' |
# Remove the parts we don't want
yq 'del(.paths.*.post.requestBody.content."application/x-www-form-urlencoded")' |
yq 'del(.x-ibm-configuration)' |
yq 'del(.components.schemas.*.discriminator)' |
yq '(.. | select(style == "single")) style |= "double"' \
> ./swagger/${type}.tmp.yaml
set +x
# Merge in the narrative, and save for internal use (i.e. including status endpoint)
yq eval-all '. as $item ireduce ({}; . * $item)' \
./swagger/${type}.tmp.yaml \
./swagger/${type}-static/*.yaml \
> ./api/${type}/swagger.yaml
# Remove fields not required for public docs
# * AWS specific stuff, including security & lambdas
# * security tags
# * API catalogue dislikes tags
# * /_status not public
cat ./api/${type}/swagger.yaml |
yq 'with(.paths.*.*.responses.*.content; with_entries(.key |= . + ";version=1" ))' |
yq 'with(.components.requestBodies.*.content; with_entries(.key |= . + ";version=1" ))' |
yq 'with(.components.responses.*.content; with_entries(.key |= . + ";version=1" ))' |
yq 'del(.paths.*.*.x-amazon-apigateway-integration)' |
yq 'del(.paths.*.*.security)' |
yq 'del(.tags)' |
yq 'del(.paths.*.*.tags)' |
yq 'del(.paths./_status)' |
yq 'del(.components.securitySchemes."${authoriser_name}")' \
> ./api/${type}/record-locator/${type}.yaml
rm ./swagger/${type}.tmp.yaml
# Remove fields not valid on AWS but otherwise required in public docs
# * 4XX codes
cat ./api/${type}/swagger.yaml |
yq 'del(.. | select(has("4XX")).4XX)' \
> ./api/${type}/swagger-tmp.yaml
mv ./api/${type}/swagger-tmp.yaml ./api/${type}/swagger.yaml
else
_swagger_help
fi
;;
*) _swagger_help ;;
esac
}
_swagger "${@:1}"