-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripreferences.go
125 lines (118 loc) · 3.51 KB
/
stripreferences.go
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
package main
/*************************
This all works very simply because the openapi3 toolkit tracks
(and creates and removes) references in a 2-layer ref/value
structure. A NIL ref means that this is NOT a reference; a
non-NIL reference means, create this as the specified reference.
Therefore, to 'strip' references, rather than copy & pasting
trees and maps and sub-maps into maps and trees and yet more
sub-maps, one need only clear the Ref string.
SO MUCH EASY! THANK YOU OPENAPI LIBRARY!
***************************/
import (
oas "github.com/getkin/kin-openapi/openapi3"
// https://pkg.go.dev/github.com/getkin/kin-openapi@v0.85.0/openapi3
)
// StripReferences
// Remove the reference pointer from the loaded OAS data
// thus, when the OAS data is unmarshalled back into JSON
// there are no internal references and the full data is
// written out
func StripReferences(api *oas.T) {
if nil == api {
return
}
for _, val01 := range api.Paths {
val01.Ref = ""
StripPathItem(val01)
}
}
// StripPathItem
// For a given oas.PathItem, remove the references
// within it. Since all the references occur within
// an Operation ... just clear the Operations
func StripPathItem(item *oas.PathItem) {
if nil == item {
return
}
StripOperationReferences(item.Connect)
StripOperationReferences(item.Delete)
StripOperationReferences(item.Get)
StripOperationReferences(item.Head)
StripOperationReferences(item.Options)
StripOperationReferences(item.Patch)
StripOperationReferences(item.Post)
StripOperationReferences(item.Put)
StripOperationReferences(item.Trace)
}
// StripOperationReferences
// For a given oas.Operation, remove the references
// within it. That consists of cleaning the Schema
// references and callbacks (which are PathItems)
func StripOperationReferences(op *oas.Operation) {
// todo: fix for non-Post (no requestbody) operations
if nil == op {
return
}
op.RequestBody.Ref = ""
for _, val01 := range op.RequestBody.Value.Content {
StripReferencesSchema(val01.Schema)
}
for _, val02 := range op.Responses {
val02.Ref = ""
for _, val03 := range val02.Value.Content {
StripReferencesSchema(val03.Schema)
}
}
for _, val04 := range op.Callbacks {
val04.Ref = ""
for _, val05 := range *val04.Value {
StripPathItem(val05)
}
}
}
// StripReferencesSchema Eventually, whatever reference
// paths an OAS file has comes down to schema references,
// and this recursively clears those schemas as well as
// all the schema references within
func StripReferencesSchema(schema *oas.SchemaRef) {
if nil == schema {
return
}
// This is the actual work of replacing the reference
schema.Ref = ""
schemaBody := schema.Value
// now handle all the potential sub-schemas ...
if 0 != len(schemaBody.Extensions) {
xLog.Print("WARNING: Extension Properties references are NOT handled by this program")
}
if nil != schemaBody.OneOf {
for _, val01 := range schemaBody.OneOf {
StripReferencesSchema(val01)
}
}
if nil != schemaBody.AnyOf {
for _, val02 := range schemaBody.AnyOf {
StripReferencesSchema(val02)
}
}
if nil != schemaBody.AllOf {
for _, val03 := range schemaBody.AllOf {
StripReferencesSchema(val03)
}
}
if nil != schemaBody.Not {
StripReferencesSchema(schemaBody.Not)
}
if nil != schemaBody.Items {
StripReferencesSchema(schema.Value.Items)
}
if nil != schemaBody.Properties {
for _, val04 := range schemaBody.Properties {
StripReferencesSchema(val04)
}
}
if nil != schemaBody.AdditionalProperties {
StripReferencesSchema(schemaBody.AdditionalProperties)
}
}