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

Support primitives in an anyOf from the parameters coercer #67

Merged
merged 1 commit into from
Apr 26, 2018

Conversation

brandur-stripe
Copy link
Contributor

@brandur-stripe brandur-stripe commented Apr 26, 2018

I was running stripe-ruby's test suite against a recent version of the
OpenAPI, and noticed that unfortunately, recent updates have broken it.
In particular, the new tiers feature under plan creation now has a
primitive type nested in an anyOf, which is something that we've never
had to handle before:

up_to:
  anyOf:
  - enum:
    - inf
    type: string
  - type: integer

stripe-ruby tries to run this against it:

plan = Stripe::Plan.create(
  ...
  tiers: [{ up_to: 100, amount: 1000 }, { up_to: "inf", amount: 2000 }]
)

What's supposed to happen is that "100" is coerced to an integer, but it's not
because the integer possibility is nested in an anyOf as seen above.

This patch addresses the problem by allowing the coercer to recurse into an
anyOf to try coercion. It's able to theoretically handle any number of
anyOf possibilities, and will dutifully try coercing each one until it finds
a coercable type (or it doesn't, which is okay).

I've run stripe-ruby's test suite against a version of stripe-mock with
this patch in it to verify that it works.

r? @tmaxwell-stripe (This one should be a little simpler to review than the other PR.)
cc @stripe/api-libraries

I was running `stripe-ruby`'s test suite against a recent version of the
OpenAPI, and noticed that unfortunately, recent updates have broken it.
In particular, the new tiers feature under plan creation now has a
primitive type nested in an `anyOf`, which is something that we've never
had to handle before:

``` yaml
up_to:
  anyOf:
  - enum:
    - inf
    type: string
  - type: integer
```

`stripe-ruby` tries to run this against it:

``` ruby
plan = Stripe::Plan.create(
  ...
  tiers: [{ up_to: 100, amount: 1000 }, { up_to: "inf", amount: 2000 }]
)
```

What's supposed to happen is that "100" is coerced to an integer, but it's not
because the integer possibility is nested in an `anyOf` as seen above.

This patch addresses the problem by allowing the coercer to recurse into an
`anyOf` to try coercion. It's able to theoretically handle any number of
`anyOf` possibilities, and will dutifully try coercing each one until it finds
a coercable type (or it doesn't, which is okay).

I've run `stripe-ruby`'s test suite against a version of `stripe-mock` with
this patch in it to verify that it works.
@tmaxwell-stripe
Copy link
Contributor

LGTM

@brandur-stripe
Copy link
Contributor Author

Thanks!

@brandur-stripe brandur-stripe merged commit e16881a into master Apr 26, 2018
@brandur-stripe brandur-stripe deleted the brandur-param-coercer-any-of branch April 26, 2018 20:44
@alexander-stripe
Copy link

Ah nice!

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.

4 participants