All Projects → stoplightio → json-ref-resolver

stoplightio / json-ref-resolver

Licence: Apache-2.0 License
[Deprecated] Recursively resolve JSON pointers and remote authorities.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to json-ref-resolver

Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+2529.63%)
Mutual labels:  resolver, json-schema, swagger, openapi
Datamodel Code Generator
Pydantic model generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
Stars: ✭ 393 (+1355.56%)
Mutual labels:  json-schema, swagger, openapi
Ring Swagger
Swagger Spec for Clojure Web Apps
Stars: ✭ 351 (+1200%)
Mutual labels:  json-schema, swagger, openapi
Spectral
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v2 & v3.
Stars: ✭ 876 (+3144.44%)
Mutual labels:  json-schema, swagger, openapi
fhir-fuel.github.io
Place to prepare proposal to FHIR about JSON, JSON-Schema, Swagger/OpenAPI, JSON native databases and other JSON-frendly formats (yaml, edn, avro, protobuf etc) and technologies
Stars: ✭ 20 (-25.93%)
Mutual labels:  json-schema, swagger, openapi
Full Stack Fastapi Postgresql
Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.
Stars: ✭ 7,635 (+28177.78%)
Mutual labels:  json-schema, swagger, openapi
Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+2977.78%)
Mutual labels:  json-schema, swagger, openapi
Full Stack Fastapi Couchbase
Full stack, modern web application generator. Using FastAPI, Couchbase as database, Docker, automatic HTTPS and more.
Stars: ✭ 243 (+800%)
Mutual labels:  json-schema, swagger, openapi
Spot
Spot is a concise, developer-friendly way to describe your API contract.
Stars: ✭ 230 (+751.85%)
Mutual labels:  json-schema, swagger, openapi
Univalue
High performance RAII C++ JSON library and universal value object class
Stars: ✭ 46 (+70.37%)
Mutual labels:  json-schema, json-data, json-parser
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+146522.22%)
Mutual labels:  json-schema, swagger, openapi
openapi-schemas
JSON Schemas for every version of the OpenAPI Specification
Stars: ✭ 22 (-18.52%)
Mutual labels:  json-schema, swagger, openapi
Prance
Resolving Swagger/OpenAPI 2.0 and 3.0 Parser
Stars: ✭ 133 (+392.59%)
Mutual labels:  resolver, swagger, openapi
PowerJSON
Powerjson is json's improved data format.
Stars: ✭ 24 (-11.11%)
Mutual labels:  json-data, json-parser
format-to-json
An algorithm that can format a string to json-like template. 字符串JSON格式化的算法。
Stars: ✭ 30 (+11.11%)
Mutual labels:  json-schema, json-parser
gin-swagger
DRY templates for go-swagger
Stars: ✭ 79 (+192.59%)
Mutual labels:  swagger, openapi
mock-json-schema
Simple utility to mock example objects based on JSON schema definitions
Stars: ✭ 23 (-14.81%)
Mutual labels:  json-schema, openapi
php-json-schema-model-generator
Creates (immutable) PHP model classes from JSON-Schema files including all validation rules as PHP code
Stars: ✭ 36 (+33.33%)
Mutual labels:  json-schema, openapi
api
🚀 Automatic SDK generation from an OpenAPI definition
Stars: ✭ 127 (+370.37%)
Mutual labels:  swagger, openapi
openapi-generator-for-spring
Open API v3 Generator for Spring Boot applications
Stars: ✭ 54 (+100%)
Mutual labels:  swagger, openapi

JSON Ref Resolver

Buy us a tree CircleCI

Follow $ref values in JSON Schema, OpenAPI (formerly known as Swagger), and any other objects with $ref values inside of them.

Features

  • Performant: Hot paths are memoized, remote URIs are resolved concurrently, and the minimum surface area is crawled and resolved.
  • Caching: Results from remote URIs are cached.
  • Immutable: The original object is not changed, and structural sharing is used to only change relevant bits. example test
  • Reference equality: $refs to the same location will resolve to the same object in memory. example test
  • Flexible: Bring your own readers for http://, file://, mongo://, custom://... etc, or use one of ours.
  • Cross Platform: Supports POSIX and Windows style file paths.
  • Reliable: Well tested to handle all sorts of circular reference edge cases.

Installation

Supported in modern browsers and node.

yarn add @stoplight/json-ref-resolver

Usage

All relevant types and options can be found in src/types.ts.

// Import the Resolver class.
import { Resolver } from "@stoplight/json-ref-resolver";

/**
 * Create a Resolver instance. Resolve can be called on this instance multiple times to take advantage of caching.
 *
 * @param globalOpts {IResolverOpts} [{}]
 *
 * These options are used on every resolve call for this resolver instance.
 *
 * See `IResolverOpts` interface defined in [src/types.ts](src/types.ts) for available options.
 *
 * @return IResolver
 */
const resolver = new Resolver(globalOpts);

