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