jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 57 forks source link

OpenAPIV3 - minimum/maximum being read incorrectly as numbers #266

Closed rabbiveesh closed 2 years ago

rabbiveesh commented 2 years ago

Steps to reproduce the behavior

I was trying to generate an API Client using OpenAPI::Client, which use JSON::Validator to read the schema supplied. I tried loading amazon's definitions for their Advertising - Sponsored Brands API (available here). Example perl code:

use JSON::Validator;

my $jv = JSON::Validator->new->schema(
  'https://d3a0d0y2hgofx6.cloudfront.net/openapi/en-us/sponsored-brands/3-0/openapi.yaml');
use DDP; p $jv->schema->errors;

This gives a list of 43 different errors, most of which are because the bidMultiplier in many different operations have a minimum and a maximum, which are getting read as strings instead of numbers.

Expected behavior

The schema should validate. Of course, I'm no expert on OpenAPIv3, but I assume amazon is.

Actual behavior

As mentioned above, it simply explodes with errors. Most seem to be b/c of the number vs string issue, though there may be more problems.

rabbiveesh commented 2 years ago

Looking closer at the errors, it seems like the paths are gettting messed up with various refs. first error is

[0]  /components/schemas/SBCampaign/$ref: /oneOf/1 Missing property. (JSON::Validator::Error)

I have no idea what that path is. If you look at the SBCampaign, it doesn't have any $refs directly under it. I also did a scan for every oneOf in the document, and all of them have the minimum 2 properties.

jhthorsen commented 2 years ago

I'm surprised that the numbers wasn't indeed read as numbers, but either way: I fixed it by coercing strings and numbers in the latest version.

Will be available on CPAN soon.