Skip to content

Commit fd90271

Browse files
authored
Merge pull request #67 from fredbi/fix-57
Fixed flattening on circular $ref's with remote URL
2 parents ae8c99d + 5445a00 commit fd90271

9 files changed

+410
-40
lines changed

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ linters:
4343
- gci
4444
- dogsled
4545
- paralleltest
46+
- tparallel
47+
- thelper
48+
- ifshort
49+
- forbidigo

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ notifications:
1313
slack:
1414
secure: Sf7kZf7ZGbnwWUMpffHwMu5A0cHkLK2MYY32LNTPj4+/3qC3Ghl7+9v4TSLOqOlCwdRNjOGblAq7s+GDJed6/xgRQl1JtCi1klzZNrYX4q01pgTPvvGcwbBkIYgeMaPeIRcK9OZnud7sRXdttozgTOpytps2U6Js32ip7uj5mHSg2ub0FwoSJwlS6dbezZ8+eDhoha0F/guY99BEwx8Bd+zROrT2TFGsSGOFGN6wFc7moCqTHO/YkWib13a2QNXqOxCCVBy/lt76Wp+JkeFppjHlzs/2lP3EAk13RIUAaesdEUHvIHrzCyNJEd3/+KO2DzsWOYfpktd+KBCvgaYOsoo7ubdT3IROeAegZdCgo/6xgCEsmFc9ZcqCfN5yNx2A+BZ2Vwmpws+bQ1E1+B5HDzzaiLcYfG4X2O210QVGVDLWsv1jqD+uPYeHY2WRfh5ZsIUFvaqgUEnwHwrK44/8REAhQavt1QAj5uJpsRd7CkRVPWRNK+yIky+wgbVUFEchRNmS55E7QWf+W4+4QZkQi7vUTMc9nbTUu2Es9NfvfudOpM2wZbn98fjpb/qq/nRv6Bk+ca+7XD5/IgNLMbWp2ouDdzbiHLCOfDUiHiDJhLfFZx9Bwo7ZwfzeOlbrQX66bx7xRKYmOe4DLrXhNcpbsMa8qbfxlZRCmYbubB/Y8h4=
1515
script:
16-
- gotestsum -f short-verbose -- -race -timeout=20m -coverprofile=coverage.txt -covermode=atomic ./...
16+
- gotestsum -f short-verbose -- -timeout=20m -coverprofile=coverage.txt -covermode=atomic -args -enable-long ./...
17+
- gotestsum -f short-verbose -- -race -timeout=20m ./...

README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
# OpenAPI initiative analysis [![Build Status](https://travis-ci.org/go-openapi/analysis.svg?branch=master)](https://travis-ci.org/go-openapi/analysis) [![Build status](https://ci.appveyor.com/api/projects/status/x377t5o9ennm847o/branch/master?svg=true)](https://ci.appveyor.com/project/casualjim/go-openapi/analysis/branch/master) [![codecov](https://codecov.io/gh/go-openapi/analysis/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/analysis)
1+
# OpenAPI initiative analysis
2+
3+
[![Build Status](https://travis-ci.org/go-openapi/analysis.svg?branch=master)](https://travis-ci.org/go-openapi/analysis)
4+
[![Build status](https://ci.appveyor.com/api/projects/status/x377t5o9ennm847o/branch/master?svg=true)](https://ci.appveyor.com/project/casualjim/go-openapi/analysis/branch/master)
5+
[![codecov](https://codecov.io/gh/go-openapi/analysis/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/analysis)
26
[![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io)
37
[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/analysis/master/LICENSE)
4-
[![GoDoc](https://godoc.org/github.com/go-openapi/analysis?status.svg)](http://godoc.org/github.com/go-openapi/analysis)
8+
[![Go Reference](https://pkg.go.dev/badge/github.com/go-openapi/analysis.svg)](https://pkg.go.dev/github.com/go-openapi/analysis)
59
[![GolangCI](https://golangci.com/badges/github.com/go-openapi/analysis.svg)](https://golangci.com)
610
[![Go Report Card](https://goreportcard.com/badge/github.com/go-openapi/analysis)](https://goreportcard.com/report/github.com/go-openapi/analysis)
711

812

913
A foundational library to analyze an OAI specification document for easier reasoning about the content.
14+
15+
## What's inside?
16+
17+
* A analyzer providing methods to walk the functional content of a specification
18+
* A spec flattener producing a self-contained document bundle, while preserving `$ref`s
19+
* A spec merger ("mixin") to merge several spec documents into a primary spec
20+
* A spec "fixer" ensuring that response descriptions are non empty
21+
22+
[Documentation](https://godoc.org/github.com/go-openapi/analysis)
23+
24+
## FAQ
25+
26+
* Does this library support OpenAPI 3?
27+
28+
> No.
29+
> This package currently only supports OpenAPI 2.0 (aka Swagger 2.0).
30+
> There is no plan to make it evolve toward supporting OpenAPI 3.x.
31+
> This [discussion thread](https://github.com/go-openapi/spec/issues/21) relates the full story.
32+
>

appveyor.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ build: off
1414
environment:
1515
GOPATH: c:\gopath
1616

17-
stack: go 1.12
17+
stack: go 1.15
1818

1919
test_script:
20-
- echo "test disabled for now"
21-
#- go test -v -timeout 20m ./...
22-
#artifacts:
23-
# - path: '%GOPATH%\bin\*.exe'
20+
- go test -v -timeout 20m ./...
21+
2422
deploy: off
2523

2624
notifications:

flatten.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"log"
2020
"net/http"
2121
"net/url"
22-
"os"
2322
slashpath "path"
2423
"path/filepath"
2524
"sort"
@@ -34,18 +33,25 @@ import (
3433
)
3534

3635
// FlattenOpts configuration for flattening a swagger specification.
36+
//
37+
// The BasePath parameter is used to locate remote relative $ref found in the specification.
38+
// This path is a file: it points to the location of the root document and may be either a local
39+
// file path or a URL.
40+
//
41+
// If none specified, relative references (e.g. "$ref": "folder/schema.yaml#/definitions/...")
42+
// found in the spec are searched from the current working directory.
3743
type FlattenOpts struct {
3844
Spec *Spec // The analyzed spec to work with
3945
flattenContext *context // Internal context to track flattening activity
4046

41-
BasePath string
47+
BasePath string // The location of the root document for this spec to resolve relative $ref
4248

4349
// Flattening options
44-
Expand bool // If Expand is true, we skip flattening the spec and expand it instead
45-
Minimal bool
46-
Verbose bool
47-
RemoveUnused bool
48-
ContinueOnError bool // Continues when facing some issues
50+
Expand bool // When true, skip flattening the spec and expand it instead (if Minimal is false)
51+
Minimal bool // When true, do not decompose complex structures such as allOf
52+
Verbose bool // enable some reporting on possible name conflicts detected
53+
RemoveUnused bool // When true, remove unused parameters, responses and definitions after expansion/flattening
54+
ContinueOnError bool // Continue when spec expansion issues are found
4955

5056
/* Extra keys */
5157
_ struct{} // require keys
@@ -91,6 +97,7 @@ func newContext() *context {
9197
//
9298
// There is a minimal and a full flattening mode.
9399
//
100+
//
94101
// Minimally flattening a spec means:
95102
// - Expanding parameters, responses, path items, parameter items and header items (references to schemas are left
96103
// unscathed)
@@ -105,6 +112,8 @@ func newContext() *context {
105112
// NOTE: arbitrary JSON pointers (other than $refs to top level definitions) are rewritten as definitions if they
106113
// represent a complex schema or express commonality in the spec.
107114
// Otherwise, they are simply expanded.
115+
// Self-referencing JSON pointers cannot resolve to a type and trigger an error.
116+
//
108117
//
109118
// Minimal flattening is necessary and sufficient for codegen rendering using go-swagger.
110119
//
@@ -137,15 +146,6 @@ func newContext() *context {
137146
//
138147
func Flatten(opts FlattenOpts) error {
139148
debugLog("FlattenOpts: %#v", opts)
140-
// Make sure opts.BasePath is an absolute path
141-
if !filepath.IsAbs(opts.BasePath) {
142-
cwd, _ := os.Getwd()
143-
opts.BasePath = filepath.Join(cwd, opts.BasePath)
144-
}
145-
// make sure drive letter on windows is normalized to lower case
146-
u, _ := url.Parse(opts.BasePath)
147-
opts.BasePath = u.String()
148-
149149
opts.flattenContext = newContext()
150150

151151
// recursively expand responses, parameters, path items and items in simple schemas.
@@ -180,6 +180,7 @@ func Flatten(opts FlattenOpts) error {
180180
// This inlining deals with name conflicts by introducing auto-generated names ("OAIGen")
181181
var err error
182182
if imported, err = importExternalReferences(&opts); err != nil {
183+
debugLog("error in importExternalReferences: %v", err)
183184
return err
184185
}
185186
opts.Spec.reload() // re-analyze

go.mod

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
module github.com/go-openapi/analysis
22

33
require (
4-
github.com/go-openapi/errors v0.19.9 // indirect
54
github.com/go-openapi/jsonpointer v0.19.5
6-
github.com/go-openapi/loads v0.19.6
7-
github.com/go-openapi/spec v0.19.15
8-
github.com/go-openapi/strfmt v0.19.11
9-
github.com/go-openapi/swag v0.19.12
10-
github.com/mitchellh/mapstructure v1.4.0 // indirect
11-
github.com/stretchr/testify v1.6.1
5+
github.com/go-openapi/loads v0.20.2
6+
github.com/go-openapi/spec v0.20.3
7+
github.com/go-openapi/strfmt v0.20.0
8+
github.com/go-openapi/swag v0.19.14
9+
github.com/stretchr/testify v1.7.0
1210
)
1311

1412
go 1.13

0 commit comments

Comments
 (0)