-
-
Notifications
You must be signed in to change notification settings - Fork 67
fix: use json.Marshal in CreateResult instead of string concatenation #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+96
−15
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,18 @@ type TestResultSummary struct { | |
| InProgress bool `json:"inProgress"` | ||
| } | ||
|
|
||
| // createTestResultRequest is the JSON payload sent to the Microcks tests endpoint. | ||
| type createTestResultRequest struct { | ||
| ServiceID string `json:"serviceId"` | ||
| TestEndpoint string `json:"testEndpoint"` | ||
| RunnerType string `json:"runnerType"` | ||
| Timeout int64 `json:"timeout"` | ||
| SecretName string `json:"secretName,omitempty"` | ||
| FilteredOperations []string `json:"filteredOperations,omitempty"` | ||
| OperationsHeaders map[string][]HeaderDTO `json:"operationsHeaders,omitempty"` | ||
| OAuth2Context *OAuth2ClientContext `json:"oAuth2Context,omitempty"` | ||
| } | ||
|
|
||
| // HeaderDTO represents an operation header passed for Test | ||
| type HeaderDTO struct { | ||
| Name string `json:"name"` | ||
|
|
@@ -323,28 +335,36 @@ func (c *microcksClient) CreateTestResult(serviceID string, testEndpoint string, | |
| rel := &url.URL{Path: "tests"} | ||
| u := c.APIURL.ResolveReference(rel) | ||
|
|
||
| // Prepare an input string as body. | ||
| var input = "{" | ||
| input += ("\"serviceId\": \"" + serviceID + "\", ") | ||
| input += ("\"testEndpoint\": \"" + testEndpoint + "\", ") | ||
| input += ("\"runnerType\": \"" + runnerType + "\", ") | ||
| input += ("\"timeout\": " + strconv.FormatInt(timeout, 10)) | ||
| if len(secretName) > 0 { | ||
| input += (", \"secretName\": \"" + secretName + "\"") | ||
| // Build request payload via a struct so json.Marshal handles escaping. | ||
| payload := createTestResultRequest{ | ||
| ServiceID: serviceID, | ||
| TestEndpoint: testEndpoint, | ||
| RunnerType: runnerType, | ||
| Timeout: timeout, | ||
| SecretName: secretName, | ||
| } | ||
| if len(filteredOperations) > 0 && ensureValidOperationsList(filteredOperations) { | ||
| input += (", \"filteredOperations\": " + filteredOperations) | ||
| var ops []string | ||
| _ = json.Unmarshal([]byte(filteredOperations), &ops) | ||
| payload.FilteredOperations = ops | ||
| } | ||
| if len(operationsHeaders) > 0 && ensureValidOperationsHeaders(operationsHeaders) { | ||
| input += (", \"operationsHeaders\": " + operationsHeaders) | ||
| var headers map[string][]HeaderDTO | ||
| _ = json.Unmarshal([]byte(operationsHeaders), &headers) | ||
| payload.OperationsHeaders = headers | ||
| } | ||
| if len(oAuth2Context) > 0 && ensureValieOAuth2Context(oAuth2Context) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor nit: |
||
| input += (", \"oAuth2Context\": " + oAuth2Context) | ||
| var oCtx OAuth2ClientContext | ||
| _ = json.Unmarshal([]byte(oAuth2Context), &oCtx) | ||
| payload.OAuth2Context = &oCtx | ||
| } | ||
|
|
||
| input += "}" | ||
| body, err := json.Marshal(payload) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to marshal test result request: %w", err) | ||
| } | ||
|
|
||
| req, err := http.NewRequest("POST", u.String(), strings.NewReader(input)) | ||
| req, err := http.NewRequest("POST", u.String(), bytes.NewReader(body)) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
@@ -365,13 +385,13 @@ func (c *microcksClient) CreateTestResult(serviceID string, testEndpoint string, | |
| // Dump response if verbose required. | ||
| config.DumpResponseIfRequired("Microcks for creating test", resp, true) | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| respBody, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| panic(err.Error()) | ||
| } | ||
|
|
||
| var createTestResp map[string]interface{} | ||
| if err := json.Unmarshal(body, &createTestResp); err != nil { | ||
| if err := json.Unmarshal(respBody, &createTestResp); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unmarshal errors are silently discarded. If
ensureValidOperationsListsays "valid" but the value isn't a[]string(e.g. array of objects), the field is dropped instead of sent — a silent behavior regression vs the old code that forwarded the raw string. At minimum fall back to embedding the raw string, or return an error. I don't think we should swallow the unmarshal error.Same concern applies to R353 and R358.