npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openapi-schema-diff

v0.0.1

Published

Finds changes between two OpenAPI schemas

Downloads

166,767

Readme

openapi-schema-diff

openapi-schema-diff is a javascript library that compares two OpenAPI schemas and finds breaking changes.

Installation

npm install openapi-schema-diff

Usage

const compareOpenApiSchemas = require('openapi-schema-diff')

const sourceSchema = {
  openapi: '3.0.0',
  info: {
    title: 'My API',
    version: '1.0.0'
  },
  paths: {
    '/pets': {
      get: {
        summary: 'Returns all pets',
        responses: {
          200: {
            description: 'A list of pets.',
            content: {
              'application/json': {
                schema: {
                  type: 'array',
                  items: {
                    type: 'object',
                    properties: {
                      name: {
                        type: 'string'
                      },
                      age: {
                        type: 'integer'
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

const targetSchema = {
  openapi: '3.0.0',
  info: {
    title: 'My API',
    version: '1.0.0'
  },
  paths: {
    '/pets': {
      get: {
        summary: 'Returns all pets',
        responses: {
          200: {
            description: 'A list of pets.',
            content: {
              'application/json': {
                schema: {
                  type: 'array',
                  items: {
                    type: 'object',
                    properties: {
                      name: {
                        type: 'string'
                      },
                      age: {
                        type: 'integer'
                      },
                      breed: {
                        type: 'string'
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

const differences = compareOpenApiSchemas(sourceSchema, targetSchema)
assert.deepEqual(differences, {
  isEqual: false,
  sameRoutes: [],
  addedRoutes: [],
  deletedRoutes: [],
  changedRoutes: [
    {
      method: 'get',
      path: '/pets',
      sourceSchema: sourceSchema.paths['/pets'].get,
      targetSchema: targetSchema.paths['/pets'].get,
      changes: [
        {
          type: 'responseBody',
          statusCode: '200',
          mediaType: 'application/json',
          action: 'changed',
          sourceSchema: sourceSchema.paths['/pets'].get.responses['200'].content['application/json'],
          targetSchema: targetSchema.paths['/pets'].get.responses['200'].content['application/json'],
          changes: [
            {
              keyword: 'schema',
              changes: [
                {
                  jsonPath: '#/items/properties/breed',
                  source: undefined,
                  target: {
                    type: 'string'
                  }
                }
              ],
              comment: 'response header schema has been changed'
            }
          ],
          comment: 'response body for "200" status code and "application/json" media type has been changed in GET "/pets" route'
        }
      ]
    }
  ]
})

API

compare(sourceSchema, targetSchema)

Compares two OpenAPI schemas and returns and finds breaking changes. Source and target schemas must have the same OpenAPI major version.

  • sourceSchema <object> - source OpenAPI schema.
  • targetSchema <object> - target OpenAPI schema.
  • Returns - an object with schema differences.
    • isEqual <boolean> - true if target schema does not have breaking changes, false otherwise.
    • sameRoutes <array> - an array of routes that are present in both schemas and do not have breaking changes. See same route.
    • addedRoutes <array> - an array of routes that are present in target schema but not in source schema. See added route.
    • deletedRoutes <array> - an array of routes that are present in source schema but not in target schema. See deleted route.
    • changedRoutes <array> - an array of routes that are present in both schemas and have breaking changes. See changed route.
Same route object
  • method <string> - HTTP method name of the route.
  • path <string> - path of the route.
  • sourceSchema <object> - source OpenAPI schema of the route.
  • targetSchema <object> - target OpenAPI schema of the route.
Added route object
  • method <string> - HTTP method name of the route.
  • path <string> - path of the route.
  • targetSchema <object> - target OpenAPI schema of the route.
Deleted route object
  • method <string> - HTTP method name of the route.
  • path <string> - path of the route.
  • sourceSchema <object> - source OpenAPI schema of the route.
Changed route object
  • method <string> - HTTP method name of the route.
  • path <string> - path of the route.
  • sourceSchema <object> - source OpenAPI schema of the route.
  • targetSchema <object> - target OpenAPI schema of the route.
  • changes <array> - a list of route components (header, querystring, body, ...) that have breaking changes. See change object
Route change object
  • type <string> - type of the component. One of parameter, requestBody, responseBody, responseHeader.
  • action <string> - action that was performed on the component. One of added, deleted, changed.
  • sourceSchema <object> - source OpenAPI schema of the component.
  • targetSchema <object> - target OpenAPI schema of the component.
  • comment <string> - a comment describing the change.
  • changes <array> - a list of changes in a component json schema. Exist only if action equals to changed. Each schema keyword has it's own change object. See list of change objects.

Each of the route components has it's own unique properties that identify it. For more details look at the component change object: parameter, request body, response body, response header.

Parameter change object
  • type <string> - type of the component. Equals to parameter.
  • name <string> - name of the parameter.
  • in <string> - location of the parameter. One of query, header, path, cookie.
  • schemaChanges - a list of changes in a component json schema. See schema change object.
  • comment <string> - a comment describing the change.
Request body change object
  • type <string> - type of the component. Equals to requestBody.
  • mediaType <string> - media type of the component.
  • schemaChanges - a list of changes in a component json schema. See schema change object.
  • comment <string> - a comment describing the change.
Response body change object
  • type <string> - type of the component. Equals to responseBody.
  • statusCode <string> - HTTP status code of the component.
  • mediaType <string> - media type of the component.
  • schemaChanges - a list of changes in a component json schema. See schema change object.
  • comment <string> - a comment describing the change.
Response header change object
  • type <string> - type of the component. Equals to responseHeader.
  • header <string> - name of the header.
  • statusCode <string> - HTTP status code of the component.
  • schemaChanges - a list of changes in a component json schema. See schema change object.
  • comment <string> - a comment describing the change.

List schema keywords and their change objects

schema keyword change object
  • keyword <string> - keyword name. Equals to schema.
  • comment <string> - a comment describing the change.
  • changes <array> - a list of changes in a component json schema.
    • jsonPath <string> - JSON path of the changed schema.
    • source <object> - source subschema placed at the jsonPath.
    • target <object> - target subschema placed at the jsonPath.
required keyword change object
  • keyword <string> - keyword name. Equals to required.
  • source <boolean> - source value of the keyword.
  • target <boolean> - target value of the keyword.
  • comment <string> - a comment describing the change.

License

MIT