-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathspec_more_export.go
157 lines (148 loc) · 4.23 KB
/
spec_more_export.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
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
package openapi3
import (
"errors"
"fmt"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/net/http/pathmethod"
)
// ExportByTags creates individual specs by tag.
func (sm *SpecMore) ExportByTags() (map[string]*Spec, error) {
specs := map[string]*Spec{}
if sm.Spec == nil {
return specs, ErrSpecNotSet
}
tags := sm.Tags(&TagsOpts{InclDefs: false, InclOps: true})
for _, tag := range tags {
tagSpec, err := sm.ExportByTag(tag)
if err != nil {
return specs, errorsutil.Wrapf(err, "error SpecMore.ExportByTag(\"%s\")", tag)
}
if tagSpec != nil {
specs[tag] = tagSpec
}
}
return specs, nil
}
// ExportByTag creates an individual specs for one tag.
func (sm *SpecMore) ExportByTag(tag string) (*Spec, error) {
if (sm.Spec) == nil {
return nil, ErrSpecNotSet
}
oms := sm.OperationMetas([]string{tag})
if len(oms) == 0 {
return nil, nil
}
tagSpec := &Spec{
Components: &oas3.Components{
// ExtensionProps: sm.Spec.Components.ExtensionProps,
Extensions: sm.Spec.Components.Extensions,
SecuritySchemes: sm.Spec.Components.SecuritySchemes,
},
Info: sm.Spec.Info,
Servers: sm.Spec.Servers,
OpenAPI: sm.Spec.OpenAPI,
}
for _, om := range oms {
op, err := sm.OperationByPathMethod(om.Path, om.Method)
if err != nil {
return nil, errorsutil.Wrapf(err, "error `OperationByPathMethod` pathmethod: (%s)", pathmethod.PathMethod(om.Path, om.Method))
} else if op == nil {
continue
}
tagSpec.AddOperation(om.Path, om.Method, op)
err = sm.SchemasCopyOperation(tagSpec, op)
if err != nil {
return nil, errorsutil.Wrapf(err, "error `SpecMore.SchemasCopyOperation()` tag: (%s)", tag)
}
}
tagSm := SpecMore{Spec: tagSpec}
return tagSm.Clone()
}
var ErrJSONPointerNotParamOrSchema = errors.New("pointer is not components/parameters or components/schemas")
func (sm *SpecMore) SchemasCopyOperation(destSpec *Spec, op *oas3.Operation) error {
if sm.Spec == nil || destSpec == nil || op == nil {
return errors.New("source spec, dest spec, op cannot be nil")
}
omr := OperationMore{Operation: op}
refs := omr.JSONPointers()
for refJSONPointer := range refs {
ptr, err := ParseJSONPointer(refJSONPointer)
if err != nil {
return errorsutil.Wrapf(err, "error ParseJSONPointer() jsonpointer: (%s)", refJSONPointer)
}
paramName, isParam := ptr.IsTopParameter()
if isParam {
if paramRef, ok := sm.Spec.Components.Parameters[paramName]; ok {
if destSpec.Components.Parameters == nil {
destSpec.Components.Parameters = oas3.ParametersMap{}
}
destSpec.Components.Parameters[paramName] = paramRef
}
}
_, isSchema := ptr.IsTopSchema()
if isSchema {
err := sm.SchemasCopyJSONPointer(destSpec, refJSONPointer)
if err != nil {
return err
}
}
if !isParam && !isSchema {
return errorsutil.Wrapf(ErrJSONPointerNotParamOrSchema, "jsonpointer: (%s)", refJSONPointer)
}
}
return nil
}
func (sm *SpecMore) SchemasCopySchemaRef(destSpec *Spec, schRef *oas3.SchemaRef) error {
if sm.Spec == nil || destSpec == nil || schRef == nil {
return nil
}
if len(strings.TrimSpace(schRef.Ref)) > 0 {
err := sm.SchemasCopyJSONPointer(destSpec, schRef.Ref)
if err != nil {
return err
}
}
if schRef.Value == nil {
return nil
}
if schRef.Value.Items != nil {
err := sm.SchemasCopySchemaRef(destSpec, schRef.Value.Items)
if err != nil {
return err
}
}
for _, schRefProp := range schRef.Value.Properties {
err := sm.SchemasCopySchemaRef(destSpec, schRefProp)
if err != nil {
return err
}
}
return nil
}
func (sm *SpecMore) SchemasCopyJSONPointer(destSpec *Spec, jsonPointer string) error {
ptr, err := ParseJSONPointer(jsonPointer)
if err != nil {
return err
}
schName, ok := ptr.IsTopSchema()
if !ok {
return errors.New("json pointer is not schema pointer")
}
destSM := SpecMore{Spec: destSpec}
destSchRef := destSM.SchemaRef(schName)
if destSchRef != nil { // already present.
return nil
}
srcSchRef := sm.SchemaRef(schName)
if srcSchRef == nil {
return fmt.Errorf("json pointer not found [%s][%s]", jsonPointer, schName)
}
err = destSM.SchemaRefSet(schName, srcSchRef)
if err != nil {
return err
}
// Check recursive.
return sm.SchemasCopySchemaRef(destSpec, srcSchRef)
}