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

Empty response when expanding an optional unset attribute #46

Closed
ob-stripe opened this issue Feb 20, 2018 · 3 comments · Fixed by #60
Closed

Empty response when expanding an optional unset attribute #46

ob-stripe opened this issue Feb 20, 2018 · 3 comments · Fixed by #60
Assignees

Comments

@ob-stripe
Copy link
Contributor

ob-stripe commented Feb 20, 2018

If you try to expand outcome.rule on a charge object, you get an empty response:

$ curl -vvvv -g "http://localhost:12111/v1/charges/ch_123?expand[]=outcome.rule" -u sk_test_123:
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 12111 (#0)
* Server auth using Basic with user 'sk_test_123'
> GET /v1/charges/ch_123?expand[]=outcome.rule HTTP/1.1
> Host: localhost:12111
> Authorization: Basic c2tfdGVzdF8xMjM6
> User-Agent: curl/7.54.0
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

outcome.rule is a valid expandable attribute (cf. API ref), however it is only set when applicable and is not set in the fixtures file.

The API's behavior in this case is simply to return the charge object, e.g.:

$ curl -g "https://api.stripe.com/v1/charges/ch_...?expand[]=outcome.rule" -u $STRIPE_SECRET_KEY:
{
  "id": "ch_...",
  "object": "charge",
  ...
  "outcome": {
    "network_status": "declined_by_network",
    "reason": "expired_card",
    "risk_level": "normal",
    "seller_message": "The bank returned the decline code `expired_card`.",
    "type": "issuer_declined"
  },
  ...
}
@brandur-stripe
Copy link
Contributor

Oops, yep that's a crash:

$ go install && stripe-mock -port 12112
stripe-mock master
Routing to 115 path(s) and 197 endpoint(s) with 108 validator(s)
Listening on port 12112
Request: GET /v1/charges/ch_123
2018/02/20 12:05:09 http: panic serving [::1]:61521: Responding to /v1/charges/ch_123:
Dereferencing '#/components/schemas/charge':
Using fixture 'charge':
In property 'outcome' of object:
Choosing only branch of anyOf:
Dereferencing '#/components/schemas/charge_outcome':
In property 'rule' of object:
Expanding optional expandable field:
Dereferencing '#/components/schemas/rule':
Cannot find or generate example for: {
  "properties": {
    "action": {
      "type": "string"
    },
    "id": {
      "type": "string"
    },
    "predicate": {
      "type": "string"
    }
  },
  "required": [
    "action",
    "id",
    "predicate"
  ],
  "type": "object",
  "x-expandableFields": []
}
goroutine 8 [running]:
net/http.(*conn).serve.func1(0xc4202bafa0)
        /usr/local/Cellar/go/1.9.1/libexec/src/net/http/server.go:1697 +0xd0
panic(0x134f140, 0xc42012a5d0)
        /usr/local/Cellar/go/1.9.1/libexec/src/runtime/panic.go:491 +0x283
main.(*DataGenerator).generateInternal(0xc4209e5b10, 0xc420180960, 0xc4202ec604, 0x12, 0xc42055b7a0, 0x0, 0xc420096f20, 0x14a, 0x0, 0xc420624280, ...)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/generator.go:121 +0x192c
main.(*DataGenerator).generateInternal(0xc4209e5b10, 0xc4201805a0, 0xc4202ec604, 0x12, 0xc42055b7a0, 0x0, 0xc42000ad00, 0xfa, 0x134f140, 0xc42085e990, ...)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/generator.go:72 +0xfba
main.(*DataGenerator).generateInternal(0xc4209e5b10, 0xc420167360, 0xc4202ec604, 0x12, 0xc42055b780, 0xc42055bdf0, 0xc4200e82a0, 0xdc, 0x0, 0xc42085e940, ...)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/generator.go:187 +0x59c
main.(*DataGenerator).generateInternal(0xc4209e5b10, 0xc420167220, 0xc4202ec604, 0x12, 0xc42055b780, 0xc42055bdf0, 0xc42043dd40, 0x88, 0x134f140, 0xc42085e960, ...)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/generator.go:93 +0xe5a
main.(*DataGenerator).generateInternal(0xc4209e5b10, 0xc4202981e0, 0xc4202ec604, 0x12, 0xc42055b760, 0xc42055b7e0, 0xc420022af0, 0x67, 0x1, 0xc42021e2a0, ...)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/generator.go:187 +0x59c
main.(*DataGenerator).Generate(0xc4203a9b10, 0xc4202981e0, 0xc4202ec604, 0x12, 0xc42055b760, 0x1, 0x0, 0x0, 0x160ce01)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/generator.go:23 +0x120
main.(*StubServer).HandleRequest(0xc42064ef40, 0x159d240, 0xc4206c6000, 0xc42079e300)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/server.go:203 +0x652
main.(*StubServer).HandleRequest-fm(0x159d240, 0xc4206c6000, 0xc42079e300)
        /Users/brandur/Documents/go/src/github.com/stripe/stripe-mock/main.go:75 +0x48
net/http.HandlerFunc.ServeHTTP(0xc42055b640, 0x159d240, 0xc4206c6000, 0xc42079e300)
        /usr/local/Cellar/go/1.9.1/libexec/src/net/http/server.go:1918 +0x44
net/http.(*ServeMux).ServeHTTP(0x15ec5e0, 0x159d240, 0xc4206c6000, 0xc42079e300)
        /usr/local/Cellar/go/1.9.1/libexec/src/net/http/server.go:2254 +0x130
net/http.serverHandler.ServeHTTP(0xc4202efad0, 0x159d240, 0xc4206c6000, 0xc42079e300)
        /usr/local/Cellar/go/1.9.1/libexec/src/net/http/server.go:2619 +0xb4
net/http.(*conn).serve(0xc4202bafa0, 0x159d8c0, 0xc420662c80)
        /usr/local/Cellar/go/1.9.1/libexec/src/net/http/server.go:1801 +0x71d
created by net/http.(*Server).Serve
        /usr/local/Cellar/go/1.9.1/libexec/src/net/http/server.go:2720 +0x288

Shouldn't be too hard to shore this up. I'll take a look.

@brandur-stripe brandur-stripe self-assigned this Feb 20, 2018
@brandur-stripe
Copy link
Contributor

Just as a quick update: I looked into this one, and the reason this fails where other places don't is that right now we'll attempt to use fixture that's usually pre-specified in fixtures3 most of the time and generate a few special types of objects (like lists), but right now we fail with the message above if we can't do one of those.

Outcome and rule are a special case because they're API resources that we don't have a top-level resource for, and so the generator balks when it gets to them.

Solutions might include:

  1. Try to include even these types of nested resources in fixtures3.
  2. Allow stripe-mock to generate dummy data in this sort of case where just introspect each field and try to generate the minimum viable object (i.e., empty strings, zeroed integers and decimals, etc.).

The better approach is probably the latter so — it'd shore things up for now, and still allow us to generate higher fidelity subresource fixtures for fixtures3 that will supersede the dummy data when it's available.

brandur-stripe pushed a commit that referenced this issue Apr 14, 2018
Here we codify a path to generate a synthetic schema for objects that
couldn't be loaded from fixtures. This is useful in a couple situations:

1. Where we're trying to expand on a deep object that never lives at the
   top level of the API (see #46).
2. Where we have a schema that doesn't have have fixtures generated for
   it yet (e.g., an internal or prerelease one).

Fixes #46.
@brandur-stripe
Copy link
Contributor

I ended up going with option (2) above.

Confirmed that #48 fixes this problem:

$ curl -vvvv -g "http://localhost:12112/v1/charges/ch_123?expand[]=outcome.rule" -u sk_test_123:
* Couldn't find host localhost in the .netrc file; using defaults
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 12112 (#0)
* Server auth using Basic with user 'sk_test_123'
> GET /v1/charges/ch_123?expand[]=outcome.rule HTTP/1.1
> Host: localhost:12112
> Authorization: Basic c2tfdGVzdF8xMjM6
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Request-Id: req_123
< Stripe-Mock-Version: master
< Date: Sat, 14 Apr 2018 00:12:13 GMT
< Content-Length: 966
< Content-Type: text/plain; charset=utf-8
<
{
  "amount": 100,
  "amount_refunded": 0,
  "balance_transaction": "txn_1CDJmuCLWM6HLS3K1JZYeh6X",
  "captured": false,
  "created": 1234567890,
  "currency": "usd",
  "description": "My First Test Charge (created for API docs)",
  "fraud_details": {},
  "id": "ch_123",
  "livemode": false,
  "metadata": {},
  "object": "charge",
  "outcome": {
    "rule": {
      "action": "",
      "id": "",
      "predicate": ""
    },
    "type": ""
  },
  "paid": true,
  "refunded": false,
  "refunds": {
    "data": [
      {
        "amount": 100,
        "charge": "ch_123",
        "created": 1234567890,
        "currency": "usd",
        "id": "re_1CDJmuCLWM6HLS3KE6PhPJEl",
        "metadata": {},
        "object": "refund",
        "status": "succeeded"
      }
    ],
    "has_more": false,
    "object": "list",
    "url": "/v1/charges/ch_123/refunds"
  },
  "source": {
    "id": "acct_1CDJjyCLWM6HLS3K",
    "object": "account"
  },
  "status": "succeeded"
}
* Connection #0 to host localhost left intact

brandur-stripe pushed a commit that referenced this issue Apr 14, 2018
Here we codify a path to generate a synthetic schema for objects that
couldn't be loaded from fixtures. This is useful in a couple situations:

1. Where we're trying to expand on a deep object that never lives at the
   top level of the API (see #46).
2. Where we have a schema that doesn't have have fixtures generated for
   it yet (e.g., an internal or prerelease one).

Fixes #46.
brandur-stripe pushed a commit that referenced this issue Apr 16, 2018
Here we codify a path to generate a synthetic schema for objects that
couldn't be loaded from fixtures. This is useful in a couple situations:

1. Where we're trying to expand on a deep object that never lives at the
   top level of the API (see #46).
2. Where we have a schema that doesn't have have fixtures generated for
   it yet (e.g., an internal or prerelease one).

Fixes #46.
brandur-stripe pushed a commit that referenced this issue Apr 16, 2018
Here we codify a path to generate a synthetic schema for objects that
couldn't be loaded from fixtures. This is useful in a couple situations:

1. Where we're trying to expand on a deep object that never lives at the
   top level of the API (see #46).
2. Where we have a schema that doesn't have have fixtures generated for
   it yet (e.g., an internal or prerelease one).

Fixes #46.
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 a pull request may close this issue.

2 participants