github.com/go-swagger/go-swagger@v0.31.0/generator/structs_test.go (about) 1 package generator 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func TestPrintTags(t *testing.T) { 10 type tagFixture struct { 11 Title string 12 Schema GenSchema 13 ExpectedTags string 14 } 15 16 mustJSON := func(in interface{}) string { 17 b, _ := asJSON(in) 18 return b 19 } 20 21 fixtures := []tagFixture{ 22 { 23 Title: "no extra: default json", 24 Schema: GenSchema{ 25 OriginalName: "field", 26 }, 27 ExpectedTags: "`json:\"field\"`", 28 }, 29 { 30 Title: "no extra: default json, omitempty", 31 Schema: GenSchema{ 32 OriginalName: "field", 33 resolvedType: resolvedType{ 34 IsEmptyOmitted: true, 35 }, 36 }, 37 ExpectedTags: "`json:\"field,omitempty\"`", 38 }, 39 { 40 Title: "no extra: default json, required, omitempty", 41 Schema: GenSchema{ 42 OriginalName: "field", 43 resolvedType: resolvedType{ 44 IsEmptyOmitted: true, 45 }, 46 sharedValidations: sharedValidations{ 47 Required: true, 48 }, 49 }, 50 ExpectedTags: "`json:\"field\"`", 51 }, 52 { 53 Title: "no extra: default json, omitempty, as JSON string", 54 Schema: GenSchema{ 55 OriginalName: "field", 56 resolvedType: resolvedType{ 57 IsEmptyOmitted: true, 58 IsJSONString: true, 59 }, 60 }, 61 ExpectedTags: "`json:\"field,omitempty,string\"`", 62 }, 63 { 64 Title: "with xml name", 65 Schema: GenSchema{ 66 OriginalName: "field", 67 resolvedType: resolvedType{ 68 IsEmptyOmitted: true, 69 }, 70 sharedValidations: sharedValidations{ 71 Required: true, 72 }, 73 XMLName: "xmlfield", 74 }, 75 ExpectedTags: "`json:\"field\" xml:\"xmlfield\"`", 76 }, 77 { 78 Title: "with example (1/3)", 79 Schema: GenSchema{ 80 OriginalName: "field", 81 resolvedType: resolvedType{ 82 IsEmptyOmitted: true, 83 }, 84 sharedValidations: sharedValidations{ 85 Required: true, 86 }, 87 Example: mustJSON("xyz"), 88 StructTags: []string{"example"}, 89 }, 90 ExpectedTags: "`json:\"field\" example:\"\\\"xyz\\\"\"`", 91 }, 92 { 93 Title: "with example (2/3)", 94 Schema: GenSchema{ 95 OriginalName: "field", 96 resolvedType: resolvedType{ 97 IsEmptyOmitted: true, 98 }, 99 sharedValidations: sharedValidations{ 100 Required: true, 101 }, 102 Example: mustJSON(15), 103 StructTags: []string{"example"}, 104 }, 105 ExpectedTags: "`json:\"field\" example:\"15\"`", 106 }, 107 { 108 Title: "with example (3/3)", 109 Schema: GenSchema{ 110 OriginalName: "field", 111 resolvedType: resolvedType{ 112 IsEmptyOmitted: true, 113 }, 114 sharedValidations: sharedValidations{ 115 Required: true, 116 }, 117 Example: mustJSON(struct { 118 A string `json:"a"` 119 B int64 `json:"b"` 120 }{A: "xyz", B: 12}), 121 StructTags: []string{"example"}, 122 }, 123 ExpectedTags: "`json:\"field\" example:\"{\\\"a\\\":\\\"xyz\\\",\\\"b\\\":12}\"`", 124 }, 125 { 126 Title: "with example, xml, omitempty, custom tag", 127 Schema: GenSchema{ 128 OriginalName: "field", 129 resolvedType: resolvedType{ 130 IsEmptyOmitted: true, 131 }, 132 Example: mustJSON(15), 133 StructTags: []string{"example"}, 134 XMLName: "xmlfield,attr", 135 CustomTag: `metric:"on"`, 136 }, 137 ExpectedTags: "`json:\"field,omitempty\" xml:\"xmlfield,attr,omitempty\" example:\"15\" metric:\"on\"`", 138 }, 139 { 140 Title: "with backticks", 141 Schema: GenSchema{ 142 OriginalName: "field", 143 Example: mustJSON(struct { 144 A string `json:"a"` 145 B int64 `json:"b"` 146 }{A: "`xyz`", B: 12}), 147 StructTags: []string{"example"}, 148 CustomTag: "metric:\"`on`\"", 149 }, 150 ExpectedTags: "\"json:\\\"field\\\" example:\\\"{\\\\\\\"a\\\\\\\":\\\\\\\"`xyz`\\\\\\\",\\\\\\\"b\\\\\\\":12}\\\" metric:\\\"`on`\\\"\"", 151 }, 152 { 153 Title: "with description", 154 Schema: GenSchema{ 155 OriginalName: "field", 156 Description: "some description", 157 StructTags: []string{"description"}, 158 }, 159 ExpectedTags: "`json:\"field\" description:\"some description\"`", 160 }, 161 { 162 Title: "with multiline description", 163 Schema: GenSchema{ 164 OriginalName: "field", 165 Description: "a\ndescription\nspanning\nmultiple\nlines", 166 StructTags: []string{"description"}, 167 }, 168 ExpectedTags: "\"json:\\\"field\\\" description:\\\"a\\\\ndescription\\\\nspanning\\\\nmultiple\\\\nlines\\\"\"", 169 }, 170 } 171 172 for _, toPin := range fixtures { 173 fixture := toPin 174 t.Run(fixture.Title, func(t *testing.T) { 175 require.Equal(t, fixture.ExpectedTags, fixture.Schema.PrintTags()) 176 }) 177 } 178 }