Skip to content

openapi: decode ErrorRS.errorCode as an integer - #33

Open
DzmitryHumianiuk wants to merge 1 commit into
masterfrom
fix/openapi-errorcode-integer
Open

DzmitryHumianiuk wants to merge 1 commit into
masterfrom
fix/openapi-errorcode-integer

Conversation

@DzmitryHumianiuk

@DzmitryHumianiuk DzmitryHumianiuk commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #28. Fixes #31 (a duplicate of #28 with a corrected reproduction).

ReportPortal sends errorCode in error responses as a number ({"errorCode":4041,"message":"..."}), but the API spec declares it as a string enum. The generated pkg/openapi client therefore failed to decode error responses and returned json: cannot unmarshal number into Go struct field ...ErrorRS.errorCode of type string instead of the HTTP status and the server's error.

This PR declares errorCode as an integer in the spec preprocessing step and regenerates the client.

Changes

  • scripts/openapi-preprocess.jq: replace ErrorRS.errorCode (string enum) with integer/int64, the same type the spec's own Error schema already uses. The filter stops with an error if the schema or the property disappears upstream, so the override cannot silently go stale.
  • pkg/openapi/ (regenerated, not hand-edited): ErrorRS.ErrorCode is now *int64. Only two generated files change: the model and the embedded api/openapi.yaml.
  • pkg/gorp/openapi_errors_test.go: new httptest test that returns real error bodies and checks the code and message the caller gets through the generated client.

Behaviour change for library users

GetErrorCode(), GetErrorCodeOk() and SetErrorCode() of openapi.ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS change from string to int64. The string version could never hold a value from a real server, so no working code depends on it.

Not changed, found on the way

  • err.Error() is now the HTTP status (404 Not Found), not the server message. The generator only puts Title/Detail fields into the text (pkg/openapi/client.go, formatErrorMessage). The code and message are in GenericOpenAPIError.Model(), and the raw body is in Body().
  • Most operations do not declare a 404 response in the spec (54 of 358 do; no launch operation does). For those, Model() is nil and the caller has to decode Body() itself. "Get a launch that doesn't exist" was never hit by the decode bug for this reason; the bug shows on statuses the spec declares (400, 403, 500, and 404 where declared).
  • 401 responses still fail to decode, for a different reason: the spec declares them as oneOf(ErrorRS, string), but the server sends an OAuth body ({"error":"invalid_token","error_description":"Invalid access token"}). Worth a separate issue.
  • task generate-openapi-client alone leaves 372 files modified, because the committed client is go fmt-formatted and the task does not format its output. Running go fmt ./pkg/openapi/ (or task fmt) afterwards brings the diff to zero. This is not documented in AGENTS.md.
  • .task/checksum/preprocess-schema is tracked in git although .task/ is in .gitignore. It changes whenever the jq filter changes. It is left untouched here.
  • pkg/gorp and internal/ do not decode errorCode anywhere. defaultHTTPErrorHandler (pkg/gorp/util.go:12) keeps the raw body as a string, so the Resty-based clients were not affected.
  • The upstream spec should be corrected in service-api (an @Schema annotation on ErrorRS.errorType). Once it is, the override becomes a no-op.

Investigation log

