forked from oapi-codegen/oapi-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externalref.go
80 lines (66 loc) · 3.02 KB
/
externalref.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
package codegen
import (
"fmt"
"strings"
)
// ensureExternalRefsInRequestBodyDefinitions ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInRequestBodyDefinitions(defs *[]RequestBodyDefinition, ref string) {
if ref == "" {
return
}
for i, rbd := range *defs {
ensureExternalRefsInSchema(&rbd.Schema, ref)
// make sure we then update it in-place
(*defs)[i] = rbd
}
}
// ensureExternalRefsInResponseDefinitions ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInResponseDefinitions(defs *[]ResponseDefinition, ref string) {
if ref == "" {
return
}
for i, rd := range *defs {
for j, rcd := range rd.Contents {
ensureExternalRefsInSchema(&rcd.Schema, ref)
// make sure we then update it in-place
rd.Contents[j] = rcd
}
// make sure we then update it in-place
(*defs)[i] = rd
}
}
// ensureExternalRefsInParameterDefinitions ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInParameterDefinitions(defs *[]ParameterDefinition, ref string) {
if ref == "" {
return
}
for i, pd := range *defs {
ensureExternalRefsInSchema(&pd.Schema, ref)
// make sure we then update it in-place
(*defs)[i] = pd
}
}
// ensureExternalRefsInSchema ensures that when an externalRef (`$ref` that points to a file that isn't the current spec) is encountered, we make sure we update our underlying `RefType` to make sure that we point to that type.
//
// This only happens if we have a non-empty `ref` passed in, and that `ref` isn't pointing to something in our file
//
// NOTE that the pointer here allows us to pass in a reference and edit in-place
func ensureExternalRefsInSchema(schema *Schema, ref string) {
if ref == "" {
return
}
// if this is already defined as the start of a struct, we shouldn't inject **??**
if strings.HasPrefix(schema.GoType, "struct {") {
return
}
parts := strings.SplitN(ref, "#", 2)
if pack, ok := globalState.importMapping[parts[0]]; ok {
schema.RefType = fmt.Sprintf("%s.%s", pack.Name, schema.GoType)
}
}