OpenAPI Merge CLI

1.3.2 · active · verified Wed Apr 22

The `openapi-merge-cli` package provides a command-line interface for merging multiple OpenAPI 3.0 specification files into a single, consolidated specification. It is built upon the `openapi-merge` library, inheriting its core merging algorithm. The tool is currently at version 1.3.2 and appears to be actively maintained in sync with its underlying library. Its primary use case is consolidating specifications from various microservices for exposure behind a single API Gateway, offering features like robust conflict resolution for component names, flexible path modification (stripping or prepending segments), and granular operation selection based on tags. It also supports merging `info.description` fields with custom titles. Unlike many general-purpose OpenAPI tools, its feature set is specifically tailored for API Gateway integration scenarios.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically set up an 'openapi-merge.json' configuration and input files, then execute the 'openapi-merge-cli' tool using Node.js to merge them.

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const configPath = path.join(__dirname, 'openapi-merge.json');
const outputPath = path.join(__dirname, 'output.swagger.json');

const config = {
  "inputs": [
    { "inputFile": "./service-a.swagger.json" },
    {
      "inputFile": "./service-b.swagger.yaml",
      "pathModification": { "prepend": "/api/v2" },
      "operationSelection": { "includeTags": ["public"] }
    }
  ],
  "output": outputPath
};

// Dummy OpenAPI input files for demonstration
const serviceA = `{
  "openapi": "3.0.0",
  "info": { "title": "Service A", "version": "1.0.0" },
  "paths": {
    "/health": {
      "get": {
        "summary": "Health check A",
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}`;

const serviceB = `
openapi: 3.0.0
info:
  title: Service B
  version: 1.0.0
paths:
  /users:
    get:
      tags:
        - public
      summary: List users B
      responses:
        '200':
          description: OK
  /admin:
    get:
      tags:
        - private
      summary: Admin endpoint B
      responses:
        '200':
          description: OK
`;

try {
  // Create dummy input files and configuration file
  fs.writeFileSync(path.join(__dirname, 'service-a.swagger.json'), serviceA);
  fs.writeFileSync(path.join(__dirname, 'service-b.swagger.yaml'), serviceB);
  fs.writeFileSync(configPath, JSON.stringify(config, null, 2));

  console.log('Created openapi-merge.json and dummy input files.');
  console.log('Running openapi-merge-cli...');

  // Execute the CLI tool using npx
  execSync(`npx openapi-merge-cli --config ${configPath}`, { stdio: 'inherit' });

  console.log(`OpenAPI specification merged to ${outputPath}`);
  console.log('You can now inspect output.swagger.json');

  // Optional: Clean up generated files
  // fs.unlinkSync(configPath);
  // fs.unlinkSync(path.join(__dirname, 'service-a.swagger.json'));
  // fs.unlinkSync(path.join(__dirname, 'service-b.swagger.yaml'));
} catch (error) {
  console.error('Failed to merge OpenAPI files:', error.message);
  process.exit(1);
}

view raw JSON →