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
|
package analysis
import (
"fmt"
"path"
"sort"
"strings"
"github.com/go-openapi/analysis/internal/flatten/operations"
"github.com/go-openapi/analysis/internal/flatten/replace"
"github.com/go-openapi/analysis/internal/flatten/schutils"
"github.com/go-openapi/analysis/internal/flatten/sortref"
"github.com/go-openapi/spec"
"github.com/go-openapi/swag"
)
// InlineSchemaNamer finds a new name for an inlined type
type InlineSchemaNamer struct {
Spec *spec.Swagger
Operations map[string]operations.OpRef
flattenContext *context
opts *FlattenOpts
}
// Name yields a new name for the inline schema
func (isn *InlineSchemaNamer) Name(key string, schema *spec.Schema, aschema *AnalyzedSchema) error {
debugLog("naming inlined schema at %s", key)
parts := sortref.KeyParts(key)
for _, name := range namesFromKey(parts, aschema, isn.Operations) {
if name == "" {
continue
}
// create unique name
newName, isOAIGen := uniqifyName(isn.Spec.Definitions, swag.ToJSONName(name))
// clone schema
sch := schutils.Clone(schema)
// replace values on schema
if err := replace.RewriteSchemaToRef(isn.Spec, key,
spec.MustCreateRef(path.Join(definitionsPath, newName))); err != nil {
return fmt.Errorf("error while creating definition %q from inline schema: %w", newName, err)
}
// rewrite any dependent $ref pointing to this place,
// when not already pointing to a top-level definition.
//
// NOTE: this is important if such referers use arbitrary JSON pointers.
an := New(isn.Spec)
for k, v := range an.references.allRefs {
r, erd := replace.DeepestRef(isn.opts.Swagger(), isn.opts.ExpandOpts(false), v)
if erd != nil {
return fmt.Errorf("at %s, %w", k, erd)
}
if isn.opts.flattenContext != nil {
isn.opts.flattenContext.warnings = append(isn.opts.flattenContext.warnings, r.Warnings...)
}
if r.Ref.String() != key && (r.Ref.String() != path.Join(definitionsPath, newName) || path.Dir(v.String()) == definitionsPath) {
continue
}
debugLog("found a $ref to a rewritten schema: %s points to %s", k, v.String())
// rewrite $ref to the new target
if err := replace.UpdateRef(isn.Spec, k,
spec.MustCreateRef(path.Join(definitionsPath, newName))); err != nil {
return err
}
}
// NOTE: this extension is currently not used by go-swagger (provided for information only)
sch.AddExtension("x-go-gen-location", GenLocation(parts))
// save cloned schema to definitions
schutils.Save(isn.Spec, newName, sch)
// keep track of created refs
if isn.flattenContext == nil {
continue
}
debugLog("track created ref: key=%s, newName=%s, isOAIGen=%t", key, newName, isOAIGen)
resolved := false
if _, ok := isn.flattenContext.newRefs[key]; ok {
resolved = isn.flattenContext.newRefs[key].resolved
}
isn.flattenContext.newRefs[key] = &newRef{
key: key,
newName: newName,
path: path.Join(definitionsPath, newName),
isOAIGen: isOAIGen,
resolved: resolved,
schema: sch,
}
}
return nil
}
// uniqifyName yields a unique name for a definition
func uniqifyName(definitions spec.Definitions, name string) (string, bool) {
isOAIGen := false
if name == "" {
name = "oaiGen"
isOAIGen = true
}
if len(definitions) == 0 {
return name, isOAIGen
}
unq := true
for k := range definitions {
if strings.EqualFold(k, name) {
unq = false
break
}
}
if unq {
return name, isOAIGen
}
name += "OAIGen"
isOAIGen = true
var idx int
unique := name
_, known := definitions[unique]
for known {
idx++
unique = fmt.Sprintf("%s%d", name, idx)
_, known = definitions[unique]
}
return unique, isOAIGen
}
func namesFromKey(parts sortref.SplitKey, aschema *AnalyzedSchema, operations map[string]operations.OpRef) []string {
var (
baseNames [][]string
startIndex int
)
if parts.IsOperation() {
baseNames, startIndex = namesForOperation(parts, operations)
}
// definitions
if parts.IsDefinition() {
baseNames, startIndex = namesForDefinition(parts)
}
result := make([]string, 0, len(baseNames))
for _, segments := range baseNames {
nm := parts.BuildName(segments, startIndex, partAdder(aschema))
if nm == "" {
continue
}
result = append(result, nm)
}
sort.Strings(result)
return result
}
func namesForParam(parts sortref.SplitKey, operations map[string]operations.OpRef) ([][]string, int) {
var (
baseNames [][]string
startIndex int
)
piref := parts.PathItemRef()
if piref.String() != "" && parts.IsOperationParam() {
if op, ok := operations[piref.String()]; ok {
startIndex = 5
baseNames = append(baseNames, []string{op.ID, "params", "body"})
}
} else if parts.IsSharedOperationParam() {
pref := parts.PathRef()
for k, v := range operations {
if strings.HasPrefix(k, pref.String()) {
startIndex = 4
baseNames = append(baseNames, []string{v.ID, "params", "body"})
}
}
}
return baseNames, startIndex
}
func namesForOperation(parts sortref.SplitKey, operations map[string]operations.OpRef) ([][]string, int) {
var (
baseNames [][]string
startIndex int
)
// params
if parts.IsOperationParam() || parts.IsSharedOperationParam() {
baseNames, startIndex = namesForParam(parts, operations)
}
// responses
if parts.IsOperationResponse() {
piref := parts.PathItemRef()
if piref.String() != "" {
if op, ok := operations[piref.String()]; ok {
startIndex = 6
baseNames = append(baseNames, []string{op.ID, parts.ResponseName(), "body"})
}
}
}
return baseNames, startIndex
}
func namesForDefinition(parts sortref.SplitKey) ([][]string, int) {
nm := parts.DefinitionName()
if nm != "" {
return [][]string{{parts.DefinitionName()}}, 2
}
return [][]string{}, 0
}
// partAdder knows how to interpret a schema when it comes to build a name from parts
func partAdder(aschema *AnalyzedSchema) sortref.PartAdder {
return func(part string) []string {
segments := make([]string, 0, 2)
if part == "items" || part == "additionalItems" {
if aschema.IsTuple || aschema.IsTupleWithExtra {
segments = append(segments, "tuple")
} else {
segments = append(segments, "items")
}
if part == "additionalItems" {
segments = append(segments, part)
}
return segments
}
segments = append(segments, part)
return segments
}
}
func nameFromRef(ref spec.Ref) string {
u := ref.GetURL()
if u.Fragment != "" {
return swag.ToJSONName(path.Base(u.Fragment))
}
if u.Path != "" {
bn := path.Base(u.Path)
if bn != "" && bn != "/" {
ext := path.Ext(bn)
if ext != "" {
return swag.ToJSONName(bn[:len(bn)-len(ext)])
}
return swag.ToJSONName(bn)
}
}
return swag.ToJSONName(strings.ReplaceAll(u.Host, ".", " "))
}
// GenLocation indicates from which section of the specification (models or operations) a definition has been created.
//
// This is reflected in the output spec with a "x-go-gen-location" extension. At the moment, this is is provided
// for information only.
func GenLocation(parts sortref.SplitKey) string {
switch {
case parts.IsOperation():
return "operations"
case parts.IsDefinition():
return "models"
default:
return ""
}
}
|