@kubb/oas 3.10.10 → 3.10.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -6,15 +6,15 @@ var remeda = require('remeda');
6
6
  var openapiCore = require('@redocly/openapi-core');
7
7
  var OASNormalize = require('oas-normalize');
8
8
  var swagger2openapi = require('swagger2openapi');
9
- var BaseOas = require('oas');
10
9
  var jsonpointer = require('jsonpointer');
10
+ var BaseOas = require('oas');
11
11
 
12
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
13
 
14
14
  var OASNormalize__default = /*#__PURE__*/_interopDefault(OASNormalize);
15
15
  var swagger2openapi__default = /*#__PURE__*/_interopDefault(swagger2openapi);
16
- var BaseOas__default = /*#__PURE__*/_interopDefault(BaseOas);
17
16
  var jsonpointer__default = /*#__PURE__*/_interopDefault(jsonpointer);
17
+ var BaseOas__default = /*#__PURE__*/_interopDefault(BaseOas);
18
18
 
19
19
  // src/types.ts
20
20
  var HttpMethods = {
@@ -73,12 +73,17 @@ var Oas = class extends BaseOas__default.default {
73
73
  if (!isDiscriminator(schema)) {
74
74
  return void 0;
75
75
  }
76
- const mapping = schema.discriminator.mapping || {};
76
+ const { mapping = {}, propertyName } = schema.discriminator;
77
77
  if (schema.oneOf) {
78
78
  schema.oneOf.forEach((schema2) => {
79
79
  if (isReference(schema2)) {
80
80
  const key = this.getKey(schema2.$ref);
81
- if (key && !Object.values(mapping).includes(schema2.$ref)) {
81
+ const refSchema = this.get(schema2.$ref);
82
+ const propertySchema = refSchema.properties?.[propertyName];
83
+ const canAdd = key && !Object.values(mapping).includes(schema2.$ref);
84
+ if (canAdd && propertySchema?.enum?.length === 1) {
85
+ mapping[propertySchema.enum[0]] = schema2.$ref;
86
+ } else if (canAdd) {
82
87
  mapping[key] = schema2.$ref;
83
88
  }
84
89
  }
@@ -88,7 +93,12 @@ var Oas = class extends BaseOas__default.default {
88
93
  schema.anyOf.forEach((schema2) => {
89
94
  if (isReference(schema2)) {
90
95
  const key = this.getKey(schema2.$ref);
91
- if (key && !Object.values(mapping).includes(schema2.$ref)) {
96
+ const refSchema = this.get(schema2.$ref);
97
+ const propertySchema = refSchema.properties?.[propertyName];
98
+ const canAdd = key && !Object.values(mapping).includes(schema2.$ref);
99
+ if (canAdd && propertySchema?.enum?.length === 1) {
100
+ mapping[propertySchema.enum[0]] = schema2.$ref;
101
+ } else if (canAdd) {
92
102
  mapping[key] = schema2.$ref;
93
103
  }
94
104
  }
@@ -133,7 +143,7 @@ var Oas = class extends BaseOas__default.default {
133
143
  }
134
144
  return responseBody.content[contentType];
135
145
  }
136
- let availablecontentType = void 0;
146
+ let availablecontentType;
137
147
  const contentTypes = Object.keys(responseBody.content);
138
148
  contentTypes.forEach((mt) => {
139
149
  if (!availablecontentType && utils.matchesMimeType.json(mt)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["BaseOas","jsonpointer","schema","matchesMimeType","OASNormalize","isPlainObject","isRef","loadConfig","bundle","swagger2openapi"],"mappings":";;;;;;;;;;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAAA,MAAA;AAAA,EACN,GAAK,EAAA,KAAA;AAAA,EACL,KAAO,EAAA,OAAA;AAAA,EACP,MAAQ,EAAA,QAAA;AAAA,EACR,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,KAAO,EAAA;AACT;ACNa,IAAA,GAAA,GAAN,cAAwCA,wBAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EAAE,GAAA,EAAK,MAA2D,EAAA,OAAA,GAAmB,EAAI,EAAA;AACnG,IAAI,IAAA,OAAO,QAAQ,QAAU,EAAA;AAC3B,MAAM,GAAA,GAAA,IAAA,CAAK,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AAA,aAAA,GAAgB,MAAMC,kBAAO,CAAA,EAAE,KAAK,SAAW,EAAA,MAAA,EAAQ,IAAM,EAAA,SAAA,EAAW,CAAA;AAE9E,IAAO,OAAA,KAAA,CAAM,aAAc,CAAA,MAAA,CAAO,MAAM,CAAA;AAAA;AAG1C,EAAM,MAAA,YAAA,GAAe,IAAIJ,6BAAAA,CAAa,SAAW,EAAA;AAAA,IAC/C,WAAa,EAAA,IAAA;AAAA,IACb,cAAgB,EAAA;AAAA,GACjB,CAAA;AACD,EAAM,MAAA,QAAA,GAAY,MAAM,YAAA,CAAa,IAAK,EAAA;AAE1C,EAAI,IAAA,mBAAA,CAAoB,QAAQ,CAAG,EAAA;AACjC,IAAA,MAAM,EAAE,OAAQ,EAAA,GAAI,MAAMK,gCAAA,CAAgB,WAAW,QAAU,EAAA;AAAA,MAC7D,OAAS,EAAA;AAAA,KACV,CAAA;AAED,IAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,SAAwB,CAAA;AAAA;AAGrD,EAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,UAAU,CAAA;AACvC","file":"index.cjs","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport { matchesMimeType } from 'oas/utils'\n\nimport jsonpointer from 'jsonpointer'\n\nimport { isDiscriminator, isReference } from './utils.ts'\n\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport type { OpenAPIV3 } from 'openapi-types'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n this.#options = options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const mapping = schema.discriminator.mapping || {}\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n\n if (key && !Object.values(mapping).includes(schema.$ref)) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n\n if (key && !Object.values(mapping).includes(schema.$ref)) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined = undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n await oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {\n if (typeof pathOrApi === 'string') {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths: true,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n"]}
1
+ {"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["BaseOas","jsonpointer","schema","matchesMimeType","OASNormalize","isPlainObject","isRef","loadConfig","bundle","swagger2openapi"],"mappings":";;;;;;;;;;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAAA,MAAA;AAAA,EACN,GAAK,EAAA,KAAA;AAAA,EACL,KAAO,EAAA,OAAA;AAAA,EACP,MAAQ,EAAA,QAAA;AAAA,EACR,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,KAAO,EAAA;AACT;ACTa,IAAA,GAAA,GAAN,cAAwCA,wBAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EAAE,GAAA,EAAK,MAA2D,EAAA,OAAA,GAAmB,EAAI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AI,MAAMK,gCAAA,CAAgB,WAAW,QAAU,EAAA;AAAA,MAC7D,OAAS,EAAA;AAAA,KACV,CAAA;AAED,IAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,SAAwB,CAAA;AAAA;AAGrD,EAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,UAAU,CAAA;AACvC","file":"index.cjs","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import jsonpointer from 'jsonpointer'\nimport BaseOas from 'oas'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { matchesMimeType } from 'oas/utils'\nimport OASNormalize from 'oas-normalize'\nimport type { OpenAPIV3 } from 'openapi-types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport { isDiscriminator, isReference } from './utils.ts'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n this.#options = options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const { mapping = {}, propertyName } = schema.discriminator\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n await oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {\n if (typeof pathOrApi === 'string') {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths: true,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n"]}
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ import { isPlainObject } from 'remeda';
5
5
  import { loadConfig, bundle } from '@redocly/openapi-core';
6
6
  import OASNormalize from 'oas-normalize';
7
7
  import swagger2openapi from 'swagger2openapi';
8
- import BaseOas from 'oas';
9
8
  import jsonpointer from 'jsonpointer';
9
+ import BaseOas from 'oas';
10
10
 
11
11
  // src/types.ts
12
12
  var HttpMethods = {
@@ -65,12 +65,17 @@ var Oas = class extends BaseOas {
65
65
  if (!isDiscriminator(schema)) {
66
66
  return void 0;
67
67
  }
68
- const mapping = schema.discriminator.mapping || {};
68
+ const { mapping = {}, propertyName } = schema.discriminator;
69
69
  if (schema.oneOf) {
70
70
  schema.oneOf.forEach((schema2) => {
71
71
  if (isReference(schema2)) {
72
72
  const key = this.getKey(schema2.$ref);
73
- if (key && !Object.values(mapping).includes(schema2.$ref)) {
73
+ const refSchema = this.get(schema2.$ref);
74
+ const propertySchema = refSchema.properties?.[propertyName];
75
+ const canAdd = key && !Object.values(mapping).includes(schema2.$ref);
76
+ if (canAdd && propertySchema?.enum?.length === 1) {
77
+ mapping[propertySchema.enum[0]] = schema2.$ref;
78
+ } else if (canAdd) {
74
79
  mapping[key] = schema2.$ref;
75
80
  }
76
81
  }
@@ -80,7 +85,12 @@ var Oas = class extends BaseOas {
80
85
  schema.anyOf.forEach((schema2) => {
81
86
  if (isReference(schema2)) {
82
87
  const key = this.getKey(schema2.$ref);
83
- if (key && !Object.values(mapping).includes(schema2.$ref)) {
88
+ const refSchema = this.get(schema2.$ref);
89
+ const propertySchema = refSchema.properties?.[propertyName];
90
+ const canAdd = key && !Object.values(mapping).includes(schema2.$ref);
91
+ if (canAdd && propertySchema?.enum?.length === 1) {
92
+ mapping[propertySchema.enum[0]] = schema2.$ref;
93
+ } else if (canAdd) {
84
94
  mapping[key] = schema2.$ref;
85
95
  }
86
96
  }
@@ -125,7 +135,7 @@ var Oas = class extends BaseOas {
125
135
  }
126
136
  return responseBody.content[contentType];
127
137
  }
128
- let availablecontentType = void 0;
138
+ let availablecontentType;
129
139
  const contentTypes = Object.keys(responseBody.content);
130
140
  contentTypes.forEach((mt) => {
131
141
  if (!availablecontentType && matchesMimeType.json(mt)) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["schema","OASNormalize"],"mappings":";;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAAA,MAAA;AAAA,EACN,GAAK,EAAA,KAAA;AAAA,EACL,KAAO,EAAA,OAAA;AAAA,EACP,MAAQ,EAAA,QAAA;AAAA,EACR,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,KAAO,EAAA;AACT;ACNa,IAAA,GAAA,GAAN,cAAwC,OAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAE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AA,GAAuB,GAAmB,EAAA;AACrG,EAAI,IAAA,OAAO,cAAc,QAAU,EAAA;AAEjC,IAAM,MAAA,MAAA,GAAS,MAAM,UAAW,EAAA;AAChC,IAAM,MAAA,aAAA,GAAgB,MAAM,MAAO,CAAA,EAAE,KAAK,SAAW,EAAA,MAAA,EAAQ,IAAM,EAAA,SAAA,EAAW,CAAA;AAE9E,IAAO,OAAA,KAAA,CAAM,aAAc,CAAA,MAAA,CAAO,MAAM,CAAA;AAAA;AAG1C,EAAM,MAAA,YAAA,GAAe,IAAIC,YAAAA,CAAa,SAAW,EAAA;AAAA,IAC/C,WAAa,EAAA,IAAA;AAAA,IACb,cAAgB,EAAA;AAAA,GACjB,CAAA;AACD,EAAM,MAAA,QAAA,GAAY,MAAM,YAAA,CAAa,IAAK,EAAA;AAE1C,EAAI,IAAA,mBAAA,CAAoB,QAAQ,CAAG,EAAA;AACjC,IAAA,MAAM,EAAE,OAAQ,EAAA,GAAI,MAAM,eAAA,CAAgB,WAAW,QAAU,EAAA;AAAA,MAC7D,OAAS,EAAA;AAAA,KACV,CAAA;AAED,IAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,SAAwB,CAAA;AAAA;AAGrD,EAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,UAAU,CAAA;AACvC","file":"index.js","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import BaseOas from 'oas'\nimport OASNormalize from 'oas-normalize'\nimport { matchesMimeType } from 'oas/utils'\n\nimport jsonpointer from 'jsonpointer'\n\nimport { isDiscriminator, isReference } from './utils.ts'\n\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport type { OpenAPIV3 } from 'openapi-types'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n this.#options = options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const mapping = schema.discriminator.mapping || {}\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n\n if (key && !Object.values(mapping).includes(schema.$ref)) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n\n if (key && !Object.values(mapping).includes(schema.$ref)) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined = undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n await oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {\n if (typeof pathOrApi === 'string') {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths: true,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n"]}
1
+ {"version":3,"sources":["../src/types.ts","../src/Oas.ts","../src/utils.ts"],"names":["schema","OASNormalize"],"mappings":";;;;;;;;;;;AAeO,IAAM,WAAc,GAAA;AAAA,EACzB,GAAK,EAAA,KAAA;AAAA,EACL,IAAM,EAAA,MAAA;AAAA,EACN,GAAK,EAAA,KAAA;AAAA,EACL,KAAO,EAAA,OAAA;AAAA,EACP,MAAQ,EAAA,QAAA;AAAA,EACR,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,KAAO,EAAA;AACT;ACTa,IAAA,GAAA,GAAN,cAAwC,OAAQ,CAAA;AAAA,EACrD,WAAoB,EAAC;AAAA,EACrB,QAAiB,GAAA,MAAA;AAAA,EAEjB,YAAY,EA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M,EAAE,OAAQ,EAAA,GAAI,MAAM,eAAA,CAAgB,WAAW,QAAU,EAAA;AAAA,MAC7D,OAAS,EAAA;AAAA,KACV,CAAA;AAED,IAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,SAAwB,CAAA;AAAA;AAGrD,EAAA,OAAO,IAAI,QAAA,CAAS,EAAE,GAAA,EAAK,UAAU,CAAA;AACvC","file":"index.js","sourcesContent":["import type * as OasTypes from 'oas/types'\n\n// external packages\nexport type { Operation } from 'oas/operation'\nexport type { HttpMethods as HttpMethod } from 'oas/types'\nexport type * as OasTypes from 'oas/types'\nexport type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\n\nexport type contentType = 'application/json' | (string & {})\n\nexport type SchemaObject = OasTypes.SchemaObject & {\n 'x-nullable'?: boolean\n $ref?: string\n}\n\nexport const HttpMethods = {\n GET: 'get',\n POST: 'post',\n PUT: 'put',\n PATCH: 'patch',\n DELETE: 'delete',\n HEAD: 'head',\n OPTIONS: 'options',\n TRACE: 'trace',\n} satisfies Record<Uppercase<OasTypes.HttpMethods>, OasTypes.HttpMethods>\n","import jsonpointer from 'jsonpointer'\nimport BaseOas from 'oas'\nimport type { Operation } from 'oas/operation'\nimport type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'\nimport { matchesMimeType } from 'oas/utils'\nimport OASNormalize from 'oas-normalize'\nimport type { OpenAPIV3 } from 'openapi-types'\nimport type { OasTypes } from './index.ts'\nimport type { contentType } from './types.ts'\nimport { isDiscriminator, isReference } from './utils.ts'\n\ntype Options = {\n contentType?: contentType\n}\n\nexport class Oas<const TOAS = unknown> extends BaseOas {\n #options: Options = {}\n document: TOAS = undefined as unknown as TOAS\n\n constructor({ oas, user }: { oas: TOAS | OASDocument | string; user?: User }, options: Options = {}) {\n if (typeof oas === 'string') {\n oas = JSON.parse(oas)\n }\n\n super(oas as OASDocument, user)\n\n this.document = oas as TOAS\n this.#options = options\n }\n\n get($ref: string) {\n const origRef = $ref\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n } else {\n return null\n }\n const current = jsonpointer.get(this.api, $ref)\n\n if (!current) {\n throw new Error(`Could not find a definition for ${origRef}.`)\n }\n return current\n }\n\n getKey($ref: string) {\n const key = $ref.split('/').pop()\n return key === '' ? undefined : key\n }\n set($ref: string, value: unknown) {\n $ref = $ref.trim()\n if ($ref === '') {\n return false\n }\n if ($ref.startsWith('#')) {\n $ref = globalThis.decodeURIComponent($ref.substring(1))\n\n jsonpointer.set(this.api, $ref, value)\n }\n }\n\n getDiscriminator(schema: OasTypes.SchemaObject): OpenAPIV3.DiscriminatorObject | undefined {\n if (!isDiscriminator(schema)) {\n return undefined\n }\n\n const { mapping = {}, propertyName } = schema.discriminator\n\n // loop over oneOf and add default mapping when none is defined\n if (schema.oneOf) {\n schema.oneOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n if (schema.anyOf) {\n schema.anyOf.forEach((schema) => {\n if (isReference(schema)) {\n const key = this.getKey(schema.$ref)\n const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)\n // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669\n const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject\n const canAdd = key && !Object.values(mapping).includes(schema.$ref)\n\n if (canAdd && propertySchema?.enum?.length === 1) {\n mapping[propertySchema.enum[0]] = schema.$ref\n } else if (canAdd) {\n mapping[key] = schema.$ref\n }\n }\n })\n }\n\n return {\n ...schema.discriminator,\n mapping,\n }\n }\n\n // TODO add better typing\n dereferenceWithRef(schema?: unknown) {\n if (isReference(schema)) {\n return {\n ...schema,\n ...this.get(schema.$ref),\n $ref: schema.$ref,\n }\n }\n\n return schema\n }\n\n /**\n * Oas does not have a getResponseBody(contentType)\n */\n #getResponseBodyFactory(responseBody: boolean | ResponseObject): (contentType?: string) => MediaTypeObject | false | [string, MediaTypeObject, ...string[]] {\n function hasResponseBody(res = responseBody): res is ResponseObject {\n return !!res\n }\n\n return (contentType) => {\n if (!hasResponseBody(responseBody)) {\n return false\n }\n\n if (isReference(responseBody)) {\n // If the request body is still a `$ref` pointer we should return false because this library\n // assumes that you've run dereferencing beforehand.\n return false\n }\n\n if (!responseBody.content) {\n return false\n }\n\n if (contentType) {\n if (!(contentType in responseBody.content)) {\n return false\n }\n\n return responseBody.content[contentType]!\n }\n\n // Since no media type was supplied we need to find either the first JSON-like media type that\n // we've got, or the first available of anything else if no JSON-like media types are present.\n let availablecontentType: string | undefined\n const contentTypes = Object.keys(responseBody.content)\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType && matchesMimeType.json(mt)) {\n availablecontentType = mt\n }\n })\n\n if (!availablecontentType) {\n contentTypes.forEach((mt: string) => {\n if (!availablecontentType) {\n availablecontentType = mt\n }\n })\n }\n\n if (availablecontentType) {\n return [availablecontentType, responseBody.content[availablecontentType]!, ...(responseBody.description ? [responseBody.description] : [])]\n }\n\n return false\n }\n }\n\n getResponseSchema(operation: Operation, statusCode: string | number): SchemaObject {\n if (operation.schema.responses) {\n Object.keys(operation.schema.responses).forEach((key) => {\n const schema = operation.schema.responses![key]\n const $ref = isReference(schema) ? schema.$ref : undefined\n\n if (schema && $ref) {\n operation.schema.responses![key] = this.get($ref)\n }\n })\n }\n\n const getResponseBody = this.#getResponseBodyFactory(operation.getResponseByStatusCode(statusCode))\n\n const { contentType } = this.#options\n const responseBody = getResponseBody(contentType)\n\n if (responseBody === false) {\n // return empty object because response will always be defined(request does not need a body)\n return {}\n }\n\n const schema = Array.isArray(responseBody) ? responseBody[1].schema : responseBody.schema\n\n if (!schema) {\n // return empty object because response will always be defined(request does not need a body)\n\n return {}\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getRequestSchema(operation: Operation): SchemaObject | undefined {\n const { contentType } = this.#options\n\n if (operation.schema.requestBody) {\n operation.schema.requestBody = this.dereferenceWithRef(operation.schema.requestBody)\n }\n\n const requestBody = operation.getRequestBody(contentType)\n\n if (requestBody === false) {\n return undefined\n }\n\n const schema = Array.isArray(requestBody) ? requestBody[1].schema : requestBody.schema\n\n if (!schema) {\n return undefined\n }\n\n return this.dereferenceWithRef(schema)\n }\n\n getParametersSchema(operation: Operation, inKey: 'path' | 'query' | 'header'): SchemaObject | null {\n const { contentType = operation.getContentType() } = this.#options\n const params = operation\n .getParameters()\n .map((schema) => {\n return this.dereferenceWithRef(schema)\n })\n .filter((v) => v.in === inKey)\n\n if (!params.length) {\n return null\n }\n\n return params.reduce(\n (schema, pathParameters) => {\n const property = pathParameters.content?.[contentType]?.schema ?? (pathParameters.schema as SchemaObject)\n const required = [...(schema.required || ([] as any)), pathParameters.required ? pathParameters.name : undefined].filter(Boolean)\n\n return {\n ...schema,\n description: schema.description,\n deprecated: schema.deprecated,\n example: schema.example,\n required,\n properties: {\n ...schema.properties,\n [pathParameters.name]: {\n description: pathParameters.description,\n ...property,\n },\n },\n }\n },\n { type: 'object', required: [], properties: {} } as SchemaObject,\n )\n }\n\n async valdiate() {\n const oasNormalize = new OASNormalize(this.api, {\n enablePaths: true,\n colorizeErrors: true,\n })\n\n await oasNormalize.validate({\n parser: {\n validate: {\n errors: {\n colorize: true,\n },\n },\n },\n })\n }\n}\n","import { isRef, isSchema } from 'oas/types'\nimport { isPlainObject } from 'remeda'\n\nimport { bundle, loadConfig } from '@redocly/openapi-core'\nimport OASNormalize from 'oas-normalize'\nimport type { OASDocument } from 'oas/types'\nimport type { ParameterObject, SchemaObject } from 'oas/types'\nimport type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'\nimport swagger2openapi from 'swagger2openapi'\nimport { Oas } from './Oas.ts'\n\nexport function isOpenApiV2Document(doc: any): doc is OpenAPIV2.Document {\n return doc && isPlainObject(doc) && !('openapi' in doc)\n}\nexport function isOpenApiV3Document(doc: any): doc is OpenAPIV3.Document {\n return doc && isPlainObject(doc) && 'openapi' in doc\n}\n\nexport function isOpenApiV3_1Document(doc: any): doc is OpenAPIV3_1.Document {\n return doc && isPlainObject<OpenAPIV3_1.Document>(doc) && 'openapi' in doc && doc.openapi.startsWith('3.1')\n}\n\nexport function isJSONSchema(obj?: unknown): obj is SchemaObject {\n return !!obj && isSchema(obj)\n}\n\nexport function isParameterObject(obj: ParameterObject | SchemaObject): obj is ParameterObject {\n return obj && 'in' in obj\n}\n\n/**\n * Determines if a schema is nullable, considering both the standard `nullable` property and the legacy `x-nullable` extension.\n *\n * @param schema - The schema object to check.\n * @returns `true` if the schema is marked as nullable; otherwise, `false`.\n */\nexport function isNullable(schema?: SchemaObject & { 'x-nullable'?: boolean }): boolean {\n return schema?.nullable ?? schema?.['x-nullable'] ?? false\n}\n\n/**\n * Determines if the given object is an OpenAPI ReferenceObject.\n *\n * @returns True if {@link obj} is a ReferenceObject; otherwise, false.\n */\nexport function isReference(obj?: any): obj is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {\n return !!obj && isRef(obj)\n}\n\n/**\n * Determines if the given object is a SchemaObject with a discriminator property of type DiscriminatorObject.\n *\n * @returns True if {@link obj} is a SchemaObject containing a non-string {@link discriminator} property.\n */\nexport function isDiscriminator(obj?: any): obj is SchemaObject & { discriminator: OpenAPIV3.DiscriminatorObject } {\n return !!obj && obj?.['discriminator'] && typeof obj.discriminator !== 'string'\n}\n\n/**\n * Determines whether a schema is required.\n *\n * Returns true if the schema has a non-empty {@link SchemaObject.required} array or a truthy {@link SchemaObject.required} property.\n *\n * @param schema - The schema object to check.\n * @returns True if the schema is required; otherwise, false.\n */\nexport function isRequired(schema?: SchemaObject): boolean {\n if (!schema) {\n return false\n }\n\n return Array.isArray(schema.required) ? !!schema.required?.length : !!schema.required\n}\n\nexport function isOptional(schema?: SchemaObject): boolean {\n return !isRequired(schema)\n}\n\nexport async function parse(pathOrApi: string | OASDocument, oasClass: typeof Oas = Oas): Promise<Oas> {\n if (typeof pathOrApi === 'string') {\n // resolve external refs\n const config = await loadConfig()\n const bundleResults = await bundle({ ref: pathOrApi, config, base: pathOrApi })\n\n return parse(bundleResults.bundle.parsed)\n }\n\n const oasNormalize = new OASNormalize(pathOrApi, {\n enablePaths: true,\n colorizeErrors: true,\n })\n const document = (await oasNormalize.load()) as OpenAPI.Document\n\n if (isOpenApiV2Document(document)) {\n const { openapi } = await swagger2openapi.convertObj(document, {\n anchors: true,\n })\n\n return new oasClass({ oas: openapi as OASDocument })\n }\n\n return new oasClass({ oas: document })\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/oas",
3
- "version": "3.10.10",
3
+ "version": "3.10.12",
4
4
  "description": "Oas helpers",
5
5
  "keywords": [
6
6
  "typescript",
@@ -55,7 +55,7 @@
55
55
  "oas": "^27.0.0",
56
56
  "oas-normalize": "^14.0.0",
57
57
  "openapi-types": "^12.1.3",
58
- "remeda": "^2.21.3",
58
+ "remeda": "^2.21.6",
59
59
  "swagger2openapi": "^7.0.8",
60
60
  "ts-toolbelt": "^9.6.0"
61
61
  },
@@ -63,10 +63,10 @@
63
63
  "@stoplight/yaml": "^4.3.0",
64
64
  "@types/swagger2openapi": "^7.0.4",
65
65
  "expect-type": "^1.2.1",
66
- "tsup": "^8.4.0",
66
+ "tsup": "^8.5.0",
67
67
  "typescript": "^5.8.3",
68
- "@kubb/config-ts": "3.10.10",
69
- "@kubb/config-tsup": "3.10.10"
68
+ "@kubb/config-ts": "3.10.12",
69
+ "@kubb/config-tsup": "3.10.12"
70
70
  },
71
71
  "engines": {
72
72
  "node": ">=20"
package/src/Oas.ts CHANGED
@@ -1,16 +1,13 @@
1
- import BaseOas from 'oas'
2
- import OASNormalize from 'oas-normalize'
3
- import { matchesMimeType } from 'oas/utils'
4
-
5
1
  import jsonpointer from 'jsonpointer'
6
-
7
- import { isDiscriminator, isReference } from './utils.ts'
8
-
2
+ import BaseOas from 'oas'
9
3
  import type { Operation } from 'oas/operation'
10
4
  import type { MediaTypeObject, OASDocument, ResponseObject, SchemaObject, User } from 'oas/types'
5
+ import { matchesMimeType } from 'oas/utils'
6
+ import OASNormalize from 'oas-normalize'
7
+ import type { OpenAPIV3 } from 'openapi-types'
11
8
  import type { OasTypes } from './index.ts'
12
9
  import type { contentType } from './types.ts'
13
- import type { OpenAPIV3 } from 'openapi-types'
10
+ import { isDiscriminator, isReference } from './utils.ts'
14
11
 
15
12
  type Options = {
16
13
  contentType?: contentType
@@ -71,15 +68,21 @@ export class Oas<const TOAS = unknown> extends BaseOas {
71
68
  return undefined
72
69
  }
73
70
 
74
- const mapping = schema.discriminator.mapping || {}
71
+ const { mapping = {}, propertyName } = schema.discriminator
75
72
 
76
73
  // loop over oneOf and add default mapping when none is defined
77
74
  if (schema.oneOf) {
78
75
  schema.oneOf.forEach((schema) => {
79
76
  if (isReference(schema)) {
80
77
  const key = this.getKey(schema.$ref)
81
-
82
- if (key && !Object.values(mapping).includes(schema.$ref)) {
78
+ const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)
79
+ // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669
80
+ const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject
81
+ const canAdd = key && !Object.values(mapping).includes(schema.$ref)
82
+
83
+ if (canAdd && propertySchema?.enum?.length === 1) {
84
+ mapping[propertySchema.enum[0]] = schema.$ref
85
+ } else if (canAdd) {
83
86
  mapping[key] = schema.$ref
84
87
  }
85
88
  }
@@ -90,8 +93,14 @@ export class Oas<const TOAS = unknown> extends BaseOas {
90
93
  schema.anyOf.forEach((schema) => {
91
94
  if (isReference(schema)) {
92
95
  const key = this.getKey(schema.$ref)
93
-
94
- if (key && !Object.values(mapping).includes(schema.$ref)) {
96
+ const refSchema: OpenAPIV3.SchemaObject = this.get(schema.$ref)
97
+ // special case where enum in the schema is set without mapping being defined, see https://github.com/kubb-labs/kubb/issues/1669
98
+ const propertySchema = refSchema.properties?.[propertyName] as OpenAPIV3.SchemaObject
99
+ const canAdd = key && !Object.values(mapping).includes(schema.$ref)
100
+
101
+ if (canAdd && propertySchema?.enum?.length === 1) {
102
+ mapping[propertySchema.enum[0]] = schema.$ref
103
+ } else if (canAdd) {
95
104
  mapping[key] = schema.$ref
96
105
  }
97
106
  }
@@ -150,7 +159,7 @@ export class Oas<const TOAS = unknown> extends BaseOas {
150
159
 
151
160
  // Since no media type was supplied we need to find either the first JSON-like media type that
152
161
  // we've got, or the first available of anything else if no JSON-like media types are present.
153
- let availablecontentType: string | undefined = undefined
162
+ let availablecontentType: string | undefined
154
163
  const contentTypes = Object.keys(responseBody.content)
155
164
  contentTypes.forEach((mt: string) => {
156
165
  if (!availablecontentType && matchesMimeType.json(mt)) {