/**
 * Resolve the passed in object, replacing all references.

 * @param resolveOpts {any} - The object to resolve.

 * @param resolveOpts {IResolveOpts} [{}]
 *
 * These options override any globalOpts specified on the resolver instance, and only apply during this resolve call.
 *
 * See `IResolveOpts` interface defined in [src/types.ts](src/types.ts) for available options.
 *
 * @return IResolveResult - see [src/types.ts](src/types.ts) for interface definition.
 */
const resolved = await resolver.resolve(sourceObj, resolveOpts);

Example: Basic Inline Dereferencing

By default, only inline references will be dereferenced.

import { Resolver } from "@stoplight/json-ref-resolver";

const resolver = new Resolver();
const resolved = await resolver.resolve({
  user: {
    $ref: "#/models/user"
  },
  models: {
    user: {
      name: "john"
    }
  }
});

// ==> result is the original object, with local refs resolved and replaced
expect(resolved.result).toEqual({
  user: {
    name: "john"
  },
  models: {
    user: {
      name: "john"
    }
  }
});

Example: Dereference a Subset of the Source

This will dereference the minimal number of references needed for the given target, and return the target.

In the example below, the address reference (https://slow-website.com/definitions#/address) will NOT be dereferenced, since it is not needed to resolve the #/user jsonPointer target we have specified. However, #/models/user/card IS dereferenced since it is needed in order to full dereference the #/user property.

import { Resolver } from "@stoplight/json-ref-resolver";

const resolver = new Resolver();
const resolved = await resolver.resolve(
  {
    user: {
      $ref: "#/models/user"
    },
    address: {
      $ref: "https://slow-website.com/definitions#/address"
    },
    models: {
      user: {
        name: "john",
        card: {
          $ref: "#/models/card"
        }
      },
      card: {
        type: "visa"
      }
    }
  },
  {
    jsonPointer: "#/user"
  }
);

// ==> result is the target object, with refs resolved and replaced
expect(resolved.result).toEqual({
  name: "john",
  card: {
    type: "visa"
  }
});

Example: Dereferencing Remote References with Resolvers

By default only inline references (those that point to values inside of the original object) are dereferenced.

In order to dereference remote URIs (file, http, etc) you must provide resolvers for each URI scheme.

Resolvers are keyed by scheme, receive the URI to fetch, and must return the fetched data.

import { Resolver } from "@stoplight/json-ref-resolver";

// some example http library
import * as axios from "axios";

// if we're in node, we create a file resolver with fs
import * as fs from "fs";

// create our resolver instance
const resolver = new Resolver({
  // resolvers can do anything, so long as they define an async read function that resolves to a value
  resolvers: {
    // this resolver will be invoked for refs with the https protocol
    https: {
      async resolve(ref: uri.URI) {
        return axios({
          method: "get",
          url: String(ref)
        });
      }
    },

    // this resolver will be invoked for refs with the file protocol
    file: {
      async resolve(ref: uri.URI) {
        return fs.read(String(ref));
      }
    }
  }
});

const resolved = await resolver.resolve({
  definitions: {
    someOASFile: {
      $ref: "./main.oas2.yml#/definitions/user"
    },
    someMarkdownFile: {
      $ref: "https://foo.com/intro.md"
    }
  }
});

// ==> result is the original object, with refs resolved and replaced
expect(resolved.result).toEqual({
  definitions: {
    someOASFile: {
      // ... the data located in the relative file `./main.oas2.yml` and inner json path `#/definitions/user`
    },
    someMarkdownFile: {
      // ... the data located at the url `https://foo.com/intro.md`
    }
  }
});

Example: Dereferencing Relative Remote References with the baseUri Option

If there are relative remote references (for example, a relative file path ../model.json), then the location of the source data must be specified via the baseUri option. Relative references will be dereferenced against this baseUri.

import { Resolver } from "@stoplight/json-ref-resolver";
import * as fs from "fs";
import * as URI from "urijs";

const resolver = new Resolver({
  readers: {
    file: {
      async read(ref: uri.URI) {
        return fs.read(String(ref));
      }
    }
  }
});

const sourcePath = "/specs/api.json";
const sourceData = fs.readSync(sourcePath);
// sourceData === {
//   user: {
//     $ref: "../models/user.json"
//   }
// }

const resolved = await resolver.resolve(sourceData, {
  // Indicate where the `sourceData` being resolved lives, so that relative remote references can be fetched and resolved.
  baseUri: new URI(sourcePath)
});

expect(resolved.result).toEqual({
  user: {
    // ... the user object defined in `../models/user.json`
  }
});

In the above example, the user $ref will resolve to /models/user.json, because ../models/user.json is resolved against the baseUri of the current document (which was indicated at /specs/api.json). Relative references will not work if the source document has no baseUri set.

This is a simplistic example of a reader. You can create your own, but we have built some readers which you might find useful.

Contributing

If you are interested in contributing to Spectral itself, check out our contributing docs to get started.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].