Skip to content

Commit

Permalink
rebased
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Aug 30, 2023
1 parent 58396db commit 7ecc3c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
6 changes: 3 additions & 3 deletions openapi3/issue819_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ paths:
err = doc.Validate(sl.Context)
require.NoError(t, err)

require.NotNil(t, doc.Paths["/v1/operation"].Get.Responses.Get(201))
require.Nil(t, doc.Paths["/v1/operation"].Get.Responses.Get(404))
require.Nil(t, doc.Paths["/v1/operation"].Get.Responses.Get(999))
require.NotNil(t, doc.Paths.Value("/v1/operation").Get.Responses.Status(201))
require.Nil(t, doc.Paths.Value("/v1/operation").Get.Responses.Status(404))
require.Nil(t, doc.Paths.Value("/v1/operation").Get.Responses.Status(999))
}
43 changes: 15 additions & 28 deletions openapi3/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,6 @@ func NewResponsesWithCapacity(cap int) *Responses {
return &Responses{m: make(map[string]*ResponseRef, cap)}
}

// Get returns a ResponseRef for the given status
// If an exact match isn't initially found a patterned field is checked using
// the first digit to determine the range (eg: 201 to 2XX)
// See https://spec.openapis.org/oas/v3.0.3#patterned-fields-0
func (responses Responses) Get(status int) *ResponseRef {
st := strconv.FormatInt(int64(status), 10)
if rref, ok := responses[st]; ok {
return rref
}
st = string(st[0]) + "XX"
switch st {
case "1XX":
return responses["1XX"]
case "2XX":
return responses["2XX"]
case "3XX":
return responses["3XX"]
case "4XX":
return responses["4XX"]
case "5XX":
return responses["5XX"]
default:
return nil
}
}

// NewResponses builds a responses object with response objects in insertion order.
func NewResponses(opts ...NewResponsesOption) *Responses {
responses := NewResponsesWithCapacity(len(opts))
Expand Down Expand Up @@ -84,9 +58,22 @@ func (responses *Responses) Default() *ResponseRef {
return responses.Value("default")
}

// Status returns the response that exactly matches the given status, or nil.
// Status returns a ResponseRef for the given status
// If an exact match isn't initially found a patterned field is checked using
// the first digit to determine the range (eg: 201 to 2XX)
// See https://spec.openapis.org/oas/v3.0.3#patterned-fields-0
func (responses *Responses) Status(status int) *ResponseRef {
return responses.Value(strconv.FormatInt(int64(status), 10))
st := strconv.FormatInt(int64(status), 10)
if rref, ok := responses.Get(st); ok {
return rref
}
st = string(st[0]) + "XX" //fixme status $ 100
switch st {
case "1XX", "2XX", "3XX", "4XX", "5XX":
return responses.Value(st)
default:
return nil
}
}

// FIXME Any HTTP status code can be used as the property name, but only one property per code, to describe the expected response for that HTTP status code. A Reference Object can link to a response that is defined in the OpenAPI Object's components/responses section. This field MUST be enclosed in quotation marks (for example, "200") for compatibility between JSON and YAML. To define a range of response codes, this field MAY contain the uppercase wildcard character X. For example, 2XX represents all response codes between [200-299]. Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX. If a response is defined using an explicit code, the explicit code definition takes precedence over the range definition for that code.
Expand Down

0 comments on commit 7ecc3c1

Please sign in to comment.