Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions pkg/gorp/openapi_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package gorp

import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/reportportal/goRP/v5/pkg/openapi"
)

// ReportPortal writes ErrorRS.errorCode as a number (see ErrorRS.ErrorTypeSerializer in service-api),
// so the generated client has to decode error bodies of this shape. The bodies below are what a real
// server returns.
func TestOpenAPIClientDecodesErrorResponse(t *testing.T) {
t.Parallel()

const project = "prj"

tests := []struct {
name string
status int
body string
// declared tells whether the spec declares an error model for this status of the operation.
// When it doesn't, the generated client leaves Model() empty and the caller decodes Body().
// Such cases accept both, so a spec refresh that declares the status doesn't break the test.
declared bool
call func(c *Client) error
wantCode int64
wantMsg string
}{
{
name: "launch not found, 404 is not declared for the operation",
status: http.StatusNotFound,
body: `{"errorCode":4041,"message":"Launch '1' not found. Did you use correct Launch ID?"}`,
call: func(c *Client) error {
_, _, err := c.LaunchAPI.GetLaunch(t.Context(), "1", project).Execute()

return err
},
wantCode: 4041,
wantMsg: "Launch '1' not found. Did you use correct Launch ID?",
},
{
name: "activity not found, 404 is declared as ErrorRS",
status: http.StatusNotFound,
body: `{"errorCode":40411,"message":"Activity '1' not found. Did you use correct Activity ID?"}`,
declared: true,
call: func(c *Client) error {
_, _, err := c.ActivityAPI.GetActivity(t.Context(), 1, project).Execute()

return err
},
wantCode: 40411,
wantMsg: "Activity '1' not found. Did you use correct Activity ID?",
},
{
name: "incorrect sorting parameter, 400 is declared as ErrorRS",
status: http.StatusBadRequest,
body: `{"errorCode":40012,"message":"Sorting parameter noSuchField is not defined"}`,
declared: true,
call: func(c *Client) error {
_, _, err := c.LaunchAPI.GetProjectLaunches(t.Context(), project).
PageSort("noSuchField,asc").
Execute()

return err
},
wantCode: 40012,
wantMsg: "Sorting parameter noSuchField is not defined",
},
{
name: "delete launch fails on the server, 500 is declared as ErrorRS",
status: http.StatusInternalServerError,
body: `{"errorCode":5000,"message":"Unclassified error [TransientObjectException]"}`,
declared: true,
call: func(c *Client) error {
_, _, err := c.LaunchAPI.DeleteLaunch(t.Context(), 80, project).Execute()

return err
},
wantCode: 5000,
wantMsg: "Unclassified error [TransientObjectException]",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tt.status)
w.Write([]byte(tt.body))
}),
)
defer srv.Close()

u, err := url.Parse(srv.URL)
require.NoError(t, err)

err = tt.call(NewClient(u, WithApiKeyAuth(t.Context(), "uuid")))
require.Error(t, err)

var apiErr *openapi.GenericOpenAPIError
require.ErrorAs(t, err, &apiErr)
assert.JSONEq(t, tt.body, string(apiErr.Body()))
// a decoding failure would replace the HTTP status in the error text
assert.Contains(t, err.Error(), http.StatusText(tt.status))
assert.NotContains(t, err.Error(), "cannot unmarshal")

errorRS, ok := apiErr.Model().(openapi.ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS)
if tt.declared {
require.Truef(t, ok, "unexpected error model %T", apiErr.Model())
} else if !ok {
require.NoError(t, json.Unmarshal(apiErr.Body(), &errorRS))
}

