Git Product home page Git Product logo

prance-translating-parser's Introduction

Translating Parser for Prance

This is a Translating parser for Prance, a library processing and validating OpenAPI specification files. It targets use cases when a specification spans across multiple files that need to be compiled to a single document, but inlining of the references objects is not desired or event possible.

Frameworks like Connexion unfortunately do not support file references in the specification and Prance can be used to alleviate this missing feature. Inlining can result in excessive memory usage and also can make code generators not work properly if they rely on properly reused component schema definitions. If the specification contains a recursive object definition, inlining would result in an infinite loop.

The original motivation is to make Red Hat Insights Host Inventory work with recursive objects defined in a schema that is incorporated to the OpenAPI specification of the application.

Examples

Imagine having an OpenAPI specification file that references an object from an external file. The current Resolving Parser would inline all of those, even if they were pointing to the same object, using a lot of memory. If the target object would recursively reference itself, such inlining wouldn’t be even possible.

openapi.spec.yaml

---
openapi: 3.0.3
info:
  title: Example specification for the translating parser
  version: 0.0.0
paths:
  /hosts:
    get:
      responses:
        default:
          description: >-
            An operation with a referenced schema.
          content:
            application/json:
              schema:
                $ref: 'schemas.yaml#/$defs/Response'

schemas.spec.yaml

---
$defs:
  Response:
    type: object
    properties:
      simple:
        $ref: "#/$defs/Simple"
      nested:
        $ref: "#/$defs/Nested"
  Simple:
    type: object
  Nested:
    type: object
    additionalProperties:
      $ref: "#/$defs/Nested"

Resolving Parser result

---
openapi: 3.0.3
info:
  title: Example specification for the translating parser
  version: 0.0.0
paths:
  /hosts:
    get:
      responses:
        default:
          description: >-
            An operation with a referenced schema.
          content:
            application/json:
              schema:
                type: object
                properties:
                  simple:
                    type: object
                  nested:
                    type: object
                    additionalProperties:
                      <RECURSION!>

All references are removed and replaces with the referenced content. If a single object is referenced multiple times, the whole schema is inlined in place of each reference. In case of the recursive reference, this causes an infinite loop unless a custom Recursion Handler is provided.

Translating Parser result

---
openapi: 3.0.3
info:
  title: Example specification for the translating parser
  version: 0.0.0
paths:
  /hosts:
    get:
      responses:
        default:
          description: >-
            An operation with a referenced schema.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/schemas.spec.yaml_Response"
components:
  schemas:
    schemas.spec.yaml_Response:
      type: object
      properties:
        simple:
          $ref: "#/components/schemas/schemas.spec.yaml_Simple"
        nested:
          $ref: "#/components/schemas/schemas.spec.yaml_Nested"
      schemas.spec.yaml_Simple:
        type: object
      schemas.spec.yaml_Nested:
        type: object
        additionalProperties:
          $ref: "#/components/schemas/schemas.spec.yaml_Nested"

Here, the schemas from the external files are carried over to the original specification document. References remain references, just pointing to the Schemas collection. This applies to the recursive object too that is still recursive, only referencing itself in its new location.

Development environment

  1. Get Pipenv.
$ pip install pipenv
  1. Install requirements.
$ pipenv sync
  1. ???
  2. Profit.

Usage

The core here is the TranslatingParser that can be used instead of Prance’s ResolvingParser to process the specification.

from translating_parser.parser import TranslatingParser
parser = TranslatingParser("openapi.spec.yaml")
parser.parse()
print(parser.specification)

There is a companion CLI script main.py that can be used to test and debug the parser.

usage: main.py [-h] [--parser PARSER] [--output-format OUTPUT_FORMAT]
               [--verbose]
               [file]

positional arguments:
  file                  OpenAPI specification file. Default:
                        specs/openapi.spec.yaml

optional arguments:
  -h, --help            show this help message and exit
  --parser PARSER, -p PARSER
                        Parser class. Default:
                        translating_parser.parser.TranslatingParser
  --output-format OUTPUT_FORMAT, -f OUTPUT_FORMAT
                        Output format: json or yaml. Default: json
  --verbose, -v         Verbose output

To use the original Resolving Parser, use -p prance.ResolvingParser.

Testing

$ PYTHONPATH=. pipenv run pytest

License

MIT, to be compatible with Prance.

prance-translating-parser's People

Contributors

glutexo avatar

Stargazers

Rafael Carício avatar

Watchers

James Cloos avatar  avatar  avatar

prance-translating-parser's Issues

README is missing clear steps to execute main.py

README should provide clear steps from cloning to executing main.py, e.g.

  1. clone prance-translating-parser
  2. Go into the repo and run pip install or pip sync
  3. Run pipenv shell
  4. export PYTHONPATH=. May want to look at Host-Inventory, which does not require explicitly setting PYTHONPATH
  5. python main.py
  6. pytest

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.