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

Generate synthetic fixtures for nullable types when expansions are requested #446

Merged
merged 3 commits into from
Aug 25, 2023

Conversation

anniel-stripe
Copy link
Contributor

@anniel-stripe anniel-stripe commented Aug 25, 2023

The issue in #445 is caused by a crash when you try to expand a nullable attribute:

$ stripe-mock
stripe-mock 0.159.0
Routing to 306 path(s) and 453 endpoint(s) with 453 validator(s)
Listening for HTTP at address: [::]:12111
Listening for HTTPS at address: [::]:12112
Request: GET /v1/invoices/in_1NhTSDJ7Bu9fw2TI7tY1f5zD
Request data = map[expand:[account_tax_ids]]
2023/08/25 15:27:12 http: panic serving 127.0.0.1:53788: Responding to GET /v1/invoices/in_1NhTSDJ7Bu9fw2TI7tY1f5zD:
Dereferencing '#/components/schemas/invoice':
Using fixture 'invoice':
In property 'account_tax_ids' of object:
We were asked to expand a key, but our example has null for that key.
goroutine 49 [running]:
net/http.(*conn).serve.func1()
	/opt/hostedtoolcache/go/1.19.7/x64/src/net/http/server.go:1850 +0xb0
panic({0x100e9c660, 0x140018e3560})
	/opt/hostedtoolcache/go/1.19.7/x64/src/runtime/panic.go:890 +0x258
github.com/stripe/stripe-mock/server.(*DataGenerator).generateInternal(0x140010398f8, 0x14001acf320)
	/home/runner/work/stripe-mock/stripe-mock/server/generator.go:339 +0xe2c
github.com/stripe/stripe-mock/server.(*DataGenerator).generateInternal(0x140010398f8, 0x14001039670)
	/home/runner/work/stripe-mock/stripe-mock/server/generator.go:399 +0x10dc
github.com/stripe/stripe-mock/server.(*DataGenerator).Generate(0x140010398f8, 0x14001039950)
	/home/runner/work/stripe-mock/stripe-mock/server/generator.go:105 +0x114
github.com/stripe/stripe-mock/server.(*StubServer).HandleRequest(0x140008ea260, {0x100f044a8, 0x1400081ba40}, 0x14001ab0000)
	/home/runner/work/stripe-mock/stripe-mock/server/server.go:353 +0x968
net/http.HandlerFunc.ServeHTTP(0x14001ab9a38?, {0x100f044a8?, 0x1400081ba40?}, 0x100d6f7e8?)
	/opt/hostedtoolcache/go/1.19.7/x64/src/net/http/server.go:2109 +0x38
net/http.(*ServeMux).ServeHTTP(0x140003dc0a4?, {0x100f044a8, 0x1400081ba40}, 0x14001ab0000)
	/opt/hostedtoolcache/go/1.19.7/x64/src/net/http/server.go:2487 +0x140
github.com/stripe/stripe-mock/server.(*DoubleSlashFixHandler).ServeHTTP(0x140018a29c0, {0x100f044a8, 0x1400081ba40}, 0x14001ab0000)
	/home/runner/work/stripe-mock/stripe-mock/server/server.go:47 +0xb0
net/http.serverHandler.ServeHTTP({0x14001a9a1b0?}, {0x100f044a8, 0x1400081ba40}, 0x14001ab0000)
	/opt/hostedtoolcache/go/1.19.7/x64/src/net/http/server.go:2947 +0x2c4
net/http.(*conn).serve(0x14001aaa000, {0x100f049a0, 0x14001a9a0c0})
	/opt/hostedtoolcache/go/1.19.7/x64/src/net/http/server.go:1991 +0x560
created by net/http.(*Server).Serve
	/opt/hostedtoolcache/go/1.19.7/x64/src/net/http/server.go:3102 +0x444

This is because generateSyntheticFixtures defaults to returning nil if the property is nullable. However, this poses an issue when we want to expand that nullable field.

This PR updates generateSyntheticFixtures to check expansions and generate non-nil fixtures for nullable properties if they're being expanded.

@anniel-stripe anniel-stripe requested review from a team and richardm-stripe and removed request for a team August 25, 2023 23:13
server/generator.go Outdated Show resolved Hide resolved
@anniel-stripe anniel-stripe enabled auto-merge (squash) August 25, 2023 23:23
@anniel-stripe anniel-stripe merged commit 1d80bcc into master Aug 25, 2023
@anniel-stripe anniel-stripe deleted the anniel-non-empty-expandable branch August 28, 2023 16:30
anniel-stripe added a commit that referenced this pull request Aug 28, 2023
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.

2 participants