github.com/go-swagger/go-swagger@v0.31.0/codescan/schema_go118_test.go (about)

     1  package codescan
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-openapi/spec"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  var go118ClassificationCtx *scanCtx
    12  
    13  func loadGo118ClassificationPkgsCtx(t testing.TB, extra ...string) *scanCtx {
    14  	if go118ClassificationCtx != nil {
    15  		return go118ClassificationCtx
    16  	}
    17  	sctx, err := newScanCtx(&Options{
    18  		Packages: append([]string{
    19  			"github.com/go-swagger/go-swagger/fixtures/goparsing/go118",
    20  		}, extra...),
    21  	})
    22  	require.NoError(t, err)
    23  	go118ClassificationCtx = sctx
    24  	return go118ClassificationCtx
    25  }
    26  
    27  func getGo118ClassificationModel(sctx *scanCtx, nm string) *entityDecl {
    28  	decl, ok := sctx.FindDecl("github.com/go-swagger/go-swagger/fixtures/goparsing/go118", nm)
    29  	if !ok {
    30  		return nil
    31  	}
    32  	return decl
    33  }
    34  
    35  func TestGo118SwaggerTypeNamed(t *testing.T) {
    36  	sctx := loadGo118ClassificationPkgsCtx(t)
    37  	decl := getGo118ClassificationModel(sctx, "NamedWithType")
    38  	require.NotNil(t, decl)
    39  	prs := &schemaBuilder{
    40  		ctx:  sctx,
    41  		decl: decl,
    42  	}
    43  	models := make(map[string]spec.Schema)
    44  	require.NoError(t, prs.Build(models))
    45  	schema := models["namedWithType"]
    46  
    47  	assertProperty(t, &schema, "object", "some_map", "", "SomeMap")
    48  }
    49  
    50  func TestGo118AliasedModels(t *testing.T) {
    51  	sctx := loadGo118ClassificationPkgsCtx(t)
    52  
    53  	names := []string{
    54  		"SomeObject",
    55  	}
    56  
    57  	defs := make(map[string]spec.Schema)
    58  	for _, nm := range names {
    59  		decl := getGo118ClassificationModel(sctx, nm)
    60  		require.NotNil(t, decl)
    61  
    62  		prs := &schemaBuilder{
    63  			decl: decl,
    64  			ctx:  sctx,
    65  		}
    66  		require.NoError(t, prs.Build(defs))
    67  	}
    68  
    69  	for k := range defs {
    70  		for i, b := range names {
    71  			if b == k {
    72  				// remove the entry from the collection
    73  				names = append(names[:i], names[i+1:]...)
    74  			}
    75  		}
    76  	}
    77  	if assert.Empty(t, names) {
    78  		// map types
    79  		assertMapDefinition(t, defs, "SomeObject", "object", "", "")
    80  	}
    81  }
    82  
    83  func TestGo118InterfaceField(t *testing.T) {
    84  	sctx := loadGo118ClassificationPkgsCtx(t)
    85  	decl := getGo118ClassificationModel(sctx, "Interfaced")
    86  	require.NotNil(t, decl)
    87  	prs := &schemaBuilder{
    88  		ctx:  sctx,
    89  		decl: decl,
    90  	}
    91  	models := make(map[string]spec.Schema)
    92  	require.NoError(t, prs.Build(models))
    93  
    94  	schema := models["Interfaced"]
    95  	assertProperty(t, &schema, "", "custom_data", "", "CustomData")
    96  }
    97  
    98  func TestGo118ParameterParser_Issue2011(t *testing.T) {
    99  	sctx := loadGo118ClassificationPkgsCtx(t)
   100  	operations := make(map[string]*spec.Operation)
   101  	td := getParameter(sctx, "NumPlates")
   102  	prs := &parameterBuilder{
   103  		ctx:  sctx,
   104  		decl: td,
   105  	}
   106  	require.NoError(t, prs.Build(operations))
   107  
   108  	op := operations["putNumPlate"]
   109  	require.NotNil(t, op)
   110  	require.Len(t, op.Parameters, 1)
   111  	sch := op.Parameters[0].Schema
   112  	require.NotNil(t, sch)
   113  }
   114  
   115  func TestGo118ParseResponses_Issue2011(t *testing.T) {
   116  	sctx := loadGo118ClassificationPkgsCtx(t)
   117  	responses := make(map[string]spec.Response)
   118  	td := getResponse(sctx, "NumPlatesResp")
   119  	prs := &responseBuilder{
   120  		ctx:  sctx,
   121  		decl: td,
   122  	}
   123  	require.NoError(t, prs.Build(responses))
   124  
   125  	resp := responses["NumPlatesResp"]
   126  	require.Empty(t, resp.Headers)
   127  	require.NotNil(t, resp.Schema)
   128  }
   129  
   130  func TestGo118_Issue2809(t *testing.T) {
   131  	sctx := loadGo118ClassificationPkgsCtx(t)
   132  	decl := getGo118ClassificationModel(sctx, "transportErr")
   133  	require.NotNil(t, decl)
   134  	prs := &schemaBuilder{
   135  		ctx:  sctx,
   136  		decl: decl,
   137  	}
   138  	models := make(map[string]spec.Schema)
   139  	require.NoError(t, prs.Build(models))
   140  
   141  	schema := models["transportErr"]
   142  	assertProperty(t, &schema, "", "data", "", "Data")
   143  }