Tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Automatic conversion capabilities for transforming Swagger 2.0 and Postman collections to OpenAPI 3.x format, ensuring consistent output regardless of input format.
Convert any supported API definition format to OpenAPI 3.x, with automatic detection and transformation of Swagger 2.0 and Postman collections.
/**
* Convert a given API definition to OpenAPI format if it is not already
* Handles Swagger 2.0 to OpenAPI 3.x conversion and Postman to OpenAPI conversion
* @returns Promise resolving to OpenAPI document
*/
async convert(): Promise<OpenAPI.Document>;Usage Examples:
import OASNormalize from "oas-normalize";
// Convert Swagger 2.0 to OpenAPI 3.x
const swagger20Spec = {
swagger: "2.0",
info: { title: "Pet Store", version: "1.0.0" },
host: "petstore.swagger.io",
basePath: "/v2",
schemes: ["https"],
paths: {
"/pets": {
get: {
summary: "List pets",
responses: {
"200": {
description: "Successful response"
}
}
}
}
}
};
const oas = new OASNormalize(swagger20Spec);
const openapi = await oas.convert();
console.log(openapi.openapi); // "3.0.3" (or similar)
console.log(openapi.servers); // [{ url: "https://petstore.swagger.io/v2" }]// Convert Postman collection to OpenAPI
const postmanCollection = {
info: {
name: "Pet Store API",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
item: [
{
name: "Get Pets",
request: {
method: "GET",
url: "{{baseUrl}}/pets"
}
}
]
};
const oasPostman = new OASNormalize(postmanCollection);
const openapi = await oasPostman.convert();
console.log(openapi.openapi); // "3.0.0"
console.log(openapi.info.title); // Derived from Postman collection// OpenAPI definitions pass through unchanged
const openapi31Spec = {
openapi: "3.1.0",
info: { title: "Already OpenAPI", version: "1.0.0" },
paths: {}
};
const oasAlready = new OASNormalize(openapi31Spec);
const result = await oasAlready.convert();
// Returns the same object (no conversion needed)Converts Swagger 2.0 specifications to OpenAPI 3.x format using the swagger2openapi library:
// Input: Swagger 2.0
const swagger = {
swagger: "2.0",
info: { title: "API", version: "1.0" },
host: "api.example.com",
basePath: "/v1",
schemes: ["https"],
definitions: {
Pet: {
type: "object",
properties: {
name: { type: "string" }
}
}
},
paths: {
"/pets": {
get: {
responses: {
"200": {
description: "Success",
schema: { $ref: "#/definitions/Pet" }
}
}
}
}
}
};
// Output: OpenAPI 3.x equivalent
const converted = await new OASNormalize(swagger).convert();
// Results in:
// - openapi: "3.0.3"
// - servers: [{ url: "https://api.example.com/v1" }]
// - components.schemas.Pet (moved from definitions)
// - Updated response schema referencesKey Swagger 2.0 Conversion Features:
Converts Postman Collections (v2.0/v2.1) to OpenAPI format using @readme/postman-to-openapi:
// Input: Postman Collection
const postmanCollection = {
info: {
name: "My API",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
variable: [
{ key: "baseUrl", value: "https://api.example.com" }
],
item: [
{
name: "Create User",
request: {
method: "POST",
url: "{{baseUrl}}/users",
header: [
{ key: "Content-Type", value: "application/json" }
],
body: {
mode: "raw",
raw: '{"name": "John", "email": "john@example.com"}'
}
}
}
]
};
const converted = await new OASNormalize(postmanCollection).convert();
// Results in OpenAPI 3.x with:
// - Inferred schemas from request/response examples
// - Authentication schemes from Postman auth settings
// - Path parameters from URL templatesKey Postman Conversion Features:
OpenAPI 3.x specifications are returned unchanged:
const openapi = {
openapi: "3.1.0",
info: { title: "Already OpenAPI" },
paths: {}
};
const result = await new OASNormalize(openapi).convert();
// Returns identical object - no conversion performedconst unsupportedSpec = {
// Missing swagger, openapi, or Postman collection indicators
info: { title: "Unknown format" }
};
try {
await new OASNormalize(unsupportedSpec).convert();
} catch (error) {
console.error(error.message); // "The supplied API definition is unsupported."
}const swagger12 = {
swagger: "1.2",
info: { title: "Old Swagger" }
};
try {
await new OASNormalize(swagger12).convert();
} catch (error) {
console.error(error.message); // "Swagger v1.2 is unsupported."
}The convert method works seamlessly with other OASNormalize methods:
const swagger = { swagger: "2.0", /* ... */ };
const oas = new OASNormalize(swagger);
// Convert, then validate the result
const openapi = await oas.convert();
await new OASNormalize(openapi).validate();
// Or validate the converted result directly
await oas.validate(); // Internally converts first if neededThe conversion process uses default settings optimized for compatibility:
swagger2openapi with anchors enabled for better $ref handling// The conversion is NOT cached - each call performs the conversion
const oas = new OASNormalize(swaggerSpec);
const first = await oas.convert(); // Performs conversion
const second = await oas.convert(); // Performs conversion again
console.log(first === second); // false (different object instances)Install with Tessl CLI
npx tessl i tessl/npm-oas-normalize