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-types-from-examples

v0.1.1

Published

Generate schema types of request bodies and responses based on given examples for openapi files

Downloads

5

Readme

openapi-schema-types-from-examples

This package makes it possible to add missing schema types to an openapi specification based on the sample request payloads and sample responses of the given openapi specification.

The schema types are generated for all endpoints that examples for the content type 'application json' that are used for request bodies or responses.

Installation

Using npm:

npm i openapi-schema-types-from-examples

Using yarn:

yarn add openapi-schema-types-from-examples

Quick Usage

const fs = require('fs/promises')
const addSchemaTypesFromExamples = require('openapi-schema-types-from-examples')

const openapiSpecificationFile = './swagger.json'

const openapiSpecification = await fs.readFile(openapiSpecificationFile, { encoding: 'utf-8' })
const openapiSpecificationWithTypes = addSchemaTypesFromExamples(openapiSpecification, 'json')

console.log(openapiSpecificationWithTypes);

API

addSchemaTypesFromExamples(openapi: string, format: 'json' | 'yaml' = 'json'): string

Adds the missing schema types to the openapi specification. The schema types will be generated for each request and response based on the examples in the specification.

Example

Original schema

openapi: 3.0.0
info:
  title: Sample
  version: 1.0.0
servers:
  - url: http://{{base_url}}
paths:
  /api/profile:
    get:
      tags:
        - default
      summary: /api/profile
      responses:
        '200':
          description: OK
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                type: object
              example:
                firstName: Max
                lastName: Mustermann
                age: 54
                skills:
                  - name: javascript
                    level: 5
                address:
                  street: Musterstreet
                  zip: '12355'
                  country: Germany
        '404':
          description: Not Found
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                type: object
              example:
                error:
                  code: 404
                  message: Not found
    post:
      tags:
        - default
      summary: /api/profile
      requestBody:
        content:
          application/json:
            schema:
              type: object
              example:
                firstName: Max
                lastName: Mustermann
                age: 54
                skills:
                  - name: javascript
                    level: 5
                address:
                  street: Musterstreet
                  zip: '12355'
                  country: Germany
      responses:
        '200':
          description: OK
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                type: object
              example:
                firstName: Max
                lastName: Mustermann
                age: 54
                skills:
                  - name: javascript
                    level: 5
                address:
                  street: Musterstreet
                  zip: '12355'
                  country: Germany
        '404':
          description: Not Found
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                type: object
              example:
                error:
                  code: 404
                  message: Not found

New schema with types

openapi: 3.0.0
info:
  title: Sample
  version: 1.0.0
servers:
  - url: http://{{base_url}}
paths:
  /api/profile:
    get:
      tags:
        - default
      summary: /api/profile
      responses:
        '200':
          description: OK
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GET_apiprofile_response_200'
              example:
                firstName: Max
                lastName: Mustermann
                age: 54
                skills:
                  - name: javascript
                    level: 5
                address:
                  street: Musterstreet
                  zip: '12355'
                  country: Germany
        '404':
          description: Not Found
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/GET_apiprofile_response_404'
              example:
                error:
                  code: 404
                  message: Not found
    post:
      tags:
        - default
      summary: /api/profile
      requestBody:
        content:
          application/json:
            schema:
              type: object
              example:
                firstName: Max
                lastName: Mustermann
                age: 54
                skills:
                  - name: javascript
                    level: 5
                address:
                  street: Musterstreet
                  zip: '12355'
                  country: Germany
              $ref: '#/components/schemas/POST_apiprofile'
      responses:
        '200':
          description: OK
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/POST_apiprofile_response_200'
              example:
                firstName: Max
                lastName: Mustermann
                age: 54
                skills:
                  - name: javascript
                    level: 5
                address:
                  street: Musterstreet
                  zip: '12355'
                  country: Germany
        '404':
          description: Not Found
          headers:
            Content-Type:
              schema:
                type: string
                example: application/json
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/POST_apiprofile_response_404'
              example:
                error:
                  code: 404
                  message: Not found
components:
  schemas:
    POST_apiprofile:
      type: object
      properties:
        firstName:
          type: string
        lastName:
          type: string
        age:
          type: number
        skills:
          type: array
          items:
            $ref: '#/components/schemas/POST_apiprofile_skills'
        address:
          $ref: '#/components/schemas/POST_apiprofile_address'
    GET_apiprofile_response_200:
      type: object
      properties:
        firstName:
          type: string
        lastName:
          type: string
        age:
          type: number
        skills:
          type: array
          items:
            $ref: '#/components/schemas/GET_apiprofile_response_200_skills'
        address:
          $ref: '#/components/schemas/GET_apiprofile_response_200_address'
    GET_apiprofile_response_404:
      type: object
      properties:
        error:
          $ref: '#/components/schemas/GET_apiprofile_response_404_error'
    POST_apiprofile_response_200:
      type: object
      properties:
        firstName:
          type: string
        lastName:
          type: string
        age:
          type: number
        skills:
          type: array
          items:
            $ref: '#/components/schemas/POST_apiprofile_response_200_skills'
        address:
          $ref: '#/components/schemas/POST_apiprofile_response_200_address'
    POST_apiprofile_response_404:
      type: object
      properties:
        error:
          $ref: '#/components/schemas/POST_apiprofile_response_404_error'
    POST_apiprofile_skills:
      type: object
      properties:
        name:
          type: string
        level:
          type: number
    POST_apiprofile_address:
      type: object
      properties:
        street:
          type: string
        zip:
          type: string
        country:
          type: string
    GET_apiprofile_response_200_skills:
      type: object
      properties:
        name:
          type: string
        level:
          type: number
    GET_apiprofile_response_200_address:
      type: object
      properties:
        street:
          type: string
        zip:
          type: string
        country:
          type: string
    GET_apiprofile_response_404_error:
      type: object
      properties:
        code:
          type: number
        message:
          type: string
    POST_apiprofile_response_200_skills:
      type: object
      properties:
        name:
          type: string
        level:
          type: number
    POST_apiprofile_response_200_address:
      type: object
      properties:
        street:
          type: string
        zip:
          type: string
        country:
          type: string
    POST_apiprofile_response_404_error:
      type: object
      properties:
        code:
          type: number
        message:
          type: string

Use with postman-to-openapi

If postman collections are converted to openapi specifications using postman-to-openapi, the specification does not create any schema types.

In combination of both packages a postman collection can be converted to an openapi specification with schema types.

const fs = require('fs/promises')
const postman2openapi = require('postman-to-openapi')
const addSchemaTypesFromExamples = require('openapi-schema-types-from-examples')

const format = 'json'
const openapiSpecification = await postman2openapi('./postman_collection.json', null, { outputFormat: format })
const openapiSpecificationWithTypes = addSchemaTypesFromExamples(openapiSpecification, format)