forked from OpenBanking-Brasil/draft-openapi
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdictionary_generator
executable file
·271 lines (242 loc) · 9.26 KB
/
dictionary_generator
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env ruby
require 'yaml'
require 'pp'
require 'csv'
require 'pathname'
require 'uri'
require 'optparse'
options = {}
cli = OptionParser.new do |parser|
parser.banner = 'Usage: dictionary_generator [-c] -f swagger_file.yaml -o generated/'
parser.on('-c', '--conditional', 'Enable conditional value on mandatory column')
parser.on('-f', '--filename FILENAME', 'Yaml Swagger filename') do |f| Pathname.new f end
parser.on('-o', '--output OUTPUT', 'Output directory') do |o| Pathname.new o end
end
cli.parse!(into: options)
oas = YAML.load_file(options[:filename])
output_path = options[:output].realpath.to_s + '/'
$isRegulatoryRequiredAPI = false
headers = {
'COLUMN_1' => 'Xpath',
'COLUMN_2' => 'Nome',
'COLUMN_3' => 'Definição',
'COLUMN_4' => 'Tipo de Dado',
'COLUMN_5' => 'Tamanho',
'COLUMN_6' => 'Mandatoriedade',
'COLUMN_7' => 'Formato',
'COLUMN_8' => 'Domínio',
'COLUMN_9' => 'Mínimo de Ocorrências',
'COLUMN_10' => 'Máximo de Ocorrências',
'COLUMN_11' => 'Restrições',
'COLUMN_12' => 'Nulidade',
'COLUMN_13' => 'Tipo de Dado Json',
'COLUMN_14' => 'Exemplo',
'COLUMN_15' => 'Tamanho mínimo'
}
headersXregulatory = {
'COLUMN_1' => 'Xpath',
'COLUMN_2' => 'Nome',
'COLUMN_3' => 'Definição',
'COLUMN_4' => 'Tipo de Dado',
'COLUMN_5' => 'Tamanho',
'COLUMN_6' => 'Mandatoriedade',
'COLUMN_7' => 'Obrigatório por Regulação',
'COLUMN_8' => 'Formato',
'COLUMN_9' => 'Domínio',
'COLUMN_10' => 'Mínimo de Ocorrências',
'COLUMN_11' => 'Máximo de Ocorrências',
'COLUMN_12' => 'Restrições',
'COLUMN_13' => 'Nulidade',
'COLUMN_14' => 'Tipo de Dado Json',
'COLUMN_15' => 'Exemplo',
'COLUMN_16' => 'Tamanho mínimo'
}
def format_type(type)
return '' if !type
types = {
'array' => 'Lista',
'double' => 'Número',
'integer' => 'Inteiro',
'string' => 'Texto',
'number' => 'Número',
'object' => 'Objeto',
'boolean' => 'Booleano',
'enum' => 'Texto',
'date' => 'Data',
'date-time' => 'Date Hora'
}
return types[type.downcase] ? types[type.downcase] : 'Não Identificado!'
end
# Extract fields from Schema Object recursivily
def extract_fields(schema, path = '', fields = [], ignore = ['links', 'meta'])
if !schema['oneOf'].nil?
schema['oneOf'].each do |option|
fields = sub_extract_fields(option, path, fields, ignore)
end
return fields
else
return sub_extract_fields(schema, path, fields, ignore)
end
end
def sub_extract_fields(schema, path = '', fields = [], ignore = ['links', 'meta'])
if ['array', 'object'].include? schema['type']
index = 'properties'
if schema['type'] == 'array' and !['array', 'object'].include? schema['items']['type']
items = schema['items']
items['isRequired'] = schema['isRequired']
items['isRegulatoryRequired'] = schema['isRegulatoryRequired']
items['maxItems'] = schema['maxItems']
items['minItems'] = schema['minItems']
items['description'] = schema['description']
items['name'] = schema['name']
items['type'] = schema['type'] + "-" + items['type'];
fields.concat extract_fields(items, path)
return fields
elsif path != ''
parent = schema.clone
parent['path'] = path
if !fields.any? { |objeto| objeto['path'] == path }
fields.push parent
end
end
schema = schema['items'] if schema['type'] == 'array'
schema[index].select{|key, _| !ignore.include? key}.each_with_index do |(name, spec)|
spec['name'] = name
spec['isRequired'] = true if schema['required'] and schema['required'].include? name
spec['isRegulatoryRequired'] = true if schema['x-regulatory-required'] and schema['x-regulatory-required'].include? name
if !$isRegulatoryRequiredAPI
$isRegulatoryRequiredAPI = true if schema['x-regulatory-required'] and schema['x-regulatory-required'].include? name
end
fields.concat extract_fields(spec, path + '/' + name)
end
else
schema['path'] = path
fields.push schema
end
return fields
end
# Write headers and rows on given filename
def generate_csv(filename, headers, rows)
CSV.open(filename, 'w', col_sep: ";") do |file|
file << headers.values
rows.each do |row|
file << row.values
end
end
end
def format_enum(enum)
return enum.join(" \n") if enum != nil
end
def format_restriction(text)
return '' if !text
if text.include?('[Restrição]')
return text.partition('[Restrição]').last
elsif text.include?('[Restrições]')
return text.partition('[Restrições]').last
else
return ''
end
end
def format_definition(text)
return '' if !text
return text.match(/^\*/)[0].strip.delete('\\\"')
end
# Analyse
puts "> Starting analyse on " + options[:filename].to_s
oas['paths'].each_with_index do |(path, methods)|
methods.each_with_index do |(op, spec)|
rows = []
puts '─ ' + op.upcase + ' ' + path
puts ' ├── Extracting fields from responses...'
spec['responses'].select {|status, _| (200..299).member? status.to_i }.each_with_index do |(status, response)|
if response['content'] == nil
puts ' └── INFO: No content was found!'
elsif
applicationData = response['content'];
if response['content']['application/json'] != nil
applicationData = response['content']['application/json'];
elsif response['content']['application/jwt'] != nil
applicationData = response['content']['application/jwt'];
end
extract_fields(applicationData['schema'], '', []).each do |field|
min_items = field['minItems']
max_items = field['maxItems']
data_type = field['type']
restriction = format_restriction(field['description'])
mandatory = field['isRequired'] ? "Obrigatório" : "Opcional"
regulatoryMandatory = field['isRegulatoryRequired'] ? "Obrigatório" : "Opcional"
if ['date', 'date-time'].include? field['format']
data_type = field['format']
end
if field['type'] && field['type'].match(/array/)
data_type = 'array'
field['type'] = 'array'
end
if field['minItems'] == nil
min_items = field['isRequired'] ? 1 : 0
end
if field['type'] == 'array'
if field['maxItems'] == nil
max_items = 'N'
end
else
max_items = 1
end
if restriction != '' && options[:conditional] && !field['isRequired']
mandatory = 'Condicional'
end
if $isRegulatoryRequiredAPI
row = {
headersXregulatory['COLUMN_1'] => field['path'],
headersXregulatory['COLUMN_2'] => field['name'],
headersXregulatory['COLUMN_3'] => field['description'],
headersXregulatory['COLUMN_4'] => format_type(data_type),
headersXregulatory['COLUMN_5'] => field['maxLength'] ? field['maxLength'] : field['maximum'],
headersXregulatory['COLUMN_6'] => mandatory,
headersXregulatory['COLUMN_7'] => regulatoryMandatory,
headersXregulatory['COLUMN_8'] => field['pattern'],
headersXregulatory['COLUMN_9'] => format_enum(field['enum']),
headersXregulatory['COLUMN_10'] => min_items,
headersXregulatory['COLUMN_11'] => max_items,
headersXregulatory['COLUMN_12'] => restriction,
headersXregulatory['COLUMN_13'] => field['nullable'] ? "Permitido" : "Não permitido",
headersXregulatory['COLUMN_14'] => field['type'],
headersXregulatory['COLUMN_15'] => field['example'],
headersXregulatory['COLUMN_16'] => field['minLength'] ? field['minLength'] : field['minimum']
}
else
row = {
headers['COLUMN_1'] => field['path'],
headers['COLUMN_2'] => field['name'],
headers['COLUMN_3'] => field['description'],
headers['COLUMN_4'] => format_type(data_type),
headers['COLUMN_5'] => field['maxLength'] ? field['maxLength'] : field['maximum'],
headers['COLUMN_6'] => mandatory,
headers['COLUMN_7'] => field['pattern'],
headers['COLUMN_8'] => format_enum(field['enum']),
headers['COLUMN_9'] => min_items,
headers['COLUMN_10'] => max_items,
headers['COLUMN_11'] => restriction,
headers['COLUMN_12'] => field['nullable'] ? "Permitido" : "Não permitido",
headers['COLUMN_13'] => field['type'],
headers['COLUMN_14'] => field['example'],
headers['COLUMN_15'] => field['minLength'] ? field['minLength'] : field['minimum']
}
end
rows.push row
end
end
end
version = "v" + oas['info']['version'].split(/-/, 2).first
filename = output_path + spec['operationId'] + "_" + version + '.csv'
puts ' ├── Writing file ' + filename
if $isRegulatoryRequiredAPI
generate_csv(filename, headersXregulatory, rows)
else
generate_csv(filename, headers, rows)
end
puts " └── Finished! \n\n"
end
end
puts '> Files generated!'
exit 0