Giter Site home page Giter Site logo

swagger-diff's Introduction

swagger-diff

NPM Version Build Status

This package provides utils and CLI to compute the diff between two swagger API specifications. Output diff can be configured according to version change.

Purpose

  • Identify breaking and smooth changes.
  • Ensure API versioning consistency.
  • Compute API changelogs.
  • Prevent unexpected API changes.

Swagger Compatilibity

Supports only swagger spec 2.0.

Installation

Install using npm:

npm install swagger-diff

Usage

CLI

The binary allows you to use swagger-diff in CLI.

$ swagger-diff <old> <new>

It prints the diff between old and new swagger files according to configuration and returns false if any diff "error". It can also write the diff result in a JSON file. Use -h for option defails.

Note: old and new parameters can either be the file path or the URL of the swagger file.

Example of CLI output CLI output example

Node

var SwaggerDiff = require('swagger-diff');

SwaggerDiff(oldSpec, newSpec, config).then(function (diff) {
  // Handle result
});

Note: on nodeJS, oldSpec and newSpec can either be a file path, a URL or a plain object. config can be a file path or a plain object.

Note: Please refer to How it works section for details about output.

Browsers

Dist folder contains an UMD bundle allowing you to either reference swagger-diff.min.js in your HTML or import module using Require.js.

Reference swagger-diff.min.js in your HTML and use the global variable SwaggerDiff.

<script src="node_modules/swagger-diff/dist/swagger-diff.min.js"></script>
<script>
  SwaggerDiff(oldSpec, newSpec, config).then(function (diff) {
    // Handle result
  });
</script>

Hosted versions of swagger-diff releases are available on JSDeliver CDN.

Note: in browser, oldSpec and newSpec can only be a URL or a plain object. config can only be a plain object.

Note: Please refer to How it works section for details about output.

Diffs

Swagger-Diff defines rules that performs ONE type of diff checking. These rules are separated in 2 groups:

  • breaking change
  • smooth change

Breaking changes

Examples:

  • Delete path
  • Rename path operationId
  • Delete/Rename parametters
  • Add a constraint on a parametter (like isRequired)
  • Modify a response item

Smooth changes

Examples:

  • Add a path
  • Add a param
  • Add response item
  • Add/Update descriptions

Configuration

In the configuration file (default: .swagger-diff), you can customize the level of log you want for type of changes.

{
  "changes": {
    "breaks": 3,
    "smooths": 2
  }
}

It's also possible to define different level of logs according to version change.

{
  "changes": {
    "breaks": {
      "major": 2,
      "minor": 3,
      "patch": 3,
      "unchanged": 3
    },
    "smooths": {
      "major": 0,
      "minor": 1,
      "patch": 2,
      "unchanged": 3
    }
  }
}

Levels of log

3-error
2-warning
1-info
0-ignore

Configure specific rules

You can also configure specific level of logs for some rules.

{
  "rules": {
    "delete-path": 0,
    "add-path": {
      "major": 2,
      "minor": 3,
      "patch": 3,
      "unchanged": 3
    }
  }
}

How it works

To compute the diff, it exectutes a workflow composed of 4 main steps.

How it works

Preparation

Dereference

Resolve JSON references and dereference URIs.

Inline global definitions

Swagger spec 2.0 allows to specify global definitions for parameters, security, schemes, consumes and produces that can then be overriden when needed. It inlines these definitions in every paths objects.

Index definitions

parameters are indexed by their name in order to allow raw-diff to compare parameters nicely.

Raw diff

deep-diff lib is used to compute deep raw diff.

Rules application

Exectute each rule on each raw diff to output breakings and smooth changes.

Final diff

Post process diffs to output errors, warnings, infos according to configuration and version change.

Note: unmatchDiffs are the raw diffs that didn't much any rules. They can include breaking changes not implemented yet.

swagger-diff's People

Contributors

astampoulis avatar dasevilla avatar zallek 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  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

swagger-diff's Issues

Adding open source license

Would it be possible to open source license this project ?

We'd love to use it in our internal projects !

unmatched diff section

I'm very interested in using this tool to help identify differences between API versions. I noted we have a large number of lines reported in the unmatchDiffs section of kind equals to E. I'm not 100% clear why we see them from the swagger definitions. The help indicate

Note: unmatchDiffs are the raw diffs that didn't much any rules. They can include breakings changes not implemented yet.

Looking at our swagger definitions we can see the same required attributes marked for a particular model however version A does specify them in a slightly different order to version B. Should order be included as a difference in this way within a definition?

Add integration test

  • Compare one local file to a url using CLI
  • Compare one local file to a url using API

Rules

break

  • Delete path
  • Edit OperationId
  • Delete OperationId
  • Add Param required
  • Edit Param required
  • Edit Param type
  • Edit Param location (in)
  • Edit Host
  • Edit BasePath
  • Edit Array collectionFormat
  • Edit Array items type (in param, response, ...)
  • Add required object property (need to inline)
  • Edit Object Property required (need to inline)
  • Edit Object Property type

smooth

  • Add path
  • Add optional param
  • Delete a param
  • Add optional Object Property
  • Delete Object Property

todo later

  • Edit Security
  • Edit Produced mime types
  • Edit Consumed mime types

Is this project still alive?

My team is currently exploring options for detecting breaking changes between Swagger files. After exploring what is available in the open source world at the moment this project seems the most mature, with the code being well structured and thoroughly tested ๐Ÿ‘

We are juggling between contributing to this project or writing our own equivalent tool. Our concern with contributing to this project is there is there has not been a commit to this codebase in almost 5 months and a PR has been sitting in review for over 3 months.

Here is the list of swagger changes that this tool currently does not detect that we would be interested in adding support for (not comprehensive, just what we discovered during a few hours of spiking):

  • Adding a new status code
  • Changing an existing status code
  • Adding a response body
  • Making required response properties optional
  • Adding a max or min restriction to a numeric response property
  • Adding a max or min restriction to a numeric request property
  • Removing a response body
  • Removing a format from a response property

Is this project still alive?
If we were to start working on this would you prefer many smaller PR's or fewer larger PR's?

not a valid JSON Schema?

Hello,

I have ran swagger-diff on my swagger which is valid and working but I get the following error without too much information:

swagger-diff{"name":"SyntaxError","message":""http://myservice.mycompany.com/swagger/docs/v1\" is not a valid JSON Schema","stack":"SyntaxError: "http://myservice.mycompany.com/swagger/docs/v1\" is not a valid JSON Schema\n at Function.ono [as syntax] (D:\Azure-Agents\Agent1\_work\_tasks\swagger-diff_d0b32dd5-f8bd-4118-8e66-699ad33189a2\1.2.1\node_modules\ono\lib\index.js:61:20)\n at D:\Azure-Agents\Agent1\_work\_tasks\swagger-diff_d0b32dd5-f8bd-4118-8e66-699ad33189a2\1.2.1\node_modules\json-schema-ref-parser\lib\index.js:106:19\n at process._tickCallback (internal/process/next_tick.js:109:7)"}

Can you point me where I can see the reason of the failure?

Thanks,

Greg

Add a bunch of new rules

Add needed rules to remove any unmatchedDiff on Google Drive v2 v3 comparison.

https://api.apis.guru/v2/specs/googleapis.com/drive/v2/swagger.json
https://api.apis.guru/v2/specs/googleapis.com/drive/v3/swagger.json

https://gist.githubusercontent.com/zallek/6ac28225cb907edfe8f1c4714fb13d24/raw/1d99658b22be3eec338139f185738c8b9ebb1509/gistfile1.txt

  • add-description (improve the rule)
  • edit-description (improve the rule)
  • add-method
  • delete-method
  • add-security-requirement
  • edit-security-requirement
  • delete-security-requirement
    (etc)
  • add-info
  • edit-info
  • delete-info

make successful CLI execution silent

in reporter.js,

similar to https://github.com/zallek/swagger-diff/blob/master/src/reporter.js#L29 and https://github.com/zallek/swagger-diff/blob/master/src/reporter.js#L36, move https://github.com/zallek/swagger-diff/blob/master/src/reporter.js#L22 and https://github.com/zallek/swagger-diff/blob/master/src/reporter.js#L15 within their respective if-blocks.

CLI users could then (given the correct config rules) interpret non-empty stdout as evidence of diff failure.

I'm happy to put together a PR if that would help. Thanks in advance!

Separate rule for add-required-object-property

Very nice work, thanks for your efforts.

We have the following situation : we are using object ExportConfiguration in our API. It is being exposed as an output parameter.
We added a new property to this ExportConfiguration object which is required.

Because of the fact we set the rule "add-required-object-property" to cause an error, it will return an error, which makes sense. However, since this object is only being used as an output parameter in the API and it is never being used as an incoming parameter, it does not really break the API.

Does it make sense for the swagger-diff library to have separate rules "add-required-object-property-on-input-parameter" and "add-required-object-property-on-output-parameter"? Or do you suggest another workaround?

All differences are detected only when info.version is equal for compared documents

I use following swagger spec:

{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0.10",
    "title" : "dummy-service"
  },
  "basePath" : "/",
  "tags" : [ {
    "name" : "multipart-support-service"
  }, {
    "name" : "protocol"
  }, {
    "name" : "protocol-v2"
  } ],
  "paths" : {
    "/protocol/version-2/message-changed" : {
      "post" : {
        "tags" : [ "protocol-v2" ],
        "summary" : "Receive operation for protocol service version 2",
        "description" : "This operation is also very important",
        "operationId" : "IProtocolServiceV2_receive_POST",
        "consumes" : [ "application/json" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "in" : "body",
          "name" : "msg",
          "description" : "message to process as Message object",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/Message"
          }
        }, {
          "name" : "User-agent",
          "in" : "header",
          "description" : "User-Agent header parameter",
          "required" : true,
          "type" : "string"
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/Message"
            }
          },
          "400" : {
            "description" : "Invalid arguments passed"
          },
          "404" : {
            "description" : "Something was not found"
          }
        }
      }
    }
  },
  "definitions" : {
    "Message" : {
      "type" : "object",
      "required" : [ "message", "protocol-type" ],
      "properties" : {
        "message" : {
          "type" : "object",
          "description" : "message to process",
          "additionalProperties" : {
            "type" : "object"
          }
        },
        "protocol-type" : {
          "type" : "string",
          "example" : "OLD",
          "description" : "type of protocol",
          "enum" : [ "OLD", "NEW" ]
        }
      },
      "description" : "message type"
    }
  }
}

then I change /protocol/version-2/message to /protocol/version-2/message-changed to create new file and run following command:
target\node\node.exe target\node_modules\swagger-diff\bin\swagger-diff.js swagger-previous.json target/classes/docs/restapi/swagger-new.json

When versions are the same for both files (1.0.10) the result is as expected:

Errors (2)
delete-path                   /protocol/version-2/message - Deleted
add-path                      /protocol/version-2/message-changed - Added
Warnings (0)

When versions are 1.0.10 (previous) and 1.0.11 (new) the result is:

Errors (1)
delete-path                   /protocol/version-2/message - Deleted
Warnings (1)
add-path                      /protocol/version-2/message-changed - Added

For 1.0.10 and 1.0.11-SNAPSHOT the result is:

Errors (0)
Warnings (0)

Make it browser compatible

  • Remove dependencies to fs
  • Remove dependency to require-all -> add a script to generate rules index ...

APIs.guru: showcase and test on real-life APIs

Hi @zallek,

Didn't found your email, so I opened Github issue instead ๐Ÿ˜„
Maybe you would be interested in testing your tool against 200+ real-life Swagger files:
https://github.com/APIs-guru/api-models
They are validated by sway and could accessed through my REST API(no key needed).

You can also compare different versions of some popular API like Google Drive:
https://apis-guru.github.io/api-models/googleapis.com/drive/v1/swagger.yaml
https://apis-guru.github.io/api-models/googleapis.com/drive/v2/swagger.yaml
https://apis-guru.github.io/api-models/googleapis.com/drive/v3/swagger.yaml
IMHO it will help other developers to see all features of your tool.
All links support CORS so you can use it in online demo.

If you have any questions you can ask me directly through Skype(ivangon4arov), Hangout([email protected]) or you can ask them on public chat: https://gitter.im/APIs-guru/api-models

Use npm dependency instead op github link

In the package.json you use the following link:
"json-schema-ref-parser": "github:zallek/json-schema-ref-parser#v1",
To a very old version of your own module.

Could you just link to the regular release on npm?

Reason: my compagny's build system uses an internal npm-repo, and can't handle the external github links... :-(

would be great if you could fix this

The current version of `lodash.*` needs to be updated to avoid security audit warnings

$ npm audit
(partial)
High Prototype Pollution Package lodash.merge Patched in >=4.6.2 Dependency of swagger-diff Path swagger-diff > lodash.defaultsdeep > lodash.merge More info https://nodesecurity.io/advisories/1066 High Prototype Pollution Package lodash.merge Patched in >=4.6.1 Dependency of swagger-diff Path swagger-diff > lodash.defaultsdeep > lodash.merge More info https://nodesecurity.io/advisories/1067

(partial)

Is there a way to get 'method' in 'add-path' rule

Hi,
Is there any way to get the supported methods when a new path is added?

like,
{ "ruleId": "add-path", "message": "/v2/user/id- Added", "path": "/v2/user/id" }
should be
{ "ruleId": "add-path", "message": "/v2/user/id- Added", "path": "/v2/user/id", "method": "get" }
if I have added a new endpoint 'GET /v2/user/id' ?

Regarding a Slight CHange in Output

Hiii..
We want to change the Output from rule-centric to path-centric. It will simplify the output and make it easy to understand. Any help in doing this is much appreciated.

TIA,

Support for OpenAPI3

Hi, is there a consideration to extend this library to support openapi3. Currently, swagger-parser is now v9 and support openapi3 destructuring. Is there an issue with using newer swagger-parser to parse openapi3 doc before using the existing comparison engine? I have tried with the upgraded swagger-parser and there is no error. My only worry is that the comparison engine is not catered for openapi3 and that there are missing component that we need to add on to the comparison engine. Btw, great job on coming up with this package, it's really useful ๐Ÿ‘

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.