fix(vm): align VM error schema and WriteProblem with RFC 9457 - #37
fix(vm): align VM error schema and WriteProblem with RFC 9457#37gciavarrini wants to merge 1 commit into
Conversation
Port FLPATH-4751 from kubevirt-service-provider. VM SP is embedded here so OpenAPI validation wiring stays on the agent apiserver. Assisted-By: Claude (Anthropic) Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>
PR Summary by QodoAlign VM error contract with RFC 9457 problem details
AI Description
Diagram
High-Level Assessment
Files changed (7)
|
Code Review by Qodo
1. Problem failures use the wrong logger
|
| if _, err := buf.WriteTo(w); err != nil { | ||
| slog.Default().Error("failed to write problem+json", "error", err) |
There was a problem hiding this comment.
1. Problem failures use the wrong logger 📘 Rule violation ⌂ Architecture
WriteProblem calls slog.Default() directly instead of receiving the application's configured logger as a dependency. When writing a response fails, its diagnostic bypasses the injected logging configuration and any handlers or attributes attached to it.
Agent Prompt
## Issue description
`WriteProblem` accesses the global default logger instead of using an injected application logger.
## Issue Context
Both error paths must report through the configured logger so application handlers and attributes are preserved.
## Fix Focus Areas
- internal/openshift/kubevirtvm/kubevirt/errors.go[58-72]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| rec := httptest.NewRecorder() | ||
| kubevirt.WriteProblem(rec, http.StatusBadRequest, "Validation Error", "value is required but missing") |
There was a problem hiding this comment.
2. Validation routing remains untested 📘 Rule violation ▣ Testability
The new WriteProblem test calls the helper directly with an httptest.ResponseRecorder, while no production request-validation path invokes the function. OpenAPI validation and generated decoding failures still flow through httperror.WriteInvalidArgument, and VM operations use the embedded service-provider route rather than the generated /vms server, so the test can pass while real invalid requests continue through the existing response mechanism.
Agent Prompt
## Issue description
Replace or supplement the direct `WriteProblem` unit test with integration coverage that submits invalid VM requests through the real OpenAPI validation middleware. Wire the intended production validation path to the structured problem-details writer, or remove `WriteProblem` if the existing shared `httperror` handling is authoritative.
## Issue Context
The current test invokes `WriteProblem` directly with an `httptest.ResponseRecorder`, so it verifies only formatting and can pass without changing responses to real invalid requests. The agent API server currently routes OpenAPI validation and generated request-decoding failures through `httperror.WriteInvalidArgument`, and VM operations are exposed through the embedded service-provider routing layer rather than the generated KubeVirt `/vms` handler. Integration coverage should assert the resulting status, `application/problem+json` content type, and RFC 9457 body through the actual production wiring.
## Fix Focus Areas
- internal/openshift/kubevirtvm/kubevirt/errors_test.go[56-70]
- internal/openshift/kubevirtvm/kubevirt/errors.go[58-72]
- internal/apiserver/server.go[60-75]
- cmd/environment-agent/main.go[280-290]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
gabriel-farache
left a comment
There was a problem hiding this comment.
I think the "high" level comment from Qodo makes sense, otherwise LGTM
Summary
OpenAPI request validation returned
text/plainviahttp.Error.POST
/vms400s now useapplication/problem+jsonwith the sametype/title/status/detailbody as handler errors.The Error schema comment now says problem details (RFC 9457), not RFC 7807.
Fixes
FLPATH-4751
(Replaces dcm-project/kubevirt-service-provider#36)
Summary by Sourcery
Return OpenAPI validation errors using the same structured problem details format as other API errors.
Bug Fixes:
application/problem+jsoncontent type instead of plain text.Enhancements:
Tests: