Skip to content

Commit

Permalink
openapi3: use Ptr instead of BoolPtr,Float64Ptr,Int64Ptr,Uint64Ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Nov 26, 2024
1 parent e230c13 commit cbe9616
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 27 deletions.
11 changes: 11 additions & 0 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ FUNCTIONS
func BoolPtr(value bool) *bool
BoolPtr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func DefaultRefNameResolver(doc *T, ref ComponentRef) string
DefaultRefResolver is a default implementation of refNameResolver for the
InternalizeRefs function.
Expand Down Expand Up @@ -144,9 +146,16 @@ func DefineStringFormatValidator(name string, validator StringFormatValidator)
func Float64Ptr(value float64) *float64
Float64Ptr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func Int64Ptr(value int64) *int64
Int64Ptr is a helper for defining OpenAPI schemas.

Deprecated: Use Ptr instead.

func Ptr[T any](value T) *T
Ptr is a helper for defining OpenAPI schemas.

func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error)
ReadFromFile is a ReadFromURIFunc which reads local file URIs.

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

Deprecated: Use Ptr instead.

func ValidateIdentifier(value string) error
ValidateIdentifier returns an error if the given component name does not
match IdentifierRegExp.
Expand Down
8 changes: 4 additions & 4 deletions openapi3/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func encoding() *Encoding {
},
},
Style: "form",
Explode: BoolPtr(true),
Explode: Ptr(true),
AllowReserved: true,
}
}
Expand All @@ -74,17 +74,17 @@ func TestEncodingSerializationMethod(t *testing.T) {
},
{
name: "encoding with explode",
enc: &Encoding{Explode: BoolPtr(true)},
enc: &Encoding{Explode: Ptr(true)},
want: &SerializationMethod{Style: SerializationForm, Explode: true},
},
{
name: "encoding with no explode",
enc: &Encoding{Explode: BoolPtr(false)},
enc: &Encoding{Explode: Ptr(false)},
want: &SerializationMethod{Style: SerializationForm, Explode: false},
},
{
name: "encoding with style and explode ",
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: BoolPtr(false)},
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: Ptr(false)},
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: false},
},
}
Expand Down
13 changes: 13 additions & 0 deletions openapi3/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,35 @@ func ValidateIdentifier(value string) error {
return fmt.Errorf("identifier %q is not supported by OpenAPIv3 standard (charset: [%q])", value, identifierChars)
}

// Ptr is a helper for defining OpenAPI schemas.
func Ptr[T any](value T) *T {
return &value
}

// Float64Ptr is a helper for defining OpenAPI schemas.
//
// Deprecated: Use Ptr instead.
func Float64Ptr(value float64) *float64 {
return &value
}

// BoolPtr is a helper for defining OpenAPI schemas.
//
// Deprecated: Use Ptr instead.
func BoolPtr(value bool) *bool {
return &value
}

// Int64Ptr is a helper for defining OpenAPI schemas.
//
// Deprecated: Use Ptr instead.
func Int64Ptr(value int64) *int64 {
return &value
}

