github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/generator/spec_test.go (about)

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