mirror of
https://github.com/LukeHagar/openapi-definition-generator.git
synced 2025-12-06 04:20:23 +00:00
Allow oneOf to be an option rather than default
This commit is contained in:
@@ -47,6 +47,11 @@ export type Config = {
|
||||
* @description The type to use for null values
|
||||
*/
|
||||
nullType: 'number' | 'string' | 'integer' | 'boolean';
|
||||
|
||||
/**
|
||||
* @description Whether to allow oneOf for array items schema
|
||||
*/
|
||||
allowOneOf: boolean;
|
||||
};
|
||||
|
||||
export type StringType = {
|
||||
@@ -125,28 +130,46 @@ export function convertArray(array: unknown[], config: Config) {
|
||||
const output: ArrayType = { type: 'array' };
|
||||
const items: Output[] = [];
|
||||
const exampleArray = [];
|
||||
|
||||
let schema = {};
|
||||
for (const entry of array) {
|
||||
const map = convertObject(entry, config);
|
||||
|
||||
if (
|
||||
!items.some(
|
||||
(item) =>
|
||||
(item.type === map.type && item.format === map.format && item.type !== 'object') ||
|
||||
(item.type === 'object' &&
|
||||
JSON.stringify(Object.keys(item.properties).sort()) ===
|
||||
JSON.stringify(Object.keys(map.properties).sort()))
|
||||
)
|
||||
) {
|
||||
items.push(map);
|
||||
exampleArray.push(entry);
|
||||
if (config.allowOneOf) {
|
||||
const map = convertObject(entry, config);
|
||||
if (
|
||||
!items.some(
|
||||
(item) =>
|
||||
(item.type === map.type && item.format === map.format && item.type !== 'object') ||
|
||||
(item.type === 'object' &&
|
||||
JSON.stringify(Object.keys(item.properties).sort()) ===
|
||||
JSON.stringify(Object.keys(map.properties).sort()))
|
||||
)
|
||||
) {
|
||||
items.push(map);
|
||||
exampleArray.push(entry);
|
||||
}
|
||||
} else {
|
||||
items.push(entry);
|
||||
if (typeof entry === 'object' && !Array.isArray(entry)) {
|
||||
for (const prop in entry) {
|
||||
schema[prop] = entry[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (items.length > 1) {
|
||||
output.items = { oneOf: [...items] };
|
||||
if (config.allowOneOf) {
|
||||
if (items.length > 1) {
|
||||
output.items = { oneOf: [...items] };
|
||||
} else {
|
||||
output.items = items[0];
|
||||
}
|
||||
} else {
|
||||
output.items = items[0];
|
||||
if (Object.keys(schema).length > 0) {
|
||||
exampleArray.push(schema);
|
||||
output.items = convertObject(schema, config);
|
||||
} else {
|
||||
exampleArray.push(items[0]);
|
||||
output.items = convertObject(items[0], config);
|
||||
}
|
||||
}
|
||||
|
||||
if (config.includeExamples) output.example = exampleArray;
|
||||
|
||||
@@ -5,7 +5,8 @@ import { localStorageStore } from '@skeletonlabs/skeleton';
|
||||
const localConfig: Config = {
|
||||
allowIntegers: true,
|
||||
includeExamples: true,
|
||||
nullType: 'string'
|
||||
nullType: 'string',
|
||||
allowOneOf: false
|
||||
};
|
||||
|
||||
export const config: Writable<Config> = localStorageStore('config', localConfig);
|
||||
|
||||
@@ -52,6 +52,15 @@
|
||||
/>
|
||||
<p>Allow integer types</p>
|
||||
</label>
|
||||
<label class="flex items-center space-x-2 text-sm">
|
||||
<input
|
||||
bind:checked={$config.allowOneOf}
|
||||
class="checkbox"
|
||||
type="checkbox"
|
||||
id="allowOneOf"
|
||||
/>
|
||||
<p>Allow array oneOf</p>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -48,8 +48,8 @@
|
||||
$: run(content, $config, $yamlOut);
|
||||
</script>
|
||||
|
||||
<div class="flex flex-row flex-wrap justify-between px-2 gap-2 overflow-hidden">
|
||||
<div class="grow">
|
||||
<div class="flex flex-row justify-between px-2 gap-2 overflow-hidden">
|
||||
<div class="grow max-w-[50%]">
|
||||
<p class="text-center py-2">
|
||||
Input all of your JSON formatted Data, Typically API response bodies
|
||||
</p>
|
||||
@@ -57,7 +57,7 @@
|
||||
<JSONEditor onChange={() => run(content, $config, $yamlOut)} bind:content />
|
||||
</div>
|
||||
</div>
|
||||
<div class="grow">
|
||||
<div class="grow max-w-[50%]">
|
||||
<p class="text-center py-2">
|
||||
And here is that JSON Response formatted as a {$yamlOut === true ? 'YAML' : 'JSON'} OpenAPI Specification
|
||||
</p>
|
||||
|
||||
Reference in New Issue
Block a user