github.com/go-swagger/go-swagger@v0.31.0/generator/spec_test.go (about) 1 package generator 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/go-openapi/analysis" 8 "github.com/go-openapi/loads" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestSpec_Issue1429(t *testing.T) { 13 defer discardOutput()() 14 15 // acknowledge fix in go-openapi/spec 16 specPath := filepath.Join("..", "fixtures", "bugs", "1429", "swagger-1429.yaml") 17 _, err := loads.Spec(specPath) 18 require.NoError(t, err) 19 20 opts := testGenOpts() 21 opts.Spec = specPath 22 _, err = opts.validateAndFlattenSpec() 23 require.NoError(t, err) 24 25 // more aggressive fixture on $refs, with validation errors, but flatten ok 26 specPath = filepath.Join("..", "fixtures", "bugs", "1429", "swagger.yaml") 27 specDoc, err := loads.Spec(specPath) 28 require.NoError(t, err) 29 30 opts.Spec = specPath 31 opts.FlattenOpts.BasePath = specDoc.SpecFilePath() 32 opts.FlattenOpts.Spec = analysis.New(specDoc.Spec()) 33 opts.FlattenOpts.Minimal = true 34 err = analysis.Flatten(*opts.FlattenOpts) 35 require.NoError(t, err) 36 37 specDoc, _ = loads.Spec(specPath) // needs reload 38 opts.FlattenOpts.Spec = analysis.New(specDoc.Spec()) 39 opts.FlattenOpts.Minimal = false 40 err = analysis.Flatten(*opts.FlattenOpts) 41 require.NoError(t, err) 42 } 43 44 func TestSpec_Issue2527(t *testing.T) { 45 defer discardOutput()() 46 47 t.Run("spec should be detected as invalid", func(t *testing.T) { 48 specPath := filepath.Join("..", "fixtures", "bugs", "2527", "swagger.yml") 49 _, err := loads.Spec(specPath) 50 require.NoError(t, err) 51 52 opts := testGenOpts() 53 opts.Spec = specPath 54 opts.ValidateSpec = true // test options skip validation by default 55 _, err = opts.validateAndFlattenSpec() 56 require.Error(t, err) 57 }) 58 59 t.Run("fixed spec should be detected as valid", func(t *testing.T) { 60 specPath := filepath.Join("..", "fixtures", "bugs", "2527", "swagger-fixed.yml") 61 _, err := loads.Spec(specPath) 62 require.NoError(t, err) 63 64 opts := testGenOpts() 65 opts.Spec = specPath 66 opts.ValidateSpec = true 67 _, err = opts.validateAndFlattenSpec() 68 require.NoError(t, err) 69 }) 70 } 71 72 func TestSpec_FindSwaggerSpec(t *testing.T) { 73 keepErr := func(_ string, err error) error { return err } 74 require.Error(t, keepErr(findSwaggerSpec(""))) 75 require.Error(t, keepErr(findSwaggerSpec("nowhere"))) 76 require.Error(t, keepErr(findSwaggerSpec(filepath.Join("..", "fixtures")))) 77 require.NoError(t, keepErr(findSwaggerSpec(filepath.Join("..", "fixtures", "codegen", "shipyard.yml")))) 78 } 79 80 func TestSpec_Issue1621(t *testing.T) { 81 defer discardOutput()() 82 83 // acknowledge fix in go-openapi/spec 84 specPath := filepath.Join("..", "fixtures", "bugs", "1621", "fixture-1621.yaml") 85 _, err := loads.Spec(specPath) 86 require.NoError(t, err) 87 88 opts := testGenOpts() 89 opts.Spec = specPath 90 opts.ValidateSpec = true 91 _, err = opts.validateAndFlattenSpec() 92 require.NoError(t, err) 93 } 94 95 func TestShared_Issue1614(t *testing.T) { 96 defer discardOutput()() 97 98 // acknowledge fix in go-openapi/spec 99 specPath := filepath.Join("..", "fixtures", "bugs", "1614", "gitea.json") 100 _, err := loads.Spec(specPath) 101 require.NoError(t, err) 102 103 opts := testGenOpts() 104 opts.Spec = specPath 105 opts.ValidateSpec = true 106 _, err = opts.validateAndFlattenSpec() 107 require.NoError(t, err) 108 } 109 110 func Test_AnalyzeSpec_Issue2216(t *testing.T) { 111 defer discardOutput()() 112 113 t.Run("single-swagger-file", func(t *testing.T) { 114 specPath := filepath.Join("..", "fixtures", "bugs", "2216", "swagger-single.yml") 115 116 opts := testGenOpts() 117 opts.Spec = specPath 118 opts.ValidateSpec = true 119 opts.PropertiesSpecOrder = true 120 _, _, err := opts.analyzeSpec() 121 require.NoError(t, err) 122 }) 123 124 t.Run("splitted-swagger-file", func(t *testing.T) { 125 specPath := filepath.Join("..", "fixtures", "bugs", "2216", "swagger.yml") 126 127 opts := testGenOpts() 128 opts.Spec = specPath 129 opts.ValidateSpec = true 130 opts.PropertiesSpecOrder = true 131 _, _, err := opts.analyzeSpec() 132 require.NoError(t, err) 133 }) 134 }