assert.Equal(t, tt.wantCode, errorRS.GetErrorCode())
assert.Equal(t, tt.wantMsg, errorRS.GetMessage())
})
}
}
109 changes: 7 additions & 102 deletions pkg/openapi/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26639,110 +26639,15 @@ components:
schemas:
com.epam.reportportal.base.infrastructure.rules.exception.ErrorRS:
example:
errorCode: INCORRECT_REQUEST
errorCode: 0
message: message
stackTrace: stackTrace
properties:
errorCode:
enum:
- INCORRECT_REQUEST
- BINARY_DATA_CANNOT_BE_SAVED
- ACCESS_DENIED
- ADDRESS_LOCKED
- UPSA_USER_DENIED
- PAYMENT_REQUIRED
- PAID_PLUGIN_REQUIRED
- PROJECT_NOT_FOUND
- LAUNCH_NOT_FOUND
- TEST_SUITE_NOT_FOUND
- TEST_ITEM_NOT_FOUND
- LOG_NOT_FOUND
- ROLE_NOT_FOUND
- USER_NOT_FOUND
- WIDGET_NOT_FOUND
- WIDGET_NOT_FOUND_IN_DASHBOARD
- DASHBOARD_NOT_FOUND
- USER_FILTER_NOT_FOUND
- ACTIVITY_NOT_FOUND
- UNABLE_TO_CREATE_WIDGET
- INTEGRATION_NOT_FOUND
- PROJECT_NOT_CONFIGURED
- SERVER_SETTINGS_NOT_FOUND
- ISSUE_TYPE_NOT_FOUND
- PROJECT_SETTINGS_NOT_FOUND
- TICKET_NOT_FOUND
- AUTH_INTEGRATION_NOT_FOUND
- WIDGET_NOT_FOUND_IN_PROJECT
- USER_FILTER_NOT_FOUND_IN_PROJECT
- DASHBOARD_NOT_FOUND_IN_PROJECT
- PATTERN_TEMPLATE_NOT_FOUND_IN_PROJECT
- TEST_ITEM_OR_LAUNCH_NOT_FOUND
- ANALYZER_NOT_FOUND
- ATTACHMENT_NOT_FOUND
- UNABLE_TO_LOAD_BINARY_DATA
- CLUSTER_NOT_FOUND
- ORGANIZATION_NOT_FOUND
- NOT_FOUND
- INTEGRATION_TYPE_NOT_FOUND
- INCORRECT_FILTER_PARAMETERS
- INCORRECT_SORTING_PARAMETERS
- INCORRECT_INTEGRATION_NAME
- UNABLE_MODIFY_SHARABLE_RESOURCE
- INCORRECT_AUTHENTICATION_TYPE
- UNABLE_POST_TICKET
- UNABLE_ASSIGN_UNASSIGN_USER_TO_PROJECT
- EMAIL_CONFIGURATION_IS_INCORRECT
- PROJECT_UPDATE_NOT_ALLOWED
- UNABLE_TO_UPDATE_YOURSELF_ROLE
- FORBIDDEN_OPERATION
- RESOURCE_ALREADY_EXISTS
- UNABLE_INTERACT_WITH_INTEGRATION
- ROLE_ALREADY_EXISTS_ERROR
- USER_ALREADY_EXISTS
- USER_FILTER_ALREADY_EXISTS
- PROJECT_ALREADY_EXISTS
- DASHBOARD_UPDATE_ERROR
- UNABLE_LOAD_WIDGET_CONTENT
- UNABLE_ADD_TO_FAVORITE
- INTEGRATION_ALREADY_EXISTS
- SERVER_SETTINGS_ALREADY_EXISTS
- USER_ALREADY_ASSIGNED
- UNABLE_REMOVE_FROM_FAVORITE
- LAUNCH_IS_NOT_FINISHED
- TEST_ITEM_IS_NOT_FINISHED
- INCORRECT_FINISH_STATUS
- BAD_REQUEST_ERROR
- BAD_SAVE_LOG_REQUEST
- REPORTING_ITEM_ALREADY_FINISHED
- AMBIGUOUS_TEST_ITEM_STATUS
- FAILED_TEST_ITEM_ISSUE_TYPE_DEFINITION
- FINISH_TIME_EARLIER_THAN_START_TIME
- FINISH_ITEM_NOT_ALLOWED
- FINISH_LAUNCH_NOT_ALLOWED
- START_ITEM_NOT_ALLOWED
- CHILD_START_TIME_EARLIER_THAN_PARENT
- UNSUPPORTED_TEST_ITEM_TYPE
- LOGGING_IS_NOT_ALLOWED
- BAD_SAVE_WIDGET_REQUEST
- BAD_UPDATE_WIDGET_REQUEST
- UNABLE_LOAD_TEST_ITEM_HISTORY
- BAD_SAVE_USER_FILTER_REQUEST
- RETRIES_HANDLER_ERROR
- IMPORT_FILE_ERROR
- PARSING_XML_ERROR
- OBJECT_RETRIEVAL_ERROR
- PLUGIN_UPLOAD_ERROR
- PLUGIN_REMOVE_ERROR
- UNABLE_TO_SAVE_CHILD_ITEM_FOR_THE_RETRY
- PATTERN_ANALYSIS_ERROR
- PROJECT_DOESNT_CONTAIN_USER
- UNCLASSIFIED_REPORT_PORTAL_ERROR
- BAD_UPDATE_PREFERENCE_REQUEST
- UNSUPPORTED_MERGE_STRATEGY_TYPE
- DEMO_DATA_GENERATION_ERROR
- ATTRIBUTE_NOT_FOUND
- UNCLASSIFIED_ERROR
type: string
description: "Numeric ReportPortal error code (ErrorType code), e.g. 4041\
\ for LAUNCH_NOT_FOUND"
format: int64
type: integer
message:
type: string
stackTrace:
Expand Down Expand Up @@ -38816,10 +38721,10 @@ components:
- 0
- 0
errors:
- errorCode: INCORRECT_REQUEST
- errorCode: 0
message: message
stackTrace: stackTrace
- errorCode: INCORRECT_REQUEST
- errorCode: 0
message: message
stackTrace: stackTrace
properties:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions scripts/openapi-preprocess.jq
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ walk(
)
)

# Fix: the spec declares ErrorRS.errorCode as a string enum of ErrorType names ("LAUNCH_NOT_FOUND"),
# but the server writes the numeric code: ErrorRS.java annotates the field with
# @JsonSerialize(using = ErrorTypeSerializer.class), which calls writeNumber(error.getCode()),
# so the wire format is {"errorCode":4041,"message":"..."}. With the string type the generated
# client fails to decode every such error response ("cannot unmarshal number into ... errorCode
# of type string") and the server's message is lost. Declare it the same way as the spec's own
# `Error` schema does (integer/int64). Fails loudly if upstream renames the schema or the property.
| "com.epam.reportportal.base.infrastructure.rules.exception.ErrorRS" as $errorRS
| if .components.schemas[$errorRS].properties.errorCode then
.components.schemas[$errorRS].properties.errorCode = {
type: "integer",
format: "int64",
description: "Numeric ReportPortal error code (ErrorType code), e.g. 4041 for LAUNCH_NOT_FOUND"
}
else
error("openapi-preprocess: schema \($errorRS) has no errorCode property; update the errorCode override")
end

# Fix: the Go openapi-generator does not call the parent's ToMap() from a child's
# ToMap() when allOf inheritance is used, so discriminator fields (e.g.
# manualScenarioType) are silently dropped from MarshalJSON output.
Expand Down
Loading