(filePath string, opts LoadSwaggerWithOverlayOpts)
| 38 | } |
| 39 | |
| 40 | func LoadSwaggerWithOverlay(filePath string, opts LoadSwaggerWithOverlayOpts) (swagger *openapi3.T, err error) { |
| 41 | spec, err := LoadSwagger(filePath) |
| 42 | if err != nil { |
| 43 | return nil, fmt.Errorf("failed to load OpenAPI specification: %w", err) |
| 44 | } |
| 45 | |
| 46 | if opts.Path == "" { |
| 47 | return spec, nil |
| 48 | } |
| 49 | |
| 50 | // parse out the yaml.Node, which is required by the overlay library |
| 51 | buf := &bytes.Buffer{} |
| 52 | enc := yaml.NewEncoder(buf) |
| 53 | // set to 2 to work around https://github.com/yaml/go-yaml/issues/76 |
| 54 | enc.SetIndent(2) |
| 55 | err = enc.Encode(spec) |
| 56 | if err != nil { |
| 57 | return nil, fmt.Errorf("failed to marshal spec from %#v as YAML: %w", filePath, err) |
| 58 | } |
| 59 | |
| 60 | var node yaml.Node |
| 61 | err = yaml.NewDecoder(buf).Decode(&node) |
| 62 | if err != nil { |
| 63 | return nil, fmt.Errorf("failed to parse spec from %#v: %w", filePath, err) |
| 64 | } |
| 65 | |
| 66 | overlay, err := loader.LoadOverlay(opts.Path) |
| 67 | if err != nil { |
| 68 | return nil, fmt.Errorf("failed to load Overlay from %#v: %v", opts.Path, err) |
| 69 | } |
| 70 | |
| 71 | err = overlay.Validate() |
| 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("the Overlay in %#v was not valid: %v", opts.Path, err) |
| 74 | } |
| 75 | |
| 76 | if opts.Strict { |
| 77 | vs, err := overlay.ApplyToStrict(&node) |
| 78 | if err != nil { |
| 79 | return nil, fmt.Errorf("failed to apply Overlay %#v to specification %#v: %v\nAdditionally, the following validation errors were found:\n- %s", opts.Path, filePath, err, strings.Join(vs, "\n- ")) |
| 80 | } |
| 81 | } else { |
| 82 | err = overlay.ApplyTo(&node) |
| 83 | if err != nil { |
| 84 | return nil, fmt.Errorf("failed to apply Overlay %#v to specification %#v: %v", opts.Path, filePath, err) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | b, err := yaml.Marshal(&node) |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("failed to serialize Overlay'd specification %#v: %v", opts.Path, err) |
| 91 | } |
| 92 | |
| 93 | loader := openapi3.NewLoader() |
| 94 | loader.IsExternalRefsAllowed = true |
| 95 | |
| 96 | swagger, err = loader.LoadFromDataWithPath(b, &url.URL{ |
| 97 | Path: filepath.ToSlash(filePath), |
no test coverage detected