Skip to content

Commit

Permalink
feat #582: use OpenAPI minor version during validation if available
Browse files Browse the repository at this point in the history
  • Loading branch information
cboitel committed Jan 17, 2023
1 parent 26029ec commit 6be685d
Show file tree
Hide file tree
Showing 8 changed files with 940 additions and 470 deletions.
3 changes: 3 additions & 0 deletions .github/docs/openapi3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ type Schema struct{ ... }
func NewDateSchema() *Schema
func NewDateTimeSchema() *Schema
func NewFloat64Schema() *Schema
func NewHostnameSchema() *Schema
func NewIPv4Schema() *Schema
func NewIPv6Schema() *Schema
func NewInt32Schema() *Schema
func NewInt64Schema() *Schema
func NewIntegerSchema() *Schema
Expand Down
21 changes: 21 additions & 0 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,34 @@ func NewDateTimeSchema() *Schema {
}
}

func NewHostnameSchema() *Schema {
return &Schema{
Type: TypeString,
Format: "hostname",
}
}

func NewUUIDSchema() *Schema {
return &Schema{
Type: TypeString,
Format: "uuid",
}
}

func NewIPv4Schema() *Schema {
return &Schema{
Type: TypeString,
Format: "ipv4",
}
}

func NewIPv6Schema() *Schema {
return &Schema{
Type: TypeString,
Format: "ipv6",
}
}

func NewBytesSchema() *Schema {
return &Schema{
Type: TypeString,
Expand Down
12 changes: 12 additions & 0 deletions openapi3/schema_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ func init() {

// defined as date-time in https://www.rfc-editor.org/rfc/rfc3339#section-5.6
DefineStringFormat("date-time", `^[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T(23:59:60|(([01][0-9]|2[0-3])(:[0-5][0-9]){2}))(\.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)

// defined as uuid in https://www.rfc-editor.org/rfc/rfc4122
DefineStringFormatStartingWithOpenAPIMinorVersion("uuid", 1, FormatOfStringForUUIDOfRFC4122, true)

// defined as ipv4 in
DefineStringFormatCallbackStartingWithOpenAPIMinorVersion("ipv4", 1, validateIPv4, true)

// defined as ipv6 in https://www.rfc-editor.org/rfc/rfc4122
DefineStringFormatCallbackStartingWithOpenAPIMinorVersion("ipv6", 1, validateIPv6, true)

// hostname as defined in https://www.rfc-editor.org/rfc/rfc1123#section-2.1
DefineStringFormatStartingWithOpenAPIMinorVersion(`hostname`, 1, `^[a-zA-Z0-9][a-zA-Z0-9-.]+[a-zA-Z0-9]$`, true)
}

// DefineIPv4Format opts in ipv4 format validation on top of OAS 3 spec
Expand Down
16 changes: 4 additions & 12 deletions openapi3/schema_formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ func TestIssue430(t *testing.T) {
NewStringSchema().WithFormat("ipv6"),
)

delete(SchemaStringFormats, "ipv4")
delete(SchemaStringFormats, "ipv6")

err := schema.Validate(context.Background())
require.NoError(t, err)

Expand Down Expand Up @@ -46,11 +43,8 @@ func TestIssue430(t *testing.T) {
require.Error(t, err, ErrOneOfConflict.Error())
}

DefineIPv4Format()
DefineIPv6Format()

for datum, isV4 := range data {
err = schema.VisitJSON(datum)
err = schema.VisitJSON(datum, SetOpenAPIMinorVersion(1))
require.NoError(t, err)
if isV4 {
require.Nil(t, validateIPv4(datum), "%q should be IPv4", datum)
Expand Down Expand Up @@ -78,8 +72,6 @@ func TestFormatCallback_WrapError(t *testing.T) {
}

func TestReversePathInMessageSchemaError(t *testing.T) {
DefineIPv4Format()

SchemaErrorDetailsDisabled = true

const spc = `
Expand All @@ -99,11 +91,11 @@ components:

err = doc.Components.Schemas["Something"].Value.VisitJSON(map[string]interface{}{
`ip`: `123.0.0.11111`,
})
}, SetOpenAPIMinorVersion(1))

require.EqualError(t, err, `Error at "/ip": Not an IP address`)
// assert, do not require to ensure SchemaErrorDetailsDisabled can be set to false
assert.ErrorContains(t, err, `Error at "/ip"`)

delete(SchemaStringFormats, "ipv4")
SchemaErrorDetailsDisabled = false
}

Expand Down
Loading

0 comments on commit 6be685d

Please sign in to comment.