Skip to content

Commit

Permalink
skip operations that returns 1xx or 3xx response codes (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Sep 3, 2024
1 parent fe23190 commit f34e81c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
24 changes: 17 additions & 7 deletions openapi/internal/oas2_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"slices"
"strconv"

rest "github.com/hasura/ndc-rest-schema/schema"
"github.com/hasura/ndc-rest-schema/utils"
Expand Down Expand Up @@ -35,7 +36,7 @@ func (oc *oas2OperationBuilder) BuildFunction(pathKey string, operation *v2.Oper
if funcName == "" {
funcName = buildPathMethodName(pathKey, "get", oc.builder.ConvertOptions)
}
oc.builder.Logger.Debug("function",
oc.builder.Logger.Info("function",
slog.String("name", funcName),
slog.String("path", pathKey),
)
Expand Down Expand Up @@ -101,7 +102,7 @@ func (oc *oas2OperationBuilder) BuildProcedure(pathKey string, method string, op
procName = buildPathMethodName(pathKey, method, oc.builder.ConvertOptions)
}

oc.builder.Logger.Debug("procedure",
oc.builder.Logger.Info("procedure",
slog.String("name", procName),
slog.String("path", pathKey),
slog.String("method", method),
Expand Down Expand Up @@ -295,13 +296,22 @@ func (oc *oas2OperationBuilder) convertResponse(responses *v2.Responses, apiPath

var resp *v2.Response
if responses.Codes == nil || responses.Codes.IsZero() {
// the response is alway success
// the response is always successful
resp = responses.Default
} else {
for _, code := range []string{"200", "201", "204"} {
res := responses.Codes.GetOrZero(code)
if res != nil {
resp = res
for r := responses.Codes.First(); r != nil; r = r.Next() {
if r.Key() == "" {
continue
}
code, err := strconv.ParseInt(r.Key(), 10, 32)
if err != nil {
continue
}

if isUnsupportedResponseCodes(code) {
return nil, nil
} else if code >= 200 && code < 300 {
resp = r.Value()
break
}
}
Expand Down
26 changes: 19 additions & 7 deletions openapi/internal/oas3_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log/slog"
"strconv"
"strings"

rest "github.com/hasura/ndc-rest-schema/schema"
Expand Down Expand Up @@ -36,7 +37,7 @@ func (oc *oas3OperationBuilder) BuildFunction(itemGet *v3.Operation) (*rest.REST
funcName = buildPathMethodName(oc.pathKey, "get", oc.builder.ConvertOptions)
}

oc.builder.Logger.Debug("function",
oc.builder.Logger.Info("function",
slog.String("name", funcName),
slog.String("path", oc.pathKey),
slog.String("method", oc.method),
Expand Down Expand Up @@ -87,7 +88,7 @@ func (oc *oas3OperationBuilder) BuildProcedure(operation *v3.Operation) (*rest.R
procName = buildPathMethodName(oc.pathKey, oc.method, oc.builder.ConvertOptions)
}

oc.builder.Logger.Debug("procedure",
oc.builder.Logger.Info("procedure",
slog.String("name", procName),
slog.String("path", oc.pathKey),
slog.String("method", oc.method),
Expand Down Expand Up @@ -329,11 +330,22 @@ func (oc *oas3OperationBuilder) convertResponse(responses *v3.Responses, apiPath
}

var resp *v3.Response
for _, code := range []int{200, 201, 204} {
res := responses.FindResponseByCode(code)
if res != nil {
resp = res
break
if responses.Codes != nil && !responses.Codes.IsZero() {
for r := responses.Codes.First(); r != nil; r = r.Next() {
if r.Key() == "" {
continue
}
code, err := strconv.ParseInt(r.Key(), 10, 32)
if err != nil {
continue
}

if isUnsupportedResponseCodes(code) {
return nil, nil, nil
} else if code >= 200 && code < 300 {
resp = r.Value()
break
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions openapi/internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,8 @@ func formatWriteObjectName(name string) string {
func errParameterSchemaEmpty(fieldPaths []string) error {
return fmt.Errorf("parameter schema of $.%s is empty", strings.Join(fieldPaths, "."))
}

// redirection and information response status codes aren't supported
func isUnsupportedResponseCodes[T int | int64](code T) bool {
return code < 200 || (code >= 300 && code < 400)
}
8 changes: 8 additions & 0 deletions openapi/testdata/petstore2/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,14 @@
"responses": { "default": { "description": "successful operation" } }
}
},
"/user/logout-redirect": {
"get": {
"operationId": "logoutUserRedirect",
"produces": [],
"parameters": [],
"responses": { "302": { "description": "successful operation" } }
}
},
"/user": {
"post": {
"tags": ["user"],
Expand Down
18 changes: 18 additions & 0 deletions openapi/testdata/petstore3/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,19 @@
}
}
}
},
"/self-service/browser/flows/logout": {
"get": {
"description": "This endpoint initializes a logout flow.\n\n\u003e This endpoint is NOT INTENDED for API clients and only works\nwith browsers (Chrome, Firefox, ...).\n\nOn successful logout, the browser will be redirected (HTTP 302 Found) to the `return_to` parameter of the initial request\nor fall back to `urls.default_return_to`.\n\nMore information can be found at [Ory Kratos User Logout Documentation](https://www.ory.sh/docs/next/kratos/self-service/flows/user-logout).",
"operationId": "initializeSelfServiceBrowserLogoutFlow",
"responses": {
"302": {
"$ref": "#/components/responses/emptyResponse"
}
},
"summary": "Initialize Browser-Based Logout User Flow",
"tags": ["public"]
}
}
},
"components": {
Expand Down Expand Up @@ -3280,6 +3293,11 @@
}
}
},
"responses": {
"emptyResponse": {
"description": "Empty responses are sent when, for example, resources are deleted. The HTTP status code for empty responses is\ntypically 201."
}
},
"securitySchemes": {
"basic": {
"type": "http",
Expand Down

0 comments on commit f34e81c

Please sign in to comment.