From 5a832e41bfef6693df2f1f43f16d7fe721e1c0f1 Mon Sep 17 00:00:00 2001 From: Claude Code Date: Fri, 27 Nov 2026 15:33:10 -0500 Subject: [PATCH] Decode ErrorRS.errorCode as an integer in the OpenAPI client ReportPortal writes ErrorRS.errorCode as a number (ErrorTypeSerializer calls writeNumber(error.getCode())), but the upstream spec declares it as a string enum. The generated client therefore failed to decode every error response declared as ErrorRS and returned "json: cannot unmarshal number into Go struct field ...errorCode of type string" instead of the HTTP status and the server's error model. Override the property in scripts/openapi-preprocess.jq (integer/int64, same as the spec's own Error schema) and regenerate the client. The filter now fails if the schema or property disappears upstream. GetErrorCode/SetErrorCode/GetErrorCodeOk of ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS change from string to int64. Fixes #31 Co-Authored-By: Claude Fable 5.2 --- pkg/gorp/openapi_errors_test.go | 128 ++++++++++++++++++ pkg/openapi/api/openapi.yaml | 109 +-------------- ...infrastructure_rules_exception_error_rs.go | 13 +- scripts/openapi-preprocess.jq | 18 +++ 4 files changed, 160 insertions(+), 108 deletions(-) create mode 100644 pkg/gorp/openapi_errors_test.go diff --git a/pkg/gorp/openapi_errors_test.go b/pkg/gorp/openapi_errors_test.go new file mode 100644 index 0000000..03d5a45 --- /dev/null +++ b/pkg/gorp/openapi_errors_test.go @@ -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()) + }) + } +} diff --git a/pkg/openapi/api/openapi.yaml b/pkg/openapi/api/openapi.yaml index 1fca833..cc3b656 100644 --- a/pkg/openapi/api/openapi.yaml +++ b/pkg/openapi/api/openapi.yaml @@ -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: @@ -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: diff --git a/pkg/openapi/model_com_epam_reportportal_base_infrastructure_rules_exception_error_rs.go b/pkg/openapi/model_com_epam_reportportal_base_infrastructure_rules_exception_error_rs.go index 50aa7cc..1ce032e 100644 --- a/pkg/openapi/model_com_epam_reportportal_base_infrastructure_rules_exception_error_rs.go +++ b/pkg/openapi/model_com_epam_reportportal_base_infrastructure_rules_exception_error_rs.go @@ -20,7 +20,8 @@ var _ MappedNullable = &ComEpamReportportalBaseInfrastructureRulesExceptionError // ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS struct for ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS type ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS struct { - ErrorCode *string `json:"errorCode,omitempty"` + // Numeric ReportPortal error code (ErrorType code), e.g. 4041 for LAUNCH_NOT_FOUND + ErrorCode *int64 `json:"errorCode,omitempty"` Message *string `json:"message,omitempty"` StackTrace *string `json:"stackTrace,omitempty"` } @@ -43,9 +44,9 @@ func NewComEpamReportportalBaseInfrastructureRulesExceptionErrorRSWithDefaults() } // GetErrorCode returns the ErrorCode field value if set, zero value otherwise. -func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) GetErrorCode() string { +func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) GetErrorCode() int64 { if o == nil || IsNil(o.ErrorCode) { - var ret string + var ret int64 return ret } return *o.ErrorCode @@ -53,7 +54,7 @@ func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) GetErrorCod // GetErrorCodeOk returns a tuple with the ErrorCode field value if set, nil otherwise // and a boolean to check if the value has been set. -func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) GetErrorCodeOk() (*string, bool) { +func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) GetErrorCodeOk() (*int64, bool) { if o == nil || IsNil(o.ErrorCode) { return nil, false } @@ -69,8 +70,8 @@ func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) HasErrorCod return false } -// SetErrorCode gets a reference to the given string and assigns it to the ErrorCode field. -func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) SetErrorCode(v string) { +// SetErrorCode gets a reference to the given int64 and assigns it to the ErrorCode field. +func (o *ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS) SetErrorCode(v int64) { o.ErrorCode = &v } diff --git a/scripts/openapi-preprocess.jq b/scripts/openapi-preprocess.jq index e812fb4..66979e7 100644 --- a/scripts/openapi-preprocess.jq +++ b/scripts/openapi-preprocess.jq @@ -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.