Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-ts-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-ts-json-schema - npm Package Compare versions

Comparing version 0.12.0 to 0.12.1

dist/utils/convertOpenApiPathsParameters/convertOpenApiParametersToJsonSchema.d.ts

54

dist/openapiToTsJsonSchema.js

@@ -38,7 +38,11 @@ "use strict";

await (0, utils_1.clearFolder)(outputPath);
const schemaParser = new json_schema_ref_parser_1.default();
const bundledOpenApiSchema = await schemaParser.bundle(openApiSchemaPath);
const openApiParser = new json_schema_ref_parser_1.default();
const jsonSchemaParser = new json_schema_ref_parser_1.default();
// Resolve and inline external $ref definitions
const bundledOpenApiSchema = await openApiParser.bundle(openApiSchemaPath);
// Convert oas definitions to JSON schema
const initialJsonSchema = (0, utils_1.convertOpenApiToJsonSchema)(bundledOpenApiSchema);
const inlinedRefs = new Map();
const dereferencedJsonSchema = await schemaParser.dereference(initialJsonSchema, {
// Inline and collect internal $ref definitions
const dereferencedJsonSchema = await jsonSchemaParser.dereference(initialJsonSchema, {
dereference: {

@@ -50,24 +54,26 @@ // @ts-expect-error onDereference seems not to be properly typed

if (!inlinedRefs.has(id)) {
// Make a shallow copy of the ref schema to save it from the mutations below
inlinedRefs.set(id, { ...inlinedSchema });
/**
* "import" refHandling support:
* mark inlined ref objects with a "SCHEMA_ID_SYMBOL" to retrieve their
* original $ref value once inlined
*/
inlinedSchema[utils_1.SCHEMA_ID_SYMBOL] = id;
/**
* "inline" refHandling support:
* add a $ref comment to each inlined schema with the original ref value.
* See: https://github.com/kaelzhang/node-comment-json
*/
if (refHandling === 'inline') {
inlinedSchema[Symbol.for('before')] = [
{
type: 'LineComment',
value: ` $ref: "${ref}"`,
},
];
}
// Shallow copy the ref schema to avoid the mutations below
inlinedRefs.set(id, {
// @ts-expect-error Spread types may only be created from object types
...jsonSchemaParser.$refs.get(ref),
});
}
/**
* mark inlined ref objects with a "SCHEMA_ID_SYMBOL"
* to retrieve their id once inlined
*/
inlinedSchema[utils_1.SCHEMA_ID_SYMBOL] = id;
/**
* "inline" refHandling support:
* add a $ref comment to each inlined schema with the original ref value.
* See: https://github.com/kaelzhang/node-comment-json
*/
if (refHandling === 'inline') {
inlinedSchema[Symbol.for('before')] = [
{
type: 'LineComment',
value: ` $ref: "${ref}"`,
},
];
}
},

@@ -74,0 +80,0 @@ },

import type { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema';
import type { ParameterObject, ReferenceObject } from 'openapi3-ts/oas31';
export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;

@@ -12,2 +13,3 @@ export type JSONSchemaWithPlaceholders = JSONSchema | string;

}) => string;
export type OpenApiParameter = ParameterObject | ReferenceObject;
import type { makeRelativeModulePath, formatTypeScript, saveFile } from './utils';

@@ -14,0 +16,0 @@ export type Options = {

@@ -24,3 +24,3 @@ "use strict";

// Convert components.parameters after convertOpenApiPathsParameters is called
if ((0, _1.isOpenApiParameter)(schema)) {
if ((0, _1.isOpenApiParameterObject)(schema)) {
schema = (0, _1.convertOpenApiParameterToJsonSchema)(schema);

@@ -27,0 +27,0 @@ }

@@ -1,2 +0,5 @@

import type { JSONSchema } from '../types';
export declare function convertOpenApiParameterToJsonSchema(parameter: JSONSchema): import("json-schema").JSONSchema4;
import type { JSONSchema, OpenApiParameter } from '../types';
/**
* Convert one single OpenAPI parameter to JSON schema
*/
export declare function convertOpenApiParameterToJsonSchema(parameter: OpenApiParameter): JSONSchema;

@@ -5,3 +5,7 @@ "use strict";

const openapi_schema_to_json_schema_1 = require("@openapi-contrib/openapi-schema-to-json-schema");
/**
* Convert one single OpenAPI parameter to JSON schema
*/
function convertOpenApiParameterToJsonSchema(parameter) {
// @ts-expect-error openapi3-ts types seem to conflict with @openapi-contrib/openapi-schema-to-json-schema ones
const schema = (0, openapi_schema_to_json_schema_1.fromParameter)(parameter, { strictMode: false });

@@ -8,0 +12,0 @@ // $schema is appended by @openapi-contrib/openapi-schema-to-json-schema

import type { OpenApiSchema } from '../types';
/**
* Traverse the openAPI schema tree an brutally try to convert everything
* possible to JSON schema. We are probably overdoing since we process any object we find.
* Traverse the openAPI schema tree an brutally try to convert every oas definition
* to JSON schema. We are probably overdoing since we process any found object.
*
* - Is there a way to tell an OpenAPI schema objects convertible to JSON schema from the others?
* - Could we explicitly convert only the properties where we know conversion is needed?
* - Is there a way to tell an OpenAPI definition objects convertible to JSON schema from the others?
* - Could we explicitly convert only the properties that need it?
*

@@ -9,0 +9,0 @@ * @TODO Find a nicer way to convert convert all the expected OpenAPI schemas

@@ -20,3 +20,3 @@ "use strict";

*/
if ((0, _1.isOpenApiParameter)(value)) {
if ((0, _1.isOpenApiParameterObject)(value)) {
return value;

@@ -37,7 +37,7 @@ }

/**
* Traverse the openAPI schema tree an brutally try to convert everything
* possible to JSON schema. We are probably overdoing since we process any object we find.
* Traverse the openAPI schema tree an brutally try to convert every oas definition
* to JSON schema. We are probably overdoing since we process any found object.
*
* - Is there a way to tell an OpenAPI schema objects convertible to JSON schema from the others?
* - Could we explicitly convert only the properties where we know conversion is needed?
* - Is there a way to tell an OpenAPI definition objects convertible to JSON schema from the others?
* - Could we explicitly convert only the properties that need it?
*

@@ -44,0 +44,0 @@ * @TODO Find a nicer way to convert convert all the expected OpenAPI schemas

@@ -16,3 +16,3 @@ export { patchJsonSchema } from './makeTsJsonSchema/patchJsonSchema';

export { filenamify } from './filenamify';
export { isOpenApiParameter } from './isOpenApiParameter';
export { isOpenApiParameterObject } from './isOpenApiParameterObject';
export { clearFolder } from './clearFolder';

@@ -19,0 +19,0 @@ export { makeRelativeModulePath } from './makeRelativeModulePath';

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveFile = exports.formatTypeScript = exports.makeRelativeModulePath = exports.clearFolder = exports.isOpenApiParameter = exports.filenamify = exports.isObject = exports.addSchemaToMetaData = exports.replacePlaceholdersWithImportedSchemas = exports.replaceInlinedRefsWithStringPlaceholder = exports.idToPlaceholder = exports.PLACEHOLDER_REGEX = exports.SCHEMA_ID_SYMBOL = exports.makeId = exports.refToId = exports.parseId = exports.makeTsJsonSchemaFiles = exports.convertOpenApiParameterToJsonSchema = exports.convertOpenApiToJsonSchema = exports.convertOpenApiPathsParameters = exports.makeTsJsonSchema = exports.patchJsonSchema = void 0;
exports.saveFile = exports.formatTypeScript = exports.makeRelativeModulePath = exports.clearFolder = exports.isOpenApiParameterObject = exports.filenamify = exports.isObject = exports.addSchemaToMetaData = exports.replacePlaceholdersWithImportedSchemas = exports.replaceInlinedRefsWithStringPlaceholder = exports.idToPlaceholder = exports.PLACEHOLDER_REGEX = exports.SCHEMA_ID_SYMBOL = exports.makeId = exports.refToId = exports.parseId = exports.makeTsJsonSchemaFiles = exports.convertOpenApiParameterToJsonSchema = exports.convertOpenApiToJsonSchema = exports.convertOpenApiPathsParameters = exports.makeTsJsonSchema = exports.patchJsonSchema = void 0;
var patchJsonSchema_1 = require("./makeTsJsonSchema/patchJsonSchema");

@@ -36,4 +36,4 @@ Object.defineProperty(exports, "patchJsonSchema", { enumerable: true, get: function () { return patchJsonSchema_1.patchJsonSchema; } });

Object.defineProperty(exports, "filenamify", { enumerable: true, get: function () { return filenamify_1.filenamify; } });
var isOpenApiParameter_1 = require("./isOpenApiParameter");
Object.defineProperty(exports, "isOpenApiParameter", { enumerable: true, get: function () { return isOpenApiParameter_1.isOpenApiParameter; } });
var isOpenApiParameterObject_1 = require("./isOpenApiParameterObject");
Object.defineProperty(exports, "isOpenApiParameterObject", { enumerable: true, get: function () { return isOpenApiParameterObject_1.isOpenApiParameterObject; } });
var clearFolder_1 = require("./clearFolder");

@@ -40,0 +40,0 @@ Object.defineProperty(exports, "clearFolder", { enumerable: true, get: function () { return clearFolder_1.clearFolder; } });

@@ -5,4 +5,4 @@ import type { JSONSchema, JSONSchemaWithPlaceholders } from '../../types';

* (marked with a SCHEMA_ID_SYMBOL property holding the original $ref value)
* with a string placeholder with a reference to the original $ref value ("_OTJS-START_/id/value_OTJS-END_")
* with a string placeholder with a reference to their internal id ("_OTJS-START_/id/value_OTJS-END_")
*/
export declare function replaceInlinedRefsWithStringPlaceholder(schema: JSONSchema): JSONSchemaWithPlaceholders;

@@ -12,3 +12,3 @@ "use strict";

* Get any JSON schema node and:
* - Return ref placeholder is the entity is an inlined ref schema objects (with SCHEMA_ID_SYMBOL prop)
* - Return id placeholder if the entity is an inlined ref schema objects (with SCHEMA_ID_SYMBOL prop)
* - Return provided node in all other cases

@@ -26,3 +26,3 @@ */

* (marked with a SCHEMA_ID_SYMBOL property holding the original $ref value)
* with a string placeholder with a reference to the original $ref value ("_OTJS-START_/id/value_OTJS-END_")
* with a string placeholder with a reference to their internal id ("_OTJS-START_/id/value_OTJS-END_")
*/

@@ -29,0 +29,0 @@ function replaceInlinedRefsWithStringPlaceholder(schema) {

import type { SchemaMetaDataMap } from '../../types';
/**
* Replace Refs placeholders with imported schemas
* Replace id placeholders with imported schemas
*/

@@ -5,0 +5,0 @@ export declare function replacePlaceholdersWithImportedSchemas({ schemaAsText, absoluteDirName, schemaMetaDataMap, }: {

@@ -6,3 +6,3 @@ "use strict";

/**
* Replace Refs placeholders with imported schemas
* Replace id placeholders with imported schemas
*/

@@ -9,0 +9,0 @@ function replacePlaceholdersWithImportedSchemas({ schemaAsText, absoluteDirName, schemaMetaDataMap, }) {

/**
* Replace Refs placeholders with original ref objects
* Replace id placeholders with their relevant $ref object
*/

@@ -4,0 +4,0 @@ export declare function replacePlaceholdersWithRefs({ schemaAsText, refMapper, }: {

@@ -6,3 +6,3 @@ "use strict";

/**
* Replace Refs placeholders with original ref objects
* Replace id placeholders with their relevant $ref object
*/

@@ -9,0 +9,0 @@ function replacePlaceholdersWithRefs({ schemaAsText, refMapper, }) {

{
"name": "openapi-ts-json-schema",
"version": "0.12.0",
"version": "0.12.1",
"description": "OpenAPI to JSON schema generator with TypeScript in mind",

@@ -45,2 +45,3 @@ "main": "./dist/index.js",

"fastify": "^4.23.2",
"openapi3-ts": "^4.3.1",
"rimraf": "^5.0.1",

@@ -47,0 +48,0 @@ "simple-git-hooks": "^2.9.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc