Git Product home page Git Product logo

open_api_parser's Introduction

OpenApiParser

Build Status

A gem for parsing Open API specifications.

Usage

First, resolve a specification given an absolute file path.

specification = OpenApiParser::Specification.resolve("/path/to/my/specification.yaml")

When you resolve your file, all references will be expanded and inlined. The fully resolved specification will be validated against the Open API V2 meta schema. If you'd like to prevent meta schema validation, you can provide an option to do so.

specification = OpenApiParser::Specification.resolve("/path/to/my/specification.yaml", validate_meta_schema: false)

If you'd rather instantiate a specification with a fully resolved ruby hash, you can do so by directly instantiating an OpenApiParser::Specification::Root.

OpenApiParser::Specification::Root.new({...})

Reference resolution

OpenApiParser::Specification.resolve will fully resolve the provided Open API specification by inlining all JSON References. Two types of references are allowed:

Pointers are resolved from the root of the document in which they appear. File references are resolved relative to the file in which they appear. Here's an example of each:

definitions:
  person:
    type: object
    properties:
      name:
        type: string
      age:
        type: integer

info:
  person:
    $ref: "/definitions/person"
  other:
    $ref: "file:another/file.yaml"

For more information, see the specs.

Endpoints

With a resolved schema, you can access the information for an endpoint given a path and an HTTP verb.

endpoint = specification.endpoint("/animals", "post")

With an endpoint, you can get access to JSON schema representations of the body, headers, path, query params and response body and headers defined in your Open API specification. You can use these to validate input using any existing JSON Schema library. We recommend JsonSchema.

endpoint.body_schema
# => {...}

endpoint.header_schema
# => {...}

endpoint.path_schema
# => {...}

endpoint.query_schema
# => {...}

endpoint.response_body_schema(201)
# => {...}

endpoint.response_header_schema(201)
# => {...}

You can also use the endpoint to transform user input into json that can be validated against the schemas generated in the previous examples.

endpoint.header_json(request_headers_as_hash)
# => {...}

endpoint.path_json("/animals/123?query=param")
# => {...}

endpoint.query_json(request_query_params_as_hash)
# => {...}

For more information, see the specs.

Installation

Add this line to your application's Gemfile:

gem 'open_api_parser'

And then execute:

$ bundle

Or install it yourself as:

$ gem install open_api_parser

open_api_parser's People

Contributors

drewolson avatar ento avatar jfeltesse-mdsol avatar katsuya94 avatar nicolasleger avatar ssteeg-mdsol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

open_api_parser's Issues

self referencing definitions trigger an infinite loop

If a definition happens to have a property with a reference to itself then doing something like

OpenApiParser::Document.new(nil, my_openapi_spec_hash, nil).resolve

will crash with stack level too deep.

Any ideas on how to handle this?
Looking around it seems the OpenAPI spec is OK with this because the underlying JSON Schema spec allows for such things.

Consider json-schema gem over json_schema

I'm going to make some assertions for discussion:

While I like @brandur 's json_schema gem, and even wrote an ActiveModelSerializers integration against it, I've proposing that the json-schema should be preferred for a couple of (unsubstantiated) reasons:

  • It's complete; json_schema was never completed esp re: reference expanding and may never be
  • It's more actively maintained
  • It's up to date
  • It has better errors
  • It's easy to use
  • json_schema, like prmd and associated earlier hypermedia heroku gems allow and sometimes expect invalid schema ids

See apivore for a nice json-schema implementation

ref:

fwiw, also see https://github.com/stoplightio/api-spec-converter

Open API 3

Is there any plans to add support for Open API 3.x ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.