Skip to content

Commit 13a3ceb

Browse files
committed
feat: enhance example generation for OpenAPI compatibility with array type handling
1 parent 0d5c2c6 commit 13a3ceb

File tree

4 files changed

+55
-25
lines changed

4 files changed

+55
-25
lines changed

src/core/demo-dist/bundle.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,13 +4546,19 @@ function generateValidExampleFromSchema(
45464546
if (resolvedSchema.example !== undefined) {
45474547
return resolvedSchema.example;
45484548
}
4549-
45504549
// Use default value if available
45514550
if (resolvedSchema.default !== undefined) {
45524551
return resolvedSchema.default;
45534552
}
45544553

4555-
if (resolvedSchema.type === "object" && resolvedSchema.properties) {
4554+
// Handle type as array (OpenAPI 3.1 JSON Schema compatibility)
4555+
let schemaType = resolvedSchema.type;
4556+
if (Array.isArray(resolvedSchema.type) && resolvedSchema.type.length > 0) {
4557+
// Use first non-null type
4558+
schemaType = resolvedSchema.type.find(type => type !== "null") || resolvedSchema.type[0];
4559+
}
4560+
4561+
if (schemaType === "object" && resolvedSchema.properties) {
45564562
const example = {};
45574563
const required = resolvedSchema.required || [];
45584564

@@ -4586,10 +4592,8 @@ function generateValidExampleFromSchema(
45864592
example[propName] = value;
45874593
}
45884594
}
4589-
}
4590-
4591-
return example;
4592-
} else if (resolvedSchema.type === "array" && resolvedSchema.items) {
4595+
} return example;
4596+
} else if (schemaType === "array" && resolvedSchema.items) {
45934597
if (indent < 5) {
45944598
const itemExample = generateValidExampleFromSchema(
45954599
resolvedSchema.items,
@@ -4617,13 +4621,19 @@ function generateValidPrimitiveExample(schema) {
46174621

46184622
// Use default value if available
46194623
if (schema.default !== undefined) return schema.default;
4620-
46214624
// Handle enums first
46224625
if (schema.enum && schema.enum.length > 0) {
46234626
return schema.enum[0];
46244627
}
46254628

4626-
switch (schema.type) {
4629+
// Handle type as array (OpenAPI 3.1 JSON Schema compatibility)
4630+
let schemaType = schema.type;
4631+
if (Array.isArray(schema.type) && schema.type.length > 0) {
4632+
// Use first non-null type
4633+
schemaType = schema.type.find(type => type !== "null") || schema.type[0];
4634+
}
4635+
4636+
switch (schemaType) {
46274637
case "string":
46284638
return generateValidStringExample(schema);
46294639
case "integer":

src/core/demo-dist/demo-info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
"description": "A comprehensive test API demonstrating all supported OpenAPI security schemes"
2323
}
2424
],
25-
"buildDate": "2025-06-11T14:04:43.171Z"
25+
"buildDate": "2025-06-24T16:37:09.045Z"
2626
}

src/core/dist/bundle.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,13 +4546,19 @@ function generateValidExampleFromSchema(
45464546
if (resolvedSchema.example !== undefined) {
45474547
return resolvedSchema.example;
45484548
}
4549-
45504549
// Use default value if available
45514550
if (resolvedSchema.default !== undefined) {
45524551
return resolvedSchema.default;
45534552
}
45544553

4555-
if (resolvedSchema.type === "object" && resolvedSchema.properties) {
4554+
// Handle type as array (OpenAPI 3.1 JSON Schema compatibility)
4555+
let schemaType = resolvedSchema.type;
4556+
if (Array.isArray(resolvedSchema.type) && resolvedSchema.type.length > 0) {
4557+
// Use first non-null type
4558+
schemaType = resolvedSchema.type.find(type => type !== "null") || resolvedSchema.type[0];
4559+
}
4560+
4561+
if (schemaType === "object" && resolvedSchema.properties) {
45564562
const example = {};
45574563
const required = resolvedSchema.required || [];
45584564

@@ -4586,10 +4592,8 @@ function generateValidExampleFromSchema(
45864592
example[propName] = value;
45874593
}
45884594
}
4589-
}
4590-
4591-
return example;
4592-
} else if (resolvedSchema.type === "array" && resolvedSchema.items) {
4595+
} return example;
4596+
} else if (schemaType === "array" && resolvedSchema.items) {
45934597
if (indent < 5) {
45944598
const itemExample = generateValidExampleFromSchema(
45954599
resolvedSchema.items,
@@ -4617,13 +4621,19 @@ function generateValidPrimitiveExample(schema) {
46174621

46184622
// Use default value if available
46194623
if (schema.default !== undefined) return schema.default;
4620-
46214624
// Handle enums first
46224625
if (schema.enum && schema.enum.length > 0) {
46234626
return schema.enum[0];
46244627
}
46254628

4626-
switch (schema.type) {
4629+
// Handle type as array (OpenAPI 3.1 JSON Schema compatibility)
4630+
let schemaType = schema.type;
4631+
if (Array.isArray(schema.type) && schema.type.length > 0) {
4632+
// Use first non-null type
4633+
schemaType = schema.type.find(type => type !== "null") || schema.type[0];
4634+
}
4635+
4636+
switch (schemaType) {
46274637
case "string":
46284638
return generateValidStringExample(schema);
46294639
case "integer":

src/core/js/exampleGenerator.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ function generateValidExampleFromSchema(
3131
if (resolvedSchema.example !== undefined) {
3232
return resolvedSchema.example;
3333
}
34-
3534
// Use default value if available
3635
if (resolvedSchema.default !== undefined) {
3736
return resolvedSchema.default;
3837
}
3938

40-
if (resolvedSchema.type === "object" && resolvedSchema.properties) {
39+
// Handle type as array (OpenAPI 3.1 JSON Schema compatibility)
40+
let schemaType = resolvedSchema.type;
41+
if (Array.isArray(resolvedSchema.type) && resolvedSchema.type.length > 0) {
42+
// Use first non-null type
43+
schemaType = resolvedSchema.type.find(type => type !== "null") || resolvedSchema.type[0];
44+
}
45+
46+
if (schemaType === "object" && resolvedSchema.properties) {
4147
const example = {};
4248
const required = resolvedSchema.required || [];
4349

@@ -71,10 +77,8 @@ function generateValidExampleFromSchema(
7177
example[propName] = value;
7278
}
7379
}
74-
}
75-
76-
return example;
77-
} else if (resolvedSchema.type === "array" && resolvedSchema.items) {
80+
} return example;
81+
} else if (schemaType === "array" && resolvedSchema.items) {
7882
if (indent < 5) {
7983
const itemExample = generateValidExampleFromSchema(
8084
resolvedSchema.items,
@@ -102,13 +106,19 @@ function generateValidPrimitiveExample(schema) {
102106

103107
// Use default value if available
104108
if (schema.default !== undefined) return schema.default;
105-
106109
// Handle enums first
107110
if (schema.enum && schema.enum.length > 0) {
108111
return schema.enum[0];
109112
}
110113

111-
switch (schema.type) {
114+
// Handle type as array (OpenAPI 3.1 JSON Schema compatibility)
115+
let schemaType = schema.type;
116+
if (Array.isArray(schema.type) && schema.type.length > 0) {
117+
// Use first non-null type
118+
schemaType = schema.type.find(type => type !== "null") || schema.type[0];
119+
}
120+
121+
switch (schemaType) {
112122
case "string":
113123
return generateValidStringExample(schema);
114124
case "integer":

0 commit comments

Comments
 (0)