Skip to content
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

fix: Fixes issues w/ shared pointers to structs #378

Merged
merged 2 commits into from
Sep 16, 2024

Conversation

harrisoncramer
Copy link
Owner

@harrisoncramer harrisoncramer commented Sep 16, 2024

I'd introduced a middleware pattern that was validating structs. The middleware was set up in the server with pointers to structs, like this:

m.HandleFunc("/mr/merge", middleware(
    mergeRequestAccepterService{d, gitlabClient},
    withMr(d, gitlabClient),
    withPayloadValidation(methodToPayload{http.MethodPost: &AcceptMergeRequestRequest{}}), // Here...
    withMethodCheck(http.MethodPost),
))

This caused the struct data to be shared across requests! This was causing issues where payload fields were being shared between API calls. We want new payloads for every API call.

This MR adjusts the code to add a new factory function on the methodToPayload map that creates a new payload per request so that data is not shared across them.

The new pattern is like this:

m.HandleFunc("/mr/merge", middleware(
    mergeRequestAccepterService{d, gitlabClient},
    withMr(d, gitlabClient),
    withPayloadValidation(methodToPayload{http.MethodPost: newPayload[AcceptMergeRequestRequest]}),
    withMethodCheck(http.MethodPost),
))

Where newPayload creates a new instance of the type provided and returns a pointer to that value in memory.

@harrisoncramer harrisoncramer force-pushed the 365-error-creating-comment-on-new-code-1 branch from f95e2a3 to 8042f74 Compare September 16, 2024 20:14
@harrisoncramer harrisoncramer marked this pull request as ready for review September 16, 2024 20:19
@harrisoncramer harrisoncramer merged commit 4ff27c2 into develop Sep 16, 2024
6 checks passed
harrisoncramer added a commit that referenced this pull request Sep 16, 2024
* fix: Fixes issues w/ shared pointers to structs (#378)
* feat: Adds even better debugging and linting support (#376)

This is a #PATCH release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant