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

Optional attributes at the Event object are not optional #1937

Open
m-mattia-m opened this issue Oct 17, 2024 · 1 comment
Open

Optional attributes at the Event object are not optional #1937

m-mattia-m opened this issue Oct 17, 2024 · 1 comment
Labels

Comments

@m-mattia-m
Copy link

m-mattia-m commented Oct 17, 2024

Describe the bug

When my API receives a webhook request from Stripe via the CLI, I got the error expected required property account to be present and the location attribute is: body. After I created a manual request with the same body and added the attribute account (which is an empty string) I got the same error for the attribute: previous_attributes at the body.data location. After adding this element as well, it worked.

I checked the stripe docs and saw that the account attribute is a nullable string. The previous_attributes is a nullable map. However, the stripe-go-sdk regard these attributes as required attributes and not as optional.

Consider this pull request: #1936

To Reproduce

  1. Create a simple Go API which tries binding the request body to stripe.Event. Consider that if you
  2. Forward Stripe webhook events to your local device (stripe listen --forward-to localhost:8081/webhook)
  3. Create an event (I created a payment via the UI, which had a valid customer)

Check out this repository (m-mattia-m/stripe-go-event-bug-report) for a working example and all the request and response details (cURL, JSON, ...).

Expected behavior

I expect that I can use the stripe.Event object to bind the JSON and it works fine. It should also be able to handle optional attributes like account or previous_attributes.

Code snippets

package main

import (
	"context"
	"fmt"
	"github.com/danielgtaylor/huma/v2"
	"github.com/danielgtaylor/huma/v2/adapters/humagin"
	"github.com/gin-gonic/gin"
	"github.com/stripe/stripe-go/v80"
	"log"
	"net/http"
)

type StripeWebhookEventRequest struct {
	Body stripe.Event `json:"body" bson:"body"`
}

func main() {

	router := gin.New()
	humaConfig := huma.DefaultConfig("Stripe Webhook example", "1.0.0")
	humaConfig.Servers = []*huma.Server{
		{URL: "http://localhost:8080"},
	}
	api := humagin.New(router, humaConfig)

	huma.Register(api, huma.Operation{
		Method:      http.MethodPost,
		OperationID: "receive-stripe-webhook",
		Summary:     "Receive stripe webhook",
		Description: "Receives stripe webhook events.",
		Path:        "/webhook",
	}, GetColumnSchema())

	err := router.Run(":8080")
	if err != nil {
		log.Fatal(err.Error())
	}

}

func GetColumnSchema() func(c context.Context, input *StripeWebhookEventRequest) (*StripeWebhookEventRequest, error) {
	return func(c context.Context, input *StripeWebhookEventRequest) (*StripeWebhookEventRequest, error) {
		fmt.Println("---------- [ Successfully received Stripe event ] ----------")
		fmt.Println(input.Body)

		return nil, nil
	}
}

OS

macOS 15.0.1

Go version

Go 1.21

stripe-go version

v80.2.0

API version

2022-11-15

Additional context

No response

@m-mattia-m m-mattia-m added the bug label Oct 17, 2024
@jar-stripe
Copy link
Contributor

jar-stripe commented Oct 18, 2024

@m-mattia-m Thank you! I've added this to our internal backlog so we can track and prioritize it alongside our other work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants