• Stars
    star
    205
  • Rank 191,264 (Top 4 %)
  • Language
    JavaScript
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects

JSON Schema to OpenAPI Schema

A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects.

Features

  • converts JSON Schema Draft 00 Wright (a.k.a draft v5) to OpenAPI 3.0 Schema Object
  • switches type: ['foo', 'null'] to type: foo and nullable: true
  • supports deep structures with nested allOfs etc.
  • switches patternProperties to x-patternProperties
  • converts dependencies to an allOf + oneOf OpenAPI-valid equivalent

Installation

npm install --save json-schema-to-openapi-schema

Usage

Here's a small example to get the idea:

const toOpenApi = require('json-schema-to-openapi-schema');

const schema = {
  '$schema': 'http://json-schema.org/draft-04/schema#',
  type: ['string', 'null'],
  format: 'date-time',
};

const convertedSchema = toOpenApi(schema);

console.log(convertedSchema);

The example prints out

{
  type: 'string',
  format: 'date-time',
  nullable: true
}

NOTE: $refs are not dereferenced. Use a dereferencer such as json-schema-ref-parser prior to using this package.

Options

The function accepts options object as the second argument.

cloneSchema (boolean)

If set to false, converts the provided schema in place. If true, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults to true.

Why?

OpenAPI is often described as an extension of JSON Schema, but both specs have changed over time and grown independently. OpenAPI v2 was based on JSON Schema draft v4 with a long list of deviations, but OpenAPI v3 shrank that list, upping their support to draft v4 and making the list of discrepancies shorter. Despite OpenAPI v3 closing the gap, the issue of JSON Schema divergence has not been resolved fully.

Diagram showing data model (the objects, payload bodies, etc) and service model (endpoints, headers, metadata, etc)

Carefully writing JSON Schema for your data model kiiiinda works, but it is possible to write JSON Schema that creates invalid OpenAPI, and vice versa. For more on this, read the article OpenAPI and JSON Schema Divergence.

This tool sets out to allow folks to convert from JSON Schema (their one source of truth for everything) to OpenAPI (a thing for HTML docs and making SDKs).

Versions

† Draft v5 is also known as Draft Wright 00, as the drafts are often named after the author, and this was the first one by A. Wright. Amongst other things, draft v5 aimed to rewrite the meta files, but the experiment failed, meaning we need to continue to use the draft v4 metafiles. Ugh.

TODO

Converting Back

To convert the other way, check out openapi-schema-to-json-schema, which this package was based on.

Tests

To run the test-suite:

npm test

Credits

More Repositories

1

speccy

Well Spectually πŸ€“ Enforce quality rules on your OpenAPI 3.0.x specifications.
JavaScript
826
star
2

grabbit

A lightweight transactional message bus on top of RabbitMQ
Go
94
star
3

we-call-gem

Making healthy, happy HTTP calls
Ruby
57
star
4

ray

Resources for building interfaces with WeWork's design system
JavaScript
54
star
5

plasma

JavaScript
47
star
6

rails-sunset

Mark an endpoint as deprecated so everyone knows its going away!
Ruby
27
star
7

we-github-stats

Ruby
23
star
8

we-js-logger

Universal logger with transports to Rollbar and Logentries. Uses bunyan under the hood.
JavaScript
14
star
9

faraday-sunset

Watch out for Sunset headers on HTTP responses, as they signify the deprecation (and eventual removal) of an endpoint.
Ruby
12
star
10

env-universal

Standard environment definition, utilities and constants for browser and node JavaScript applications
JavaScript
8
star
11

express-universal-query-validator

Express middleware to provide consistently parseable query parameters to universal applications.
JavaScript
4
star
12

lex_faq

This Is a sample application for a Ruby on Rails Application that acts as a CMS for an AWS Lex Bot
Ruby
3
star
13

wework.github.io

[DEPRECATED] WeWork Engineering Blog
CSS
3
star
14

monarch

Creates Contentful migrations by mapping over React component proptypes
JavaScript
3
star
15

eslint-config-wework

JavaScript
2
star
16

we-learn-react

JavaScript
2
star
17

dotenv-rails-safe

An extension of the dotenv-rails gem that ensures setting of required environment variables
Ruby
2
star
18

freeze-tag

A rails gem for a more "Stateless" tagging experience
Ruby
1
star