// Uint64Ptr is a helper for defining OpenAPI schemas.
//
// Deprecated: Use Ptr instead.
func Uint64Ptr(value uint64) *uint64 {
return &value
}
Expand Down
4 changes: 2 additions & 2 deletions openapi3/issue376_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ info:
func TestExclusiveValuesOfValuesAdditionalProperties(t *testing.T) {
schema := &Schema{
AdditionalProperties: AdditionalProperties{
Has: BoolPtr(false),
Has: Ptr(false),
Schema: NewSchemaRef("", &Schema{}),
},
}
Expand All @@ -55,7 +55,7 @@ func TestExclusiveValuesOfValuesAdditionalProperties(t *testing.T) {

schema = &Schema{
AdditionalProperties: AdditionalProperties{
Has: BoolPtr(false),
Has: Ptr(false),
},
}
err = schema.Validate(context.Background())
Expand Down
24 changes: 12 additions & 12 deletions openapi3/issue735_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestIssue735(t *testing.T) {
},
{
name: "multiple of",
schema: &Schema{MultipleOf: Float64Ptr(5.0)},
schema: &Schema{MultipleOf: Ptr(5.0)},
value: 42,
},
{
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestIssue735(t *testing.T) {
{
name: "additional properties false",
schema: &Schema{AdditionalProperties: AdditionalProperties{
Has: BoolPtr(false),
Has: Ptr(false),
}},
value: map[string]any{"foo": 42},
extraNotContains: []any{42},
Expand Down Expand Up @@ -188,40 +188,40 @@ func TestIssue735(t *testing.T) {
{
name: "one of (matches more then one)",
schema: NewOneOfSchema(
&Schema{MultipleOf: Float64Ptr(6)},
&Schema{MultipleOf: Float64Ptr(7)},
&Schema{MultipleOf: Ptr(6.0)},
&Schema{MultipleOf: Ptr(7.0)},
),
value: 42,
},
{
name: "one of (no matches)",
schema: NewOneOfSchema(
&Schema{MultipleOf: Float64Ptr(5)},
&Schema{MultipleOf: Float64Ptr(10)},
&Schema{MultipleOf: Ptr(5.0)},
&Schema{MultipleOf: Ptr(10.0)},
),
value: 42,
},
{
name: "any of",
schema: NewAnyOfSchema(
&Schema{MultipleOf: Float64Ptr(5)},
&Schema{MultipleOf: Float64Ptr(10)},
&Schema{MultipleOf: Ptr(5.0)},
&Schema{MultipleOf: Ptr(10.0)},
),
value: 42,
},
{
name: "all of (match some)",
schema: NewAllOfSchema(
&Schema{MultipleOf: Float64Ptr(6)},
&Schema{MultipleOf: Float64Ptr(5)},
&Schema{MultipleOf: Ptr(6.0)},
&Schema{MultipleOf: Ptr(5.0)},
),
value: 42,
},
{
name: "all of (no match)",
schema: NewAllOfSchema(
&Schema{MultipleOf: Float64Ptr(10)},
&Schema{MultipleOf: Float64Ptr(5)},
&Schema{MultipleOf: Ptr(10.0)},
&Schema{MultipleOf: Ptr(5.0)},
),
value: 42,
},
Expand Down
4 changes: 2 additions & 2 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,12 @@ func (schema *Schema) WithMaxProperties(i int64) *Schema {
}

func (schema *Schema) WithAnyAdditionalProperties() *Schema {
schema.AdditionalProperties = AdditionalProperties{Has: BoolPtr(true)}
schema.AdditionalProperties = AdditionalProperties{Has: Ptr(true)}
return schema
}

func (schema *Schema) WithoutAdditionalProperties() *Schema {
schema.AdditionalProperties = AdditionalProperties{Has: BoolPtr(false)}
schema.AdditionalProperties = AdditionalProperties{Has: Ptr(false)}
return schema
}

Expand Down
6 changes: 3 additions & 3 deletions openapi3/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ var schemaExamples = []schemaExample{
Schema: &Schema{
Type: &Types{"array"},
MinItems: 2,
MaxItems: Uint64Ptr(3),
MaxItems: Ptr(uint64(3)),
UniqueItems: true,
Items: NewFloat64Schema().NewRef(),
},
Expand Down Expand Up @@ -872,7 +872,7 @@ var schemaExamples = []schemaExample{
Title: "OBJECT",
Schema: &Schema{
Type: &Types{"object"},
MaxProps: Uint64Ptr(2),
MaxProps: Ptr(uint64(2)),
Properties: Schemas{
"numberProperty": NewFloat64Schema().NewRef(),
},
Expand Down Expand Up @@ -944,7 +944,7 @@ var schemaExamples = []schemaExample{
{
Schema: &Schema{
Type: &Types{"object"},
AdditionalProperties: AdditionalProperties{Has: BoolPtr(true)},
AdditionalProperties: AdditionalProperties{Has: Ptr(true)},
},
Serialization: map[string]any{
"type": "object",
Expand Down
8 changes: 4 additions & 4 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
)

var (
explode = openapi3.BoolPtr(true)
noExplode = openapi3.BoolPtr(false)
explode = openapi3.Ptr(true)
noExplode = openapi3.Ptr(false)
arrayOf = func(items *openapi3.SchemaRef) *openapi3.SchemaRef {
return &openapi3.SchemaRef{Value: &openapi3.Schema{Type: &openapi3.Types{"array"}, Items: items}}
}
Expand Down Expand Up @@ -1712,7 +1712,7 @@ func TestDecodeBody(t *testing.T) {
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
encoding: map[string]*openapi3.Encoding{
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: openapi3.BoolPtr(false)},
"c": {Style: openapi3.SerializationSpaceDelimited, Explode: openapi3.Ptr(false)},
},
want: map[string]any{"a": "a1", "b": int64(10), "c": []any{"c1", "c2"}},
},
Expand All @@ -1725,7 +1725,7 @@ func TestDecodeBody(t *testing.T) {
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())),
encoding: map[string]*openapi3.Encoding{
"c": {Style: openapi3.SerializationPipeDelimited, Explode: openapi3.BoolPtr(false)},
"c": {Style: openapi3.SerializationPipeDelimited, Explode: openapi3.Ptr(false)},
},
want: map[string]any{"a": "a1", "b": int64(10), "c": []any{"c1", "c2"}},
},
Expand Down

0 comments on commit cbe9616

Please sign in to comment.