github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/codescan/schema_go118_test.go (about)

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