openapi-ts-request 1.12.2 → 1.12.3

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.
@@ -56,7 +56,16 @@ function resolveEnumObject(params) {
56
56
  const enumArray = schemaObject.enum;
57
57
  let enumStr = '';
58
58
  let enumLabelTypeStr = '';
59
- if (config_2.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumber)(enumArray)) {
59
+ // 获取实际的类型(处理 OpenAPI 3.1 type 数组情况)
60
+ const getActualType = (type) => {
61
+ if (Array.isArray(type)) {
62
+ // 如果是数组,返回第一个非 null 类型
63
+ return type.find((t) => t !== 'null') || 'string';
64
+ }
65
+ return type;
66
+ };
67
+ const actualType = getActualType(schemaObject.type);
68
+ if (config_2.numberEnum.includes(actualType) || (0, util_1.isAllNumber)(enumArray)) {
60
69
  if (config.isSupportParseEnumDesc && schemaObject.description) {
61
70
  const enumMap = (0, util_1.parseDescriptionEnum)(schemaObject.description);
62
71
  enumStr = `{${(0, lodash_1.map)(enumArray, (value) => {
@@ -95,7 +104,7 @@ function resolveEnumObject(params) {
95
104
  }).join(',')}}`;
96
105
  }
97
106
  else {
98
- if (config_2.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumber)(enumArray)) {
107
+ if (config_2.numberEnum.includes(actualType) || (0, util_1.isAllNumber)(enumArray)) {
99
108
  if ((config.isSupportParseEnumDesc || config.supportParseEnumDescByReg) &&
100
109
  schemaObject.description) {
101
110
  const enumMap = config.isSupportParseEnumDesc
@@ -225,7 +234,14 @@ function resolveRefObject(params) {
225
234
  }
226
235
  else {
227
236
  const schemaObj = schema;
228
- resolvedType = schemaObj.type;
237
+ // 处理 OpenAPI 3.1 的 type 数组情况
238
+ if (Array.isArray(schemaObj.type)) {
239
+ // 如果是数组,使用第一个非 null 类型
240
+ resolvedType = schemaObj.type.find((t) => t !== 'null') || 'string';
241
+ }
242
+ else {
243
+ resolvedType = schemaObj.type;
244
+ }
229
245
  }
230
246
  const finalSchema = schema;
231
247
  return Object.assign(Object.assign({}, (resolvedSchema || {})), { type: resolvedType || finalSchema.type });
@@ -104,6 +104,16 @@ function getDefaultType(schemaObject, namespace = '', schemas) {
104
104
  let type = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.type;
105
105
  const dateEnum = ['Date', 'date', 'dateTime', 'date-time', 'datetime'];
106
106
  const stringEnum = ['string', 'email', 'password', 'url', 'byte', 'binary'];
107
+ // OpenAPI 3.1 支持 type 为数组,例如 ["string", "null"]
108
+ if (Array.isArray(type)) {
109
+ return type
110
+ .map((t) => {
111
+ // 为数组中的每个类型创建一个临时的 schemaObject
112
+ const tempSchema = Object.assign(Object.assign({}, schemaObject), { type: t });
113
+ return getDefaultType(tempSchema, namespace, schemas);
114
+ })
115
+ .join(' | ');
116
+ }
107
117
  if (type === 'null') {
108
118
  return 'null';
109
119
  }
@@ -86,10 +86,14 @@ function getDateByName(name, parentsKey) {
86
86
  function primitive(schemaParams, propsName) {
87
87
  const schema = (0, util_2.objectify)(schemaParams);
88
88
  const { type, format } = schema;
89
- const value = primitives_1.primitives[`${type}_${format || getDateByName(propsName)}`] ||
90
- primitives_1.primitives[type];
89
+ // 处理 OpenAPI 3.1 type 数组情况
90
+ const actualType = Array.isArray(type)
91
+ ? type.find((t) => t !== 'null') || 'string'
92
+ : type;
93
+ const value = primitives_1.primitives[`${actualType}_${format || getDateByName(propsName)}`] ||
94
+ primitives_1.primitives[actualType];
91
95
  if ((0, lodash_1.isUndefined)(schema.example)) {
92
- return value || `Unknown Type: ${schema.type}`;
96
+ return value || `Unknown Type: ${actualType}`;
93
97
  }
94
98
  return schema.example;
95
99
  }
@@ -3,7 +3,7 @@ import type { OpenAPIObject, ParameterObject, ReferenceObject, SchemaObject } fr
3
3
  export declare function objectify<T>(thing: T): T;
4
4
  export declare function get(openAPI: OpenAPIObject, path: string): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject;
5
5
  export declare function normalizeArray(arr: string[] | string): string[];
6
- export declare function inferSchema(thing: ParameterObject | SchemaObject | ReferenceObject | OpenAPIV3.MediaTypeObject): OpenAPIV3.ReferenceObject | OpenAPIV3.MediaTypeObject | {
6
+ export declare function inferSchema(thing: ParameterObject | SchemaObject | ReferenceObject | OpenAPIV3.MediaTypeObject): import("../type").NonArraySchemaObject | OpenAPIV3.ReferenceObject | OpenAPIV3.MediaTypeObject | {
7
7
  type: string;
8
8
  enum?: any[];
9
9
  title?: string;
package/dist/type.d.ts CHANGED
@@ -16,7 +16,7 @@ export type MutuallyExclusiveWithFallback<T, N> = {
16
16
  }[keyof T];
17
17
  type Modify<T, R> = Omit<T, keyof R> & R;
18
18
  type ICustomBaseSchemaObject = {
19
- type: ISchemaObjectType;
19
+ type: ISchemaObjectType | ISchemaObjectType[];
20
20
  format?: ISchemaObjectFormat;
21
21
  additionalProperties?: boolean | ISchemaObject;
22
22
  properties?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "1.12.2",
3
+ "version": "1.12.3",
4
4
  "description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",