-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi.v
More file actions
1823 lines (1632 loc) · 52.5 KB
/
openapi.v
File metadata and controls
1823 lines (1632 loc) · 52.5 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// openapi.v - OpenAPI 3.0/3.1 规范数据结构
// 本模块提供 OpenAPI 文档的数据结构定义,支持 OpenAPI 3.0.x 和 3.1.x 规范
module hono_docs
import meiseayoung.hono
import x.json2
// ============================================================================
// OpenAPI 基础结构体 (Task 1.1)
// ============================================================================
// OpenAPIContact - 联系信息
pub struct OpenAPIContact {
pub mut:
name string
url string
email string
}
// OpenAPILicense - 许可证信息
pub struct OpenAPILicense {
pub mut:
name string // 必需
url string
}
// OpenAPIInfo - API 基本信息
pub struct OpenAPIInfo {
pub mut:
title string // 必需
version string // 必需
description string
terms_of_service string
contact OpenAPIContact
license OpenAPILicense
}
// OpenAPIServer - 服务器信息
pub struct OpenAPIServer {
pub mut:
url string // 必需
description string
}
// OpenAPIExternalDocs - 外部文档
pub struct OpenAPIExternalDocs {
pub mut:
url string // 必需
description string
}
// OpenAPITag - 标签定义
pub struct OpenAPITag {
pub mut:
name string // 必需
description string
external_docs OpenAPIExternalDocs
}
// ============================================================================
// OpenAPI 路径和操作结构体 (Task 1.2)
// ============================================================================
// OpenAPIMediaType - 媒体类型
pub struct OpenAPIMediaType {
pub mut:
schema OpenAPISchema
example string
}
// OpenAPIHeader - 响应头
pub struct OpenAPIHeader {
pub mut:
description string
required bool
schema OpenAPISchema
}
// OpenAPIResponse - 响应定义
pub struct OpenAPIResponse {
pub mut:
description string // 必需
headers map[string]OpenAPIHeader
content map[string]OpenAPIMediaType
}
// OpenAPIRequestBody - 请求体定义
pub struct OpenAPIRequestBody {
pub mut:
description string
content map[string]OpenAPIMediaType // 必需
required bool
}
// OpenAPIParameter - 参数定义
pub struct OpenAPIParameter {
pub mut:
name string // 必需
in_location string // 必需: "path", "query", "header", "cookie"
description string
required bool
deprecated bool
schema OpenAPISchema
example string
}
// OpenAPIOperation - 操作定义
pub struct OpenAPIOperation {
pub mut:
summary string
description string
operation_id string
tags []string
parameters []OpenAPIParameter
request_body OpenAPIRequestBody
responses map[string]OpenAPIResponse // 必需
deprecated bool
security []map[string][]string
}
// OpenAPIPathItem - 路径项
pub struct OpenAPIPathItem {
pub mut:
get OpenAPIOperation
post OpenAPIOperation
put OpenAPIOperation
delete OpenAPIOperation
patch OpenAPIOperation
head OpenAPIOperation
options OpenAPIOperation
summary string
description string
parameters []OpenAPIParameter
}
// ============================================================================
// OpenAPI Schema 和 Components 结构体 (Task 1.3)
// ============================================================================
// OpenAPISchema - JSON Schema 定义
pub struct OpenAPISchema {
pub mut:
schema_type string // "string", "integer", "number", "boolean", "array", "object"
format string // "int32", "int64", "float", "double", "date", "date-time", etc.
title string
description string
default_val string
example string
enum_values []string
required []string
properties map[string]OpenAPISchema
items &OpenAPISchema = unsafe { nil } // for array type
minimum f64
maximum f64
min_length int
max_length int
pattern string
nullable bool
read_only bool
write_only bool
ref string // $ref 引用
}
// OpenAPISecurityScheme - 安全方案
pub struct OpenAPISecurityScheme {
pub mut:
scheme_type string // "apiKey", "http", "oauth2", "openIdConnect"
description string
name string // for apiKey
in_location string // for apiKey: "query", "header", "cookie"
scheme string // for http: "bearer", "basic"
bearer_format string // for http bearer
}
// OpenAPIComponents - 可重用组件
pub struct OpenAPIComponents {
pub mut:
schemas map[string]OpenAPISchema
responses map[string]OpenAPIResponse
parameters map[string]OpenAPIParameter
request_bodies map[string]OpenAPIRequestBody
headers map[string]OpenAPIHeader
security_schemes map[string]OpenAPISecurityScheme
}
// OpenAPIDocument - OpenAPI 文档主结构
pub struct OpenAPIDocument {
pub mut:
openapi string // 必需: "3.0.0" 或 "3.1.0"
info OpenAPIInfo // 必需
servers []OpenAPIServer
paths map[string]OpenAPIPathItem // 必需
components OpenAPIComponents
security []map[string][]string
tags []OpenAPITag
external_docs OpenAPIExternalDocs
}
// ============================================================================
// OpenAPI 文档序列化 (Task 2.1)
// ============================================================================
// Helper function to check if a string is empty
fn is_empty(s string) bool {
return s.len == 0
}
// Helper function to check if a map is empty
fn is_map_empty[K, V](m map[K]V) bool {
return m.len == 0
}
// Helper function to check if an array is empty
fn is_array_empty[T](arr []T) bool {
return arr.len == 0
}
// to_json - 将 OpenAPIContact 序列化为 JSON
pub fn (c OpenAPIContact) to_json() json2.Any {
mut obj := map[string]json2.Any{}
if !is_empty(c.name) {
obj['name'] = json2.Any(c.name)
}
if !is_empty(c.url) {
obj['url'] = json2.Any(c.url)
}
if !is_empty(c.email) {
obj['email'] = json2.Any(c.email)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPILicense 序列化为 JSON
pub fn (l OpenAPILicense) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// name is required
obj['name'] = json2.Any(l.name)
if !is_empty(l.url) {
obj['url'] = json2.Any(l.url)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIInfo 序列化为 JSON
pub fn (i OpenAPIInfo) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// title and version are required
obj['title'] = json2.Any(i.title)
obj['version'] = json2.Any(i.version)
if !is_empty(i.description) {
obj['description'] = json2.Any(i.description)
}
if !is_empty(i.terms_of_service) {
obj['termsOfService'] = json2.Any(i.terms_of_service)
}
// Only include contact if it has any non-empty fields
if !is_empty(i.contact.name) || !is_empty(i.contact.url) || !is_empty(i.contact.email) {
obj['contact'] = i.contact.to_json()
}
// Only include license if name is set (required field)
if !is_empty(i.license.name) {
obj['license'] = i.license.to_json()
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIServer 序列化为 JSON
pub fn (s OpenAPIServer) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// url is required
obj['url'] = json2.Any(s.url)
if !is_empty(s.description) {
obj['description'] = json2.Any(s.description)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIExternalDocs 序列化为 JSON
pub fn (e OpenAPIExternalDocs) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// url is required
obj['url'] = json2.Any(e.url)
if !is_empty(e.description) {
obj['description'] = json2.Any(e.description)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPITag 序列化为 JSON
pub fn (t OpenAPITag) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// name is required
obj['name'] = json2.Any(t.name)
if !is_empty(t.description) {
obj['description'] = json2.Any(t.description)
}
if !is_empty(t.external_docs.url) {
obj['externalDocs'] = t.external_docs.to_json()
}
return json2.Any(obj)
}
// to_json - 将 OpenAPISchema 序列化为 JSON
pub fn (s OpenAPISchema) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// Handle $ref - if ref is set, only output $ref
if !is_empty(s.ref) {
obj[r'$ref'] = json2.Any(s.ref)
return json2.Any(obj)
}
// schema_type -> type
if !is_empty(s.schema_type) {
obj['type'] = json2.Any(s.schema_type)
}
if !is_empty(s.format) {
obj['format'] = json2.Any(s.format)
}
if !is_empty(s.title) {
obj['title'] = json2.Any(s.title)
}
if !is_empty(s.description) {
obj['description'] = json2.Any(s.description)
}
if !is_empty(s.default_val) {
obj['default'] = json2.Any(s.default_val)
}
if !is_empty(s.example) {
obj['example'] = json2.Any(s.example)
}
// enum_values -> enum
if !is_array_empty(s.enum_values) {
mut enum_arr := []json2.Any{}
for v in s.enum_values {
enum_arr << json2.Any(v)
}
obj['enum'] = json2.Any(enum_arr)
}
if !is_array_empty(s.required) {
mut req_arr := []json2.Any{}
for r in s.required {
req_arr << json2.Any(r)
}
obj['required'] = json2.Any(req_arr)
}
if !is_map_empty(s.properties) {
mut props := map[string]json2.Any{}
for key, val in s.properties {
props[key] = val.to_json()
}
obj['properties'] = json2.Any(props)
}
// items for array type
if s.items != unsafe { nil } {
obj['items'] = s.items.to_json()
}
if s.minimum != 0 {
obj['minimum'] = json2.Any(s.minimum)
}
if s.maximum != 0 {
obj['maximum'] = json2.Any(s.maximum)
}
if s.min_length != 0 {
obj['minLength'] = json2.Any(s.min_length)
}
if s.max_length != 0 {
obj['maxLength'] = json2.Any(s.max_length)
}
if !is_empty(s.pattern) {
obj['pattern'] = json2.Any(s.pattern)
}
if s.nullable {
obj['nullable'] = json2.Any(s.nullable)
}
if s.read_only {
obj['readOnly'] = json2.Any(s.read_only)
}
if s.write_only {
obj['writeOnly'] = json2.Any(s.write_only)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIHeader 序列化为 JSON
pub fn (h OpenAPIHeader) to_json() json2.Any {
mut obj := map[string]json2.Any{}
if !is_empty(h.description) {
obj['description'] = json2.Any(h.description)
}
if h.required {
obj['required'] = json2.Any(h.required)
}
// Only include schema if it has content
if !is_empty(h.schema.schema_type) || !is_empty(h.schema.ref) {
obj['schema'] = h.schema.to_json()
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIMediaType 序列化为 JSON
pub fn (m OpenAPIMediaType) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// Only include schema if it has content
if !is_empty(m.schema.schema_type) || !is_empty(m.schema.ref) || !is_map_empty(m.schema.properties) {
obj['schema'] = m.schema.to_json()
}
if !is_empty(m.example) {
obj['example'] = json2.Any(m.example)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIResponse 序列化为 JSON
pub fn (r OpenAPIResponse) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// description is required
obj['description'] = json2.Any(r.description)
if !is_map_empty(r.headers) {
mut headers := map[string]json2.Any{}
for key, val in r.headers {
headers[key] = val.to_json()
}
obj['headers'] = json2.Any(headers)
}
if !is_map_empty(r.content) {
mut content := map[string]json2.Any{}
for key, val in r.content {
content[key] = val.to_json()
}
obj['content'] = json2.Any(content)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIRequestBody 序列化为 JSON
pub fn (rb OpenAPIRequestBody) to_json() json2.Any {
mut obj := map[string]json2.Any{}
if !is_empty(rb.description) {
obj['description'] = json2.Any(rb.description)
}
// content is required
if !is_map_empty(rb.content) {
mut content := map[string]json2.Any{}
for key, val in rb.content {
content[key] = val.to_json()
}
obj['content'] = json2.Any(content)
}
if rb.required {
obj['required'] = json2.Any(rb.required)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIParameter 序列化为 JSON
pub fn (p OpenAPIParameter) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// name is required
obj['name'] = json2.Any(p.name)
// in_location -> in (required)
obj['in'] = json2.Any(p.in_location)
if !is_empty(p.description) {
obj['description'] = json2.Any(p.description)
}
if p.required {
obj['required'] = json2.Any(p.required)
}
if p.deprecated {
obj['deprecated'] = json2.Any(p.deprecated)
}
// Only include schema if it has content
if !is_empty(p.schema.schema_type) || !is_empty(p.schema.ref) {
obj['schema'] = p.schema.to_json()
}
if !is_empty(p.example) {
obj['example'] = json2.Any(p.example)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIOperation 序列化为 JSON
pub fn (op OpenAPIOperation) to_json() json2.Any {
mut obj := map[string]json2.Any{}
if !is_empty(op.summary) {
obj['summary'] = json2.Any(op.summary)
}
if !is_empty(op.description) {
obj['description'] = json2.Any(op.description)
}
if !is_empty(op.operation_id) {
obj['operationId'] = json2.Any(op.operation_id)
}
if !is_array_empty(op.tags) {
mut tags_arr := []json2.Any{}
for t in op.tags {
tags_arr << json2.Any(t)
}
obj['tags'] = json2.Any(tags_arr)
}
if !is_array_empty(op.parameters) {
mut params_arr := []json2.Any{}
for p in op.parameters {
params_arr << p.to_json()
}
obj['parameters'] = json2.Any(params_arr)
}
// Only include request_body if it has content
if !is_map_empty(op.request_body.content) {
obj['requestBody'] = op.request_body.to_json()
}
// responses is required
if !is_map_empty(op.responses) {
mut responses := map[string]json2.Any{}
for key, val in op.responses {
responses[key] = val.to_json()
}
obj['responses'] = json2.Any(responses)
}
if op.deprecated {
obj['deprecated'] = json2.Any(op.deprecated)
}
if !is_array_empty(op.security) {
mut sec_arr := []json2.Any{}
for sec in op.security {
mut sec_obj := map[string]json2.Any{}
for key, val in sec {
mut scopes := []json2.Any{}
for scope in val {
scopes << json2.Any(scope)
}
sec_obj[key] = json2.Any(scopes)
}
sec_arr << json2.Any(sec_obj)
}
obj['security'] = json2.Any(sec_arr)
}
return json2.Any(obj)
}
// Helper to check if an operation is empty (has no responses)
fn is_operation_empty(op OpenAPIOperation) bool {
return is_map_empty(op.responses)
}
// to_json - 将 OpenAPIPathItem 序列化为 JSON
pub fn (pi OpenAPIPathItem) to_json() json2.Any {
mut obj := map[string]json2.Any{}
if !is_empty(pi.summary) {
obj['summary'] = json2.Any(pi.summary)
}
if !is_empty(pi.description) {
obj['description'] = json2.Any(pi.description)
}
if !is_array_empty(pi.parameters) {
mut params_arr := []json2.Any{}
for p in pi.parameters {
params_arr << p.to_json()
}
obj['parameters'] = json2.Any(params_arr)
}
// HTTP methods - only include if they have responses
if !is_operation_empty(pi.get) {
obj['get'] = pi.get.to_json()
}
if !is_operation_empty(pi.post) {
obj['post'] = pi.post.to_json()
}
if !is_operation_empty(pi.put) {
obj['put'] = pi.put.to_json()
}
if !is_operation_empty(pi.delete) {
obj['delete'] = pi.delete.to_json()
}
if !is_operation_empty(pi.patch) {
obj['patch'] = pi.patch.to_json()
}
if !is_operation_empty(pi.head) {
obj['head'] = pi.head.to_json()
}
if !is_operation_empty(pi.options) {
obj['options'] = pi.options.to_json()
}
return json2.Any(obj)
}
// to_json - 将 OpenAPISecurityScheme 序列化为 JSON
pub fn (ss OpenAPISecurityScheme) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// scheme_type -> type
if !is_empty(ss.scheme_type) {
obj['type'] = json2.Any(ss.scheme_type)
}
if !is_empty(ss.description) {
obj['description'] = json2.Any(ss.description)
}
if !is_empty(ss.name) {
obj['name'] = json2.Any(ss.name)
}
// in_location -> in
if !is_empty(ss.in_location) {
obj['in'] = json2.Any(ss.in_location)
}
if !is_empty(ss.scheme) {
obj['scheme'] = json2.Any(ss.scheme)
}
if !is_empty(ss.bearer_format) {
obj['bearerFormat'] = json2.Any(ss.bearer_format)
}
return json2.Any(obj)
}
// Helper to check if components is empty
fn is_components_empty(c OpenAPIComponents) bool {
return is_map_empty(c.schemas) && is_map_empty(c.responses) &&
is_map_empty(c.parameters) && is_map_empty(c.request_bodies) &&
is_map_empty(c.headers) && is_map_empty(c.security_schemes)
}
// to_json - 将 OpenAPIComponents 序列化为 JSON
pub fn (c OpenAPIComponents) to_json() json2.Any {
mut obj := map[string]json2.Any{}
if !is_map_empty(c.schemas) {
mut schemas := map[string]json2.Any{}
for key, val in c.schemas {
schemas[key] = val.to_json()
}
obj['schemas'] = json2.Any(schemas)
}
if !is_map_empty(c.responses) {
mut responses := map[string]json2.Any{}
for key, val in c.responses {
responses[key] = val.to_json()
}
obj['responses'] = json2.Any(responses)
}
if !is_map_empty(c.parameters) {
mut parameters := map[string]json2.Any{}
for key, val in c.parameters {
parameters[key] = val.to_json()
}
obj['parameters'] = json2.Any(parameters)
}
if !is_map_empty(c.request_bodies) {
mut request_bodies := map[string]json2.Any{}
for key, val in c.request_bodies {
request_bodies[key] = val.to_json()
}
obj['requestBodies'] = json2.Any(request_bodies)
}
if !is_map_empty(c.headers) {
mut headers := map[string]json2.Any{}
for key, val in c.headers {
headers[key] = val.to_json()
}
obj['headers'] = json2.Any(headers)
}
if !is_map_empty(c.security_schemes) {
mut security_schemes := map[string]json2.Any{}
for key, val in c.security_schemes {
security_schemes[key] = val.to_json()
}
obj['securitySchemes'] = json2.Any(security_schemes)
}
return json2.Any(obj)
}
// to_json - 将 OpenAPIDocument 序列化为 JSON Any 对象
pub fn (doc OpenAPIDocument) to_json() json2.Any {
mut obj := map[string]json2.Any{}
// openapi is required
obj['openapi'] = json2.Any(doc.openapi)
// info is required
obj['info'] = doc.info.to_json()
// servers (optional)
if !is_array_empty(doc.servers) {
mut servers_arr := []json2.Any{}
for s in doc.servers {
servers_arr << s.to_json()
}
obj['servers'] = json2.Any(servers_arr)
}
// paths is required
mut paths := map[string]json2.Any{}
for key, val in doc.paths {
paths[key] = val.to_json()
}
obj['paths'] = json2.Any(paths)
// components (optional)
if !is_components_empty(doc.components) {
obj['components'] = doc.components.to_json()
}
// security (optional)
if !is_array_empty(doc.security) {
mut sec_arr := []json2.Any{}
for sec in doc.security {
mut sec_obj := map[string]json2.Any{}
for key, val in sec {
mut scopes := []json2.Any{}
for scope in val {
scopes << json2.Any(scope)
}
sec_obj[key] = json2.Any(scopes)
}
sec_arr << json2.Any(sec_obj)
}
obj['security'] = json2.Any(sec_arr)
}
// tags (optional)
if !is_array_empty(doc.tags) {
mut tags_arr := []json2.Any{}
for t in doc.tags {
tags_arr << t.to_json()
}
obj['tags'] = json2.Any(tags_arr)
}
// external_docs (optional)
if !is_empty(doc.external_docs.url) {
obj['externalDocs'] = doc.external_docs.to_json()
}
return json2.Any(obj)
}
// to_json_str - 将 OpenAPIDocument 序列化为 JSON 字符串
pub fn (doc OpenAPIDocument) to_json_str() string {
return doc.to_json().str()
}
// to_json_pretty - 将 OpenAPIDocument 序列化为格式化的 JSON 字符串
pub fn (doc OpenAPIDocument) to_json_pretty() string {
return json2.encode(doc.to_json(), prettify: true)
}
// ============================================================================
// OpenAPI 文档反序列化 (Task 2.3)
// ============================================================================
// from_json - 从 JSON Any 对象反序列化 OpenAPIContact
pub fn OpenAPIContact.from_json(j json2.Any) OpenAPIContact {
obj := j.as_map()
return OpenAPIContact{
name: if 'name' in obj { obj['name'] or { json2.Any('') }.str() } else { '' }
url: if 'url' in obj { obj['url'] or { json2.Any('') }.str() } else { '' }
email: if 'email' in obj { obj['email'] or { json2.Any('') }.str() } else { '' }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPILicense
pub fn OpenAPILicense.from_json(j json2.Any) OpenAPILicense {
obj := j.as_map()
return OpenAPILicense{
name: if 'name' in obj { obj['name'] or { json2.Any('') }.str() } else { '' }
url: if 'url' in obj { obj['url'] or { json2.Any('') }.str() } else { '' }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIInfo
pub fn OpenAPIInfo.from_json(j json2.Any) OpenAPIInfo {
obj := j.as_map()
return OpenAPIInfo{
title: if 'title' in obj { obj['title'] or { json2.Any('') }.str() } else { '' }
version: if 'version' in obj { obj['version'] or { json2.Any('') }.str() } else { '' }
description: if 'description' in obj { obj['description'] or { json2.Any('') }.str() } else { '' }
terms_of_service: if 'termsOfService' in obj { obj['termsOfService'] or { json2.Any('') }.str() } else { '' }
contact: if 'contact' in obj { OpenAPIContact.from_json(obj['contact'] or { json2.Any('') }) } else { OpenAPIContact{} }
license: if 'license' in obj { OpenAPILicense.from_json(obj['license'] or { json2.Any('') }) } else { OpenAPILicense{} }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIServer
pub fn OpenAPIServer.from_json(j json2.Any) OpenAPIServer {
obj := j.as_map()
return OpenAPIServer{
url: if 'url' in obj { obj['url'] or { json2.Any('') }.str() } else { '' }
description: if 'description' in obj { obj['description'] or { json2.Any('') }.str() } else { '' }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIExternalDocs
pub fn OpenAPIExternalDocs.from_json(j json2.Any) OpenAPIExternalDocs {
obj := j.as_map()
return OpenAPIExternalDocs{
url: if 'url' in obj { obj['url'] or { json2.Any('') }.str() } else { '' }
description: if 'description' in obj { obj['description'] or { json2.Any('') }.str() } else { '' }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPITag
pub fn OpenAPITag.from_json(j json2.Any) OpenAPITag {
obj := j.as_map()
return OpenAPITag{
name: if 'name' in obj { obj['name'] or { json2.Any('') }.str() } else { '' }
description: if 'description' in obj { obj['description'] or { json2.Any('') }.str() } else { '' }
external_docs: if 'externalDocs' in obj { OpenAPIExternalDocs.from_json(obj['externalDocs'] or { json2.Any('') }) } else { OpenAPIExternalDocs{} }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPISchema
pub fn OpenAPISchema.from_json(j json2.Any) OpenAPISchema {
return openapi_schema_from_json_impl(j)
}
// Helper function to parse OpenAPISchema from JSON
fn openapi_schema_from_json_impl(j json2.Any) OpenAPISchema {
obj := j.as_map()
// Parse properties map
mut props := map[string]OpenAPISchema{}
if 'properties' in obj {
props_obj := (obj['properties'] or { json2.Any('') }).as_map()
for key, val in props_obj {
props[key] = openapi_schema_from_json_impl(val)
}
}
// Parse enum values
mut enum_vals := []string{}
if 'enum' in obj {
for e in (obj['enum'] or { json2.Any('') }).arr() {
enum_vals << e.str()
}
}
// Parse required array
mut required := []string{}
if 'required' in obj {
for r in (obj['required'] or { json2.Any('') }).arr() {
required << r.str()
}
}
mut schema := OpenAPISchema{
schema_type: if 'type' in obj { (obj['type'] or { json2.Any('') }).str() } else { '' }
format: if 'format' in obj { (obj['format'] or { json2.Any('') }).str() } else { '' }
title: if 'title' in obj { (obj['title'] or { json2.Any('') }).str() } else { '' }
description: if 'description' in obj { (obj['description'] or { json2.Any('') }).str() } else { '' }
default_val: if 'default' in obj { (obj['default'] or { json2.Any('') }).str() } else { '' }
example: if 'example' in obj { (obj['example'] or { json2.Any('') }).str() } else { '' }
enum_values: enum_vals
required: required
properties: props
minimum: if 'minimum' in obj { (obj['minimum'] or { json2.Any('') }).f64() } else { 0 }
maximum: if 'maximum' in obj { (obj['maximum'] or { json2.Any('') }).f64() } else { 0 }
min_length: if 'minLength' in obj { (obj['minLength'] or { json2.Any('') }).int() } else { 0 }
max_length: if 'maxLength' in obj { (obj['maxLength'] or { json2.Any('') }).int() } else { 0 }
pattern: if 'pattern' in obj { (obj['pattern'] or { json2.Any('') }).str() } else { '' }
nullable: if 'nullable' in obj { (obj['nullable'] or { json2.Any('') }).bool() } else { false }
read_only: if 'readOnly' in obj { (obj['readOnly'] or { json2.Any('') }).bool() } else { false }
write_only: if 'writeOnly' in obj { (obj['writeOnly'] or { json2.Any('') }).bool() } else { false }
ref: if r'$ref' in obj { (obj[r'$ref'] or { json2.Any('') }).str() } else { '' }
}
// Parse items (for array type) - handle pointer separately
if 'items' in obj {
items_schema := openapi_schema_from_json_impl(obj['items'] or { json2.Any('') })
schema.items = &items_schema
}
return schema
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIHeader
pub fn OpenAPIHeader.from_json(j json2.Any) OpenAPIHeader {
obj := j.as_map()
return OpenAPIHeader{
description: if 'description' in obj { (obj['description'] or { json2.Any('') }).str() } else { '' }
required: if 'required' in obj { (obj['required'] or { json2.Any('') }).bool() } else { false }
schema: if 'schema' in obj { OpenAPISchema.from_json(obj['schema'] or { json2.Any('') }) } else { OpenAPISchema{} }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIMediaType
pub fn OpenAPIMediaType.from_json(j json2.Any) OpenAPIMediaType {
obj := j.as_map()
return OpenAPIMediaType{
schema: if 'schema' in obj { OpenAPISchema.from_json(obj['schema'] or { json2.Any('') }) } else { OpenAPISchema{} }
example: if 'example' in obj { (obj['example'] or { json2.Any('') }).str() } else { '' }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIResponse
pub fn OpenAPIResponse.from_json(j json2.Any) OpenAPIResponse {
obj := j.as_map()
// Parse headers map
mut headers := map[string]OpenAPIHeader{}
if 'headers' in obj {
headers_obj := (obj['headers'] or { json2.Any('') }).as_map()
for key, val in headers_obj {
headers[key] = OpenAPIHeader.from_json(val)
}
}
// Parse content map
mut content := map[string]OpenAPIMediaType{}
if 'content' in obj {
content_obj := (obj['content'] or { json2.Any('') }).as_map()
for key, val in content_obj {
content[key] = OpenAPIMediaType.from_json(val)
}
}
return OpenAPIResponse{
description: if 'description' in obj { (obj['description'] or { json2.Any('') }).str() } else { '' }
headers: headers
content: content
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIRequestBody
pub fn OpenAPIRequestBody.from_json(j json2.Any) OpenAPIRequestBody {
obj := j.as_map()
// Parse content map
mut content := map[string]OpenAPIMediaType{}
if 'content' in obj {
content_obj := (obj['content'] or { json2.Any('') }).as_map()
for key, val in content_obj {
content[key] = OpenAPIMediaType.from_json(val)
}
}
return OpenAPIRequestBody{
description: if 'description' in obj { (obj['description'] or { json2.Any('') }).str() } else { '' }
content: content
required: if 'required' in obj { (obj['required'] or { json2.Any('') }).bool() } else { false }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIParameter
pub fn OpenAPIParameter.from_json(j json2.Any) OpenAPIParameter {
obj := j.as_map()
return OpenAPIParameter{
name: if 'name' in obj { (obj['name'] or { json2.Any('') }).str() } else { '' }
in_location: if 'in' in obj { (obj['in'] or { json2.Any('') }).str() } else { '' }
description: if 'description' in obj { (obj['description'] or { json2.Any('') }).str() } else { '' }
required: if 'required' in obj { (obj['required'] or { json2.Any('') }).bool() } else { false }
deprecated: if 'deprecated' in obj { (obj['deprecated'] or { json2.Any('') }).bool() } else { false }
schema: if 'schema' in obj { OpenAPISchema.from_json(obj['schema'] or { json2.Any('') }) } else { OpenAPISchema{} }
example: if 'example' in obj { (obj['example'] or { json2.Any('') }).str() } else { '' }
}
}
// from_json - 从 JSON Any 对象反序列化 OpenAPIOperation
pub fn OpenAPIOperation.from_json(j json2.Any) OpenAPIOperation {
obj := j.as_map()
// Parse tags array
mut tags := []string{}
if 'tags' in obj {
for t in (obj['tags'] or { json2.Any('') }).arr() {
tags << t.str()
}
}
// Parse parameters array
mut parameters := []OpenAPIParameter{}
if 'parameters' in obj {
for p in (obj['parameters'] or { json2.Any('') }).arr() {
parameters << OpenAPIParameter.from_json(p)
}
}
// Parse responses map
mut responses := map[string]OpenAPIResponse{}
if 'responses' in obj {
responses_obj := (obj['responses'] or { json2.Any('') }).as_map()
for key, val in responses_obj {
responses[key] = OpenAPIResponse.from_json(val)
}
}
// Parse security array
mut security := []map[string][]string{}
if 'security' in obj {
for sec in (obj['security'] or { json2.Any('') }).arr() {
mut sec_map := map[string][]string{}
sec_obj := sec.as_map()
for key, val in sec_obj {
mut scopes := []string{}
for scope in val.arr() {
scopes << scope.str()
}
sec_map[key] = scopes
}
security << sec_map
}
}
return OpenAPIOperation{
summary: if 'summary' in obj { (obj['summary'] or { json2.Any('') }).str() } else { '' }
description: if 'description' in obj { (obj['description'] or { json2.Any('') }).str() } else { '' }
operation_id: if 'operationId' in obj { (obj['operationId'] or { json2.Any('') }).str() } else { '' }
tags: tags
parameters: parameters
request_body: if 'requestBody' in obj { OpenAPIRequestBody.from_json(obj['requestBody'] or { json2.Any('') }) } else { OpenAPIRequestBody{} }