openapi3_parser icon indicating copy to clipboard operation
openapi3_parser copied to clipboard

Convert the Openapi3Parser.load() output to hash or JSON

Open DanielRoig opened this issue 1 year ago • 7 comments

Hey! Congrats for you gem, it is really amazing! I was wondering if there is any way to convert the Openapi3Parser.load() output to hash or JSON. In my case I try something like this:

yaml_content = YAML.load_file(swagger.yaml)
schema = Openapi3Parser.load(yaml_content)
schema.to_h
{"openapi"=>"3.0.2", "info"=>Openapi3Parser::Node::Info(#/info), "externalDocs"=>Openapi3Parser::Node::ExternalDocumentation(#/externalDocs), "security"=>Openapi3Parser::Node::Array(#/security), "tags"=>Openapi3Parser::Node::Array(#/tags), "x-tagGroups"=>[{"name"=>"API V2 Resources", "tags"=>["prebuilt_collections-v2", "stocks-v2"]}, {"name"=>"API V1 Resources", "tags"=>["auth", "contract", "current_user", "feature_candidates", "feature_flags", "invitations", "investment_documents", "me", "nicknames", "prebuilt_collections", "payments", "payment_methods", "recommendation_advisories", "risk_profiles", "stock_positions", "subscription_plans", "surveys", "trading_sessions"]}], "paths"=>Openapi3Parser::Node::Paths(#/paths), "components"=>Openapi3Parser::Node::Components(#/components), "servers"=>Openapi3Parser::Node::Array(#/servers)}

But the nested Openapi3Parser objects still being objects. Are there any way to do this recursively (including the $ref resolved)? I've tried to do by myself but is really low performing. Maybe there is a method that Im missing.

Thank you!

DanielRoig avatar Dec 13 '24 15:12 DanielRoig

Glad you've got value out of the gem 👍

So I think the method you probably want is resolved_input_at which will return you the data with the references resolved. So for your example above you could do:

Openapi3Parser.load_file("swagger.yaml").resolved_input_at("/")

and you'll get a big hash back (with lots of nils) of the whole OpenAPI document with references resolved.

kevindew avatar Dec 13 '24 19:12 kevindew

Awesome ✨✨ It works perfect, thank you!

DanielRoig avatar Dec 16 '24 20:12 DanielRoig

Is there any way to not to have all of that nils? Im having a lot of problems when I try to use JSON::Validator.validate!() in my specs

DanielRoig avatar Mar 18 '25 10:03 DanielRoig

Ah what issues does that produce? Although this wasn't designed for that use case I'd have thought that the nil values were negligible. I suppose there could be problems with JSON schema dialect or individual fields used by OpenAPI.

To get rid of them you should be able to run compact on the hash for top level and to do it recursively for a nested structure.

kevindew avatar Mar 18 '25 19:03 kevindew

Ah what issues does that produce? Although this wasn't designed for that use case I'd have thought that the nil values were negligible. I suppose there could be problems with JSON schema dialect or individual fields used by OpenAPI.

To get rid of them you should be able to run compact on the hash for top level and to do it recursively for a nested structure.

Well, when I compare with JSON::Validator.validate!(extracted_schema, JSON.parse(response.body))

and extracted_schema generated with the parser is

...
value:
  type: integer
  format: int64
  minimum: 0
  maximum: nil
  exclusiveMinimum: nil
  exclusiveMaximum: nil
  multipleOf: nil
  nullable: nil
  description: nil
...

and response.body is

...
value:
     type: integer
     format: int64
     minimum: 0
...

it fails because the nil attributes in extracted_schema are not in the response

DanielRoig avatar Mar 28 '25 12:03 DanielRoig

Thanks for explaining. I've been able to replicate. That is an annoying quirk of JSON schema that the nil values have meaning

I think for now compacting the data would be the way to do it. I'll keep this open to see about considering creating a method on a schema object that can get a schema structure (or a PR is welcome).

I expect what we'd want is a as_schema method on Openapi3Parser::Node::Schema and have that know which fields should be compacted and which fields can be explicitly set to null with meaning.

kevindew avatar Apr 07 '25 16:04 kevindew

Cool! Thank you for your response!

DanielRoig avatar Apr 07 '25 18:04 DanielRoig