podman
78 строк · 2.5 Кб
1package analysis2
3import (4"log"5
6"github.com/go-openapi/spec"7)
8
9// FlattenOpts configuration for flattening a swagger specification.
10//
11// The BasePath parameter is used to locate remote relative $ref found in the specification.
12// This path is a file: it points to the location of the root document and may be either a local
13// file path or a URL.
14//
15// If none specified, relative references (e.g. "$ref": "folder/schema.yaml#/definitions/...")
16// found in the spec are searched from the current working directory.
17type FlattenOpts struct {18Spec *Spec // The analyzed spec to work with19flattenContext *context // Internal context to track flattening activity20
21BasePath string // The location of the root document for this spec to resolve relative $ref22
23// Flattening options24Expand bool // When true, skip flattening the spec and expand it instead (if Minimal is false)25Minimal bool // When true, do not decompose complex structures such as allOf26Verbose bool // enable some reporting on possible name conflicts detected27RemoveUnused bool // When true, remove unused parameters, responses and definitions after expansion/flattening28ContinueOnError bool // Continue when spec expansion issues are found29
30/* Extra keys */31_ struct{} // require keys32}
33
34// ExpandOpts creates a spec.ExpandOptions to configure expanding a specification document.
35func (f *FlattenOpts) ExpandOpts(skipSchemas bool) *spec.ExpandOptions {36return &spec.ExpandOptions{37RelativeBase: f.BasePath,38SkipSchemas: skipSchemas,39ContinueOnError: f.ContinueOnError,40}41}
42
43// Swagger gets the swagger specification for this flatten operation
44func (f *FlattenOpts) Swagger() *spec.Swagger {45return f.Spec.spec46}
47
48// croak logs notifications and warnings about valid, but possibly unwanted constructs resulting
49// from flattening a spec
50func (f *FlattenOpts) croak() {51if !f.Verbose {52return53}54
55reported := make(map[string]bool, len(f.flattenContext.newRefs))56for _, v := range f.Spec.references.allRefs {57// warns about duplicate handling58for _, r := range f.flattenContext.newRefs {59if r.isOAIGen && r.path == v.String() {60reported[r.newName] = true61}62}63}64
65for k := range reported {66log.Printf("warning: duplicate flattened definition name resolved as %s", k)67}68
69// warns about possible type mismatches70uniqueMsg := make(map[string]bool)71for _, msg := range f.flattenContext.warnings {72if _, ok := uniqueMsg[msg]; ok {73continue74}75log.Printf("warning: %s", msg)76uniqueMsg[msg] = true77}78}
79