Paths in service-api refer to develop at 017642e9e. Commands run from the repository root unless noted.

  1. Symptom. e2e cleanup on ReportPortal 6.0.0 printed cleanup: could not delete launch 80: json: cannot unmarshal number into Go struct field ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS.errorCode of type string (issue openapi: error responses can't be decoded (errorCode is a number, spec says string) #31).

  2. Spec says string.
    jq '.components.schemas["com.epam.reportportal.base.infrastructure.rules.exception.ErrorRS"].properties.errorCode | {type, enum_values: (.enum|length)}' reportportal-api-docs.json
    returns {"type":"string","enum_values":97}.
    The generated model had ErrorCode *string (git show origin/master:pkg/openapi/model_com_epam_reportportal_base_infrastructure_rules_exception_error_rs.go, line 23).

  3. Server writes a number. In service-api, src/main/java/com/epam/reportportal/base/infrastructure/rules/exception/ErrorRS.java:

    • line 55: @JsonSerialize(using = ErrorTypeSerializer.class) on the errorCode property;
    • lines 77-81: ErrorTypeSerializer.serialize calls jsonGenerator.writeNumber(error.getCode()).

    Codes are in ErrorType.java in the same package: LAUNCH_NOT_FOUND(4041, ...) at line 73, ACTIVITY_NOT_FOUND(40411, ...) at line 136, INCORRECT_SORTING_PARAMETERS(40012, ...) at line 250, UNCLASSIFIED_ERROR(5000, ...) at line 593.

  4. Other error-like schemas.
    jq -c '.components.schemas | to_entries[] | select(.value.properties.errorCode != null) | {k: .key, type: .value.properties.errorCode.type}' reportportal-api-docs.json
    lists two schemas: ErrorRS (string, wrong) and Error (integer/int64, already right). DeleteBulkRS.errors refers to ErrorRS and is fixed with it. The 401 oneOf(ErrorRS, string) also refers to it, so its ErrorRS branch now accepts a numeric code, but real 401 responses still do not decode (see "Not changed" above and the last block of step 9). The Batch*OperationError schemas have no errorCode.

  5. Which responses reach the broken decode.
    grep -n -A1 "StatusCode == 404" pkg/openapi/api_*.go | grep "var v" shows 404 is declared for 54 operations; grep -c "StatusCode == 404" pkg/openapi/api_launch.go returns 0. So a missing launch (404) is returned without a model, and the decode error appears on declared statuses, such as the 500 in the issue.

  6. Fix choice. Override the schema in scripts/openapi-preprocess.jq (lines 31-47) and regenerate, as AGENTS.md requires. Rejected alternatives:

    • editing pkg/openapi by hand: the next regeneration would undo it;
    • editing reportportal-api-docs.json: it is an upstream snapshot and the next refresh would undo it;
    • int32: enough for today's codes, but int64 matches the spec's own Error.errorCode;
    • a custom type accepting both number and string: the server never sends a string.
  7. Regeneration. Docker was not running; started it with colima start. task is not installed, so it was run as go run github.com/go-task/task/v3/cmd/task@v3.45.4 <task>.

    • Baseline, before changing the filter: task generate-openapi-client, then git status --short showed 372 modified files; after go fmt ./pkg/openapi/ the diff was empty. So the noise is formatting only.
    • With the filter change: task generate-openapi-client && go fmt ./pkg/openapi/, then git status --short shows pkg/openapi/api/openapi.yaml, the ErrorRS model, and the tracked .task/checksum/preprocess-schema (restored with git checkout).
    • Check the preprocessed spec: jq -c '.components.schemas["com.epam.reportportal.base.infrastructure.rules.exception.ErrorRS"].properties.errorCode' openapi-modified.json returns {"type":"integer","format":"int64","description":"..."}.
  8. Test. go test -count=1 -run TestOpenAPIClientDecodesErrorResponse -v ./pkg/gorp/ passes with four cases: launch not found (404, not declared, so decoded from Body(); the test also accepts a model, in case a later spec declares 404), activity not found (404, declared), wrong sort field (400, declared), delete launch failure (500, declared, the case from the issue).
    To see it fail without the fix: git checkout origin/master -- pkg/openapi/model_com_epam_reportportal_base_infrastructure_rules_exception_error_rs.go, run the test (all four cases fail with the cannot unmarshal number text), then git checkout HEAD -- <same file>.

  9. Live check, read-only (the demo instance from the issue, reported there as 6.0.0; the server version was not re-checked. Three GET requests for objects that do not exist, plus one GET with an invalid key after the fix. Nothing was created or deleted.) A throwaway program (not committed) built gorp.NewClient(...) from RP_ENDPOINT/RP_PROJECT/RP_API_KEY and printed err.Error(), GenericOpenAPIError.Body() and Model() for:

    • c.LaunchAPI.GetLaunch(ctx, "999999999", project).Execute()
    • c.ActivityAPI.GetActivity(ctx, 999999999, project).Execute()
    • c.LaunchAPI.GetProjectLaunches(ctx, project).PageSort("noSuchField,asc").PageSize(1).Execute()

    Before the fix (origin/master):

    == GET launch 999999999 (404 is not mapped to a model in the spec)
       http status : 404 Not Found
       err.Error() : 404 Not Found
       raw body    : {"errorCode":4041,"message":"Launch '999999999' not found. Did you use correct Launch ID?"}
       Model()     : <nil>
    == GET activity 999999999 (404 mapped to ErrorRS)
       http status : 404 Not Found
       err.Error() : json: cannot unmarshal number into Go struct field ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS.errorCode of type string
       raw body    : {"errorCode":40411,"message":"Activity '999999999' not found. Did you use correct Activity ID?"}
       Model()     : <nil>
    == GET launches sorted by an unknown field (400 mapped to ErrorRS)
       http status : 400 Bad Request
       err.Error() : json: cannot unmarshal number into Go struct field ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS.errorCode of type string
       raw body    : {"errorCode":40012,"message":"Sorting parameter noSuchField is not defined"}
       Model()     : <nil>
    

    After the fix:

    == GET launch 999999999 (404 is not mapped to a model in the spec)
       http status : 404 Not Found
       err.Error() : 404 Not Found
       raw body    : {"errorCode":4041,"message":"Launch '999999999' not found. Did you use correct Launch ID?"}
       Model()     : <nil>
    == GET activity 999999999 (404 mapped to ErrorRS)
       http status : 404 Not Found
       err.Error() : 404 Not Found
       raw body    : {"errorCode":40411,"message":"Activity '999999999' not found. Did you use correct Activity ID?"}
       Model()     : openapi.ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS
       errorCode   : 40411
       message     : Activity '999999999' not found. Did you use correct Activity ID?
    == GET launches sorted by an unknown field (400 mapped to ErrorRS)
       http status : 400 Bad Request
       err.Error() : 400 Bad Request
       raw body    : {"errorCode":40012,"message":"Sorting parameter noSuchField is not defined"}
       Model()     : openapi.ComEpamReportportalBaseInfrastructureRulesExceptionErrorRS
       errorCode   : 40012
       message     : Sorting parameter noSuchField is not defined
    == GET launches with an invalid API key (401 mapped to oneOf ErrorRS|string)
       http status : 401 Unauthorized
       err.Error() : data failed to match schemas in oneOf(SaveAnalyticsSettings1401Response): json: cannot unmarshal object into Go value of type string
       raw body    : {
      "error_description" : "Invalid access token",
      "error" : "invalid_token"
    }
       Model()     : <nil>
    
  10. Checks. go build ./..., go vet ./..., go test -count=1 -cover ./... (all pass; pkg/gorp 94.1%, internal/commands 77.5%), task fmt, task lint (0 issues.). task e2e was not run: it creates and deletes launches on the server, and this change does not alter what is sent to ReportPortal.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@DzmitryHumianiuk

Copy link
Copy Markdown
Member Author

Note for merging: #33 and #34 conflict at the type level. #34's generated test pins is[*string](v.ErrorCode) (pkg/gorp/openapi_compat_v5_test.go:328), and #33 makes the field *int64. Whichever PR merges second must rerun go run scripts/openapi-compat/v5names.go … -write -test pkg/gorp/openapi_compat_v5_test.go. A follow-up that builds one error type for both clients (status plus server code and message, OAuth 401 bodies) will be stacked on #33.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants