Skip to content

Commit

Permalink
fixes createDefaultValue doesn't follow anyOf, oneOf, allOf eclipseso…
Browse files Browse the repository at this point in the history
  • Loading branch information
kchobantonov committed Dec 1, 2024
1 parent fcef896 commit 7d1865f
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions packages/core/src/mappers/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,13 @@ export const showAsRequired = (
*/
export const createDefaultValue = (
schema: JsonSchema,
rootSchema: JsonSchema
) => {
const resolvedSchema = Resolve.schema(schema, schema.$ref, rootSchema);
rootSchema: JsonSchema,
defaultValue: any = {}
): any => {
const resolvedSchema =
typeof schema.$ref === 'string'
? Resolve.schema(rootSchema, schema.$ref, rootSchema)
: schema;
if (resolvedSchema.default !== undefined) {
return extractDefaults(resolvedSchema, rootSchema);
}
Expand All @@ -196,8 +200,44 @@ export const createDefaultValue = (
return extractDefaults(resolvedSchema, rootSchema);
} else if (hasType(resolvedSchema, 'null')) {
return null;
} else if (
schema.oneOf &&
Array.isArray(schema.oneOf) &&
schema.oneOf.length > 0
) {
for (const combineSchema of schema.oneOf) {
const result = createDefaultValue(combineSchema, rootSchema, undefined);
if (result !== undefined) {
// return the first one that have type information
return result;
}
}
} else if (
schema.anyOf &&
Array.isArray(schema.anyOf) &&
schema.anyOf.length > 0
) {
for (const combineSchema of schema.anyOf) {
const result = createDefaultValue(combineSchema, rootSchema, undefined);
if (result !== undefined) {
// return the first one that have type information
return result;
}
}
} else if (
schema.allOf &&
Array.isArray(schema.allOf) &&
schema.allOf.length > 0
) {
for (const combineSchema of schema.allOf) {
const result = createDefaultValue(combineSchema, rootSchema, undefined);
if (result !== undefined) {
// return the first one that have type information
return result;
}
}
} else {
return {};
return defaultValue;
}
};

Expand Down

0 comments on commit 7d1865f

Please sign in to comment.