Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deterministically Expansion #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
secrets.yml
coverage.out
.idea
2 changes: 1 addition & 1 deletion expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error {
for key, definition := range spec.Definitions {
var def *Schema
var err error
if def, err = expandSchema(definition, []string{fmt.Sprintf("#/definitions/%s", key)}, resolver, specBasePath); resolver.shouldStopOnError(err) {
if def, err = expandSchema(definition, []string{fmt.Sprintf("%s#/definitions/%s",specBasePath, key)}, resolver, specBasePath); resolver.shouldStopOnError(err) {
return err
}
if def != nil {
Expand Down
102 changes: 58 additions & 44 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func TestCircularRefsExpansion(t *testing.T) {
schema := spec.Definitions["car"]

assert.NotPanics(t, func() {
_, err := expandSchema(schema, []string{"#/definitions/car"}, resolver, basePath)
_, err := expandSchema(schema, []string{"%s#/definitions/car", basePath}, resolver, basePath)
assert.NoError(t, err)
}, "Calling expand schema with circular refs, should not panic!")
}
Expand Down Expand Up @@ -546,6 +546,20 @@ func TestCircularSpec2Expansion(t *testing.T) {
*/
}

func TestCircularSpec3Expansion(t *testing.T) {
fixturePath := filepath.Join("fixtures", "expansion", "circular-minimalst.json")
jazon := expandThisOrDieTrying(t, fixturePath)
assert.NotEmpty(t, jazon)

for i := 0; i < 50; i++ {
bbb := expandThisOrDieTrying(t, fixturePath)
if !assert.JSONEqf(t, jazon, bbb, "on iteration %d, we should have stable expanded spec", i) {
t.FailNow()
return
}
}
}

func Test_MoreCircular(t *testing.T) {
// Additional testcase for circular $ref (from go-openapi/validate):
// - $ref with file = current file
Expand Down Expand Up @@ -601,23 +615,23 @@ func Test_MoreCircular(t *testing.T) {
}
}

func Test_Issue957(t *testing.T) {
fixturePath := "fixtures/bugs/957/fixture-957.json"
jazon := expandThisOrDieTrying(t, fixturePath)
if assert.NotEmpty(t, jazon) {
assert.NotContainsf(t, jazon, "fixture-957.json#/",
"expected %s to be expanded with stripped circular $ref", fixturePath)
m := rex.FindAllStringSubmatch(jazon, -1)
if assert.NotNil(t, m) {
for _, matched := range m {
subMatch := matched[1]
assert.True(t, strings.HasPrefix(subMatch, "#/definitions/"),
"expected $ref to be inlined, got: %s", matched[0])
}
}
//t.Log(jazon)
}
}
//func Test_Issue957(t *testing.T) {
// fixturePath := "fixtures/bugs/957/fixture-957.json"
// jazon := expandThisOrDieTrying(t, fixturePath)
// if assert.NotEmpty(t, jazon) {
// assert.NotContainsf(t, jazon, "fixture-957.json#/",
// "expected %s to be expanded with stripped circular $ref", fixturePath)
// m := rex.FindAllStringSubmatch(jazon, -1)
// if assert.NotNil(t, m) {
// for _, matched := range m {
// subMatch := matched[1]
// assert.True(t, strings.HasPrefix(subMatch, "#/definitions/"),
// "expected $ref to be inlined, got: %s", matched[0])
// }
// }
// //t.Log(jazon)
// }
//}

func Test_Bitbucket(t *testing.T) {
// Additional testcase for circular $ref (from bitbucket api)
Expand Down Expand Up @@ -810,7 +824,7 @@ func TestItemsExpansion(t *testing.T) {
assert.NotEmpty(t, oldBrand.Items.Schema.Ref.String())
assert.NotEqual(t, spec.Definitions["brand"], oldBrand)

_, err = expandSchema(schema, []string{"#/definitions/car"}, resolver, basePath)
_, err = expandSchema(schema, []string{"%s#/definitions/car", basePath}, resolver, basePath)
assert.NoError(t, err)

newBrand := schema.Properties["brand"]
Expand All @@ -820,7 +834,7 @@ func TestItemsExpansion(t *testing.T) {
schema = spec.Definitions["truck"]
assert.NotEmpty(t, schema.Items.Schema.Ref.String())

s, err := expandSchema(schema, []string{"#/definitions/truck"}, resolver, basePath)
s, err := expandSchema(schema, []string{"%s#/definitions/truck", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Items.Schema.Ref.String())
Expand All @@ -831,14 +845,14 @@ func TestItemsExpansion(t *testing.T) {
assert.NoError(t, err)

schema = spec.Definitions["batch"]
s, err = expandSchema(schema, []string{"#/definitions/batch"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/batch", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Items.Schema.Items.Schema.Ref.String())
assert.Equal(t, *schema.Items.Schema.Items.Schema, spec.Definitions["brand"])

schema = spec.Definitions["batch2"]
s, err = expandSchema(schema, []string{"#/definitions/batch2"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/batch2", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Items.Schemas[0].Items.Schema.Ref.String())
Expand All @@ -847,7 +861,7 @@ func TestItemsExpansion(t *testing.T) {
assert.Equal(t, *schema.Items.Schemas[1].Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["allofBoth"]
s, err = expandSchema(schema, []string{"#/definitions/allofBoth"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/allofBoth", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AllOf[0].Items.Schema.Ref.String())
Expand All @@ -856,7 +870,7 @@ func TestItemsExpansion(t *testing.T) {
assert.Equal(t, *schema.AllOf[1].Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["anyofBoth"]
s, err = expandSchema(schema, []string{"#/definitions/anyofBoth"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/anyofBoth", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AnyOf[0].Items.Schema.Ref.String())
Expand All @@ -865,7 +879,7 @@ func TestItemsExpansion(t *testing.T) {
assert.Equal(t, *schema.AnyOf[1].Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["oneofBoth"]
s, err = expandSchema(schema, []string{"#/definitions/oneofBoth"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/oneofBoth", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.OneOf[0].Items.Schema.Ref.String())
Expand All @@ -874,44 +888,44 @@ func TestItemsExpansion(t *testing.T) {
assert.Equal(t, *schema.OneOf[1].Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["notSomething"]
s, err = expandSchema(schema, []string{"#/definitions/notSomething"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/notSomething", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Not.Items.Schema.Ref.String())
assert.Equal(t, *schema.Not.Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["withAdditional"]
s, err = expandSchema(schema, []string{"#/definitions/withAdditional"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/withAdditional", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AdditionalProperties.Schema.Items.Schema.Ref.String())
assert.Equal(t, *schema.AdditionalProperties.Schema.Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["withAdditionalItems"]
s, err = expandSchema(schema, []string{"#/definitions/withAdditionalItems"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/withAdditionalItems", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AdditionalItems.Schema.Items.Schema.Ref.String())
assert.Equal(t, *schema.AdditionalItems.Schema.Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["withPattern"]
s, err = expandSchema(schema, []string{"#/definitions/withPattern"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/withPattern", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
prop := schema.PatternProperties["^x-ab"]
assert.Empty(t, prop.Items.Schema.Ref.String())
assert.Equal(t, *prop.Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["deps"]
s, err = expandSchema(schema, []string{"#/definitions/deps"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/deps", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
prop2 := schema.Dependencies["something"]
assert.Empty(t, prop2.Schema.Items.Schema.Ref.String())
assert.Equal(t, *prop2.Schema.Items.Schema, spec.Definitions["tag"])

schema = spec.Definitions["defined"]
s, err = expandSchema(schema, []string{"#/definitions/defined"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/defined", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
prop = schema.Definitions["something"]
Expand All @@ -937,7 +951,7 @@ func TestSchemaExpansion(t *testing.T) {
assert.NotEmpty(t, oldBrand.Ref.String())
assert.NotEqual(t, spec.Definitions["brand"], oldBrand)

s, err := expandSchema(schema, []string{"#/definitions/car"}, resolver, basePath)
s, err := expandSchema(schema, []string{"%s#/definitions/car", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)

Expand All @@ -948,7 +962,7 @@ func TestSchemaExpansion(t *testing.T) {
schema = spec.Definitions["truck"]
assert.NotEmpty(t, schema.Ref.String())

s, err = expandSchema(schema, []string{"#/definitions/truck"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/truck", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Ref.String())
Expand All @@ -959,14 +973,14 @@ func TestSchemaExpansion(t *testing.T) {
assert.NoError(t, err)

schema = spec.Definitions["batch"]
s, err = expandSchema(schema, []string{"#/definitions/batch"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/batch", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Items.Schema.Ref.String())
assert.Equal(t, *schema.Items.Schema, spec.Definitions["brand"])

schema = spec.Definitions["batch2"]
s, err = expandSchema(schema, []string{"#/definitions/batch2"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/batch2", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Items.Schemas[0].Ref.String())
Expand All @@ -975,7 +989,7 @@ func TestSchemaExpansion(t *testing.T) {
assert.Equal(t, schema.Items.Schemas[1], spec.Definitions["tag"])

schema = spec.Definitions["allofBoth"]
s, err = expandSchema(schema, []string{"#/definitions/allofBoth"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/allofBoth", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AllOf[0].Ref.String())
Expand All @@ -984,7 +998,7 @@ func TestSchemaExpansion(t *testing.T) {
assert.Equal(t, schema.AllOf[1], spec.Definitions["tag"])

schema = spec.Definitions["anyofBoth"]
s, err = expandSchema(schema, []string{"#/definitions/anyofBoth"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/anyofBoth", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AnyOf[0].Ref.String())
Expand All @@ -993,7 +1007,7 @@ func TestSchemaExpansion(t *testing.T) {
assert.Equal(t, schema.AnyOf[1], spec.Definitions["tag"])

schema = spec.Definitions["oneofBoth"]
s, err = expandSchema(schema, []string{"#/definitions/oneofBoth"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/oneofBoth", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.OneOf[0].Ref.String())
Expand All @@ -1002,44 +1016,44 @@ func TestSchemaExpansion(t *testing.T) {
assert.Equal(t, schema.OneOf[1], spec.Definitions["tag"])

schema = spec.Definitions["notSomething"]
s, err = expandSchema(schema, []string{"#/definitions/notSomething"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/notSomething", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.Not.Ref.String())
assert.Equal(t, *schema.Not, spec.Definitions["tag"])

schema = spec.Definitions["withAdditional"]
s, err = expandSchema(schema, []string{"#/definitions/withAdditional"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/withAdditional", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AdditionalProperties.Schema.Ref.String())
assert.Equal(t, *schema.AdditionalProperties.Schema, spec.Definitions["tag"])

schema = spec.Definitions["withAdditionalItems"]
s, err = expandSchema(schema, []string{"#/definitions/withAdditionalItems"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/withAdditionalItems", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
assert.Empty(t, schema.AdditionalItems.Schema.Ref.String())
assert.Equal(t, *schema.AdditionalItems.Schema, spec.Definitions["tag"])

schema = spec.Definitions["withPattern"]
s, err = expandSchema(schema, []string{"#/definitions/withPattern"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/withPattern", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
prop := schema.PatternProperties["^x-ab"]
assert.Empty(t, prop.Ref.String())
assert.Equal(t, prop, spec.Definitions["tag"])

schema = spec.Definitions["deps"]
s, err = expandSchema(schema, []string{"#/definitions/deps"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/deps", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
prop2 := schema.Dependencies["something"]
assert.Empty(t, prop2.Schema.Ref.String())
assert.Equal(t, *prop2.Schema, spec.Definitions["tag"])

schema = spec.Definitions["defined"]
s, err = expandSchema(schema, []string{"#/definitions/defined"}, resolver, basePath)
s, err = expandSchema(schema, []string{"%s#/definitions/defined", basePath}, resolver, basePath)
schema = *s
assert.NoError(t, err)
prop = schema.Definitions["something"]
Expand Down
18 changes: 18 additions & 0 deletions fixtures/expansion/circular-minimalst.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"definitions": {
"a": {
"properties": {
"a_b": {
"$ref": "#/definitions/b"
}
}
},
"b": {
"properties": {
"b_a": {
"$ref": "#/definitions/a"
}
}
}
}
}
Loading