Skip to content

Commit 59997e4

Browse files
committed
openapi3: use Ptr instead of BoolPtr,Float64Ptr,Int64Ptr,Uint64Ptr
1 parent e230c13 commit 59997e4

8 files changed

+51
-28
lines changed

.github/docs/openapi3.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ FUNCTIONS
9696
func BoolPtr(value bool) *bool
9797
BoolPtr is a helper for defining OpenAPI schemas.
9898

99+
Deprecated: Use Ptr instead.
100+
99101
func DefaultRefNameResolver(doc *T, ref ComponentRef) string
100102
DefaultRefResolver is a default implementation of refNameResolver for the
101103
InternalizeRefs function.
@@ -144,9 +146,16 @@ func DefineStringFormatValidator(name string, validator StringFormatValidator)
144146
func Float64Ptr(value float64) *float64
145147
Float64Ptr is a helper for defining OpenAPI schemas.
146148

149+
Deprecated: Use Ptr instead.
150+
147151
func Int64Ptr(value int64) *int64
148152
Int64Ptr is a helper for defining OpenAPI schemas.
149153

154+
Deprecated: Use Ptr instead.
155+
156+
func Ptr[T any](value T) *T
157+
Ptr is a helper for defining OpenAPI schemas.
158+
150159
func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error)
151160
ReadFromFile is a ReadFromURIFunc which reads local file URIs.
152161

@@ -199,6 +208,8 @@ func RegisterArrayUniqueItemsChecker(fn SliceUniqueItemsChecker)
199208
func Uint64Ptr(value uint64) *uint64
200209
Uint64Ptr is a helper for defining OpenAPI schemas.
201210

211+
Deprecated: Use Ptr instead.
212+
202213
func ValidateIdentifier(value string) error
203214
ValidateIdentifier returns an error if the given component name does not
204215
match IdentifierRegExp.
@@ -2263,4 +2274,3 @@ func (xml *XML) UnmarshalJSON(data []byte) error
22632274

22642275
func (xml *XML) Validate(ctx context.Context, opts ...ValidationOption) error
22652276
Validate returns an error if XML does not comply with the OpenAPI spec.
2266-

openapi3/encoding_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func encoding() *Encoding {
5252
},
5353
},
5454
Style: "form",
55-
Explode: BoolPtr(true),
55+
Explode: Ptr(true),
5656
AllowReserved: true,
5757
}
5858
}
@@ -74,17 +74,17 @@ func TestEncodingSerializationMethod(t *testing.T) {
7474
},
7575
{
7676
name: "encoding with explode",
77-
enc: &Encoding{Explode: BoolPtr(true)},
77+
enc: &Encoding{Explode: Ptr(true)},
7878
want: &SerializationMethod{Style: SerializationForm, Explode: true},
7979
},
8080
{
8181
name: "encoding with no explode",
82-
enc: &Encoding{Explode: BoolPtr(false)},
82+
enc: &Encoding{Explode: Ptr(false)},
8383
want: &SerializationMethod{Style: SerializationForm, Explode: false},
8484
},
8585
{
8686
name: "encoding with style and explode ",
87-
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: BoolPtr(false)},
87+
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: Ptr(false)},
8888
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: false},
8989
},
9090
}

openapi3/helpers.go

+13
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,35 @@ func ValidateIdentifier(value string) error {
3131
return fmt.Errorf("identifier %q is not supported by OpenAPIv3 standard (charset: [%q])", value, identifierChars)
3232
}
3333

34+
// Ptr is a helper for defining OpenAPI schemas.
35+
func Ptr[T any](value T) *T {
36+
return &value
37+
}
38+
3439
// Float64Ptr is a helper for defining OpenAPI schemas.
40+
//
41+
// Deprecated: Use Ptr instead.
3542
func Float64Ptr(value float64) *float64 {
3643
return &value
3744
}
3845

3946
// BoolPtr is a helper for defining OpenAPI schemas.
47+
//
48+
// Deprecated: Use Ptr instead.
4049
func BoolPtr(value bool) *bool {
4150
return &value
4251
}
4352

4453
// Int64Ptr is a helper for defining OpenAPI schemas.
54+
//
55+
// Deprecated: Use Ptr instead.
4556
func Int64Ptr(value int64) *int64 {
4657
return &value
4758
}
4859

4960
// Uint64Ptr is a helper for defining OpenAPI schemas.
61+
//
62+
// Deprecated: Use Ptr instead.
5063
func Uint64Ptr(value uint64) *uint64 {
5164
return &value
5265
}

openapi3/issue376_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ info:
4646
func TestExclusiveValuesOfValuesAdditionalProperties(t *testing.T) {
4747
schema := &Schema{
4848
AdditionalProperties: AdditionalProperties{
49-
Has: BoolPtr(false),
49+
Has: Ptr(false),
5050
Schema: NewSchemaRef("", &Schema{}),
5151
},
5252
}
@@ -55,7 +55,7 @@ func TestExclusiveValuesOfValuesAdditionalProperties(t *testing.T) {
5555

5656
schema = &Schema{
5757
AdditionalProperties: AdditionalProperties{
58-
Has: BoolPtr(false),
58+
Has: Ptr(false),
5959
},
6060
}
6161
err = schema.Validate(context.Background())

openapi3/issue735_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestIssue735(t *testing.T) {
7474
},
7575
{
7676
name: "multiple of",
77-
schema: &Schema{MultipleOf: Float64Ptr(5.0)},
77+
schema: &Schema{MultipleOf: Ptr(5.0)},
7878
value: 42,
7979
},
8080
{
@@ -142,7 +142,7 @@ func TestIssue735(t *testing.T) {
142142
{
143143
name: "additional properties false",
144144
schema: &Schema{AdditionalProperties: AdditionalProperties{
145-
Has: BoolPtr(false),
145+
Has: Ptr(false),
146146
}},
147147
value: map[string]any{"foo": 42},
148148
extraNotContains: []any{42},
@@ -188,40 +188,40 @@ func TestIssue735(t *testing.T) {
188188
{
189189
name: "one of (matches more then one)",
190190
schema: NewOneOfSchema(
191-
&Schema{MultipleOf: Float64Ptr(6)},
192-
&Schema{MultipleOf: Float64Ptr(7)},
191+
&Schema{MultipleOf: Ptr(6.0)},
192+
&Schema{MultipleOf: Ptr(7.0)},
193193
),
194194
value: 42,
195195
},
196196
{
197197
name: "one of (no matches)",
198198
schema: NewOneOfSchema(
199-
&Schema{MultipleOf: Float64Ptr(5)},
200-
&Schema{MultipleOf: Float64Ptr(10)},
199+
&Schema{MultipleOf: Ptr(5.0)},
200+
&Schema{MultipleOf: Ptr(10.0)},
201201
),
202202
value: 42,
203203
},
204204
{
205205
name: "any of",
206206
schema: NewAnyOfSchema(
207-
&Schema{MultipleOf: Float64Ptr(5)},
208-
&Schema{MultipleOf: Float64Ptr(10)},
207+
&Schema{MultipleOf: Ptr(5.0)},
208+
&Schema{MultipleOf: Ptr(10.0)},
209209
),
210210
value: 42,
211211
},
212212
{
213213
name: "all of (match some)",
214214
schema: NewAllOfSchema(
215-
&Schema{MultipleOf: Float64Ptr(6)},
216-
&Schema{MultipleOf: Float64Ptr(5)},
215+
&Schema{MultipleOf: Ptr(6.0)},
216+
&Schema{MultipleOf: Ptr(5.0)},
217217
),
218218
value: 42,
219219
},
220220
{
221221
name: "all of (no match)",
222222
schema: NewAllOfSchema(
223-
&Schema{MultipleOf: Float64Ptr(10)},
224-
&Schema{MultipleOf: Float64Ptr(5)},
223+
&Schema{MultipleOf: Ptr(10.0)},
224+
&Schema{MultipleOf: Ptr(5.0)},
225225
),
226226
value: 42,
227227
},

openapi3/schema.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,12 @@ func (schema *Schema) WithMaxProperties(i int64) *Schema {
837837
}
838838

839839
func (schema *Schema) WithAnyAdditionalProperties() *Schema {
840-
schema.AdditionalProperties = AdditionalProperties{Has: BoolPtr(true)}
840+
schema.AdditionalProperties = AdditionalProperties{Has: Ptr(true)}
841841
return schema
842842
}
843843

844844
func (schema *Schema) WithoutAdditionalProperties() *Schema {
845-
schema.AdditionalProperties = AdditionalProperties{Has: BoolPtr(false)}
845+
schema.AdditionalProperties = AdditionalProperties{Has: Ptr(false)}
846846
return schema
847847
}
848848

openapi3/schema_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ var schemaExamples = []schemaExample{
542542
Schema: &Schema{
543543
Type: &Types{"array"},
544544
MinItems: 2,
545-
MaxItems: Uint64Ptr(3),
545+
MaxItems: Ptr(uint64(3)),
546546
UniqueItems: true,
547547
Items: NewFloat64Schema().NewRef(),
548548
},
@@ -872,7 +872,7 @@ var schemaExamples = []schemaExample{
872872
Title: "OBJECT",
873873
Schema: &Schema{
874874
Type: &Types{"object"},
875-
MaxProps: Uint64Ptr(2),
875+
MaxProps: Ptr(uint64(2)),
876876
Properties: Schemas{
877877
"numberProperty": NewFloat64Schema().NewRef(),
878878
},
@@ -944,7 +944,7 @@ var schemaExamples = []schemaExample{
944944
{
945945
Schema: &Schema{
946946
Type: &Types{"object"},
947-
AdditionalProperties: AdditionalProperties{Has: BoolPtr(true)},
947+
AdditionalProperties: AdditionalProperties{Has: Ptr(true)},
948948
},
949949
Serialization: map[string]any{
950950
"type": "object",

openapi3filter/req_resp_decoder_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
)
2323

2424
var (
25-
explode = openapi3.BoolPtr(true)
26-
noExplode = openapi3.BoolPtr(false)
25+
explode = openapi3.Ptr(true)
26+
noExplode = openapi3.Ptr(false)
2727
arrayOf = func(items *openapi3.SchemaRef) *openapi3.SchemaRef {
2828
return &openapi3.SchemaRef{Value: &openapi3.Schema{Type: &openapi3.Types{"array"}, Items: items}}
2929
}
@@ -1712,7 +1712,7 @@ func TestDecodeBody(t *testing.T) {
17121712
WithProperty("b", openapi3.NewIntegerSchema()).
17131713
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
17141714
encoding: map[string]*openapi3.Encoding{
1715-
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: openapi3.BoolPtr(false)},
1715+
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: openapi3.Ptr(false)},
17161716
},
17171717
want: map[string]any{"a": "a1", "b": int64(10), "c": []any{"c1", "c2"}},
17181718
},
@@ -1725,7 +1725,7 @@ func TestDecodeBody(t *testing.T) {
17251725
WithProperty("b", openapi3.NewIntegerSchema()).
17261726
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
17271727
encoding: map[string]*openapi3.Encoding{
1728-
"c": {Style: openapi3.SerializationPipeDelimited, Explode: openapi3.BoolPtr(false)},
1728+
"c": {Style: openapi3.SerializationPipeDelimited, Explode: openapi3.Ptr(false)},
17291729
},
17301730
want: map[string]any{"a": "a1", "b": int64(10), "c": []any{"c1", "c2"}},
17311731
},

0 commit comments

Comments
 (0)