-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(samples): add support for proper schema merging
This change is specific to JSON Schema 2020-12 and OpenAPI 3.1.0. Refs #8577
- Loading branch information
Showing
3 changed files
with
87 additions
and
118 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/core/plugins/json-schema-2020-12/samples-extensions/fn/core/merge.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import { isBooleanJSONSchema, isJSONSchema } from "./predicates" | ||
|
||
const merge = (target, source, config = {}) => { | ||
if (isBooleanJSONSchema(target) && target === true) return true | ||
if (isBooleanJSONSchema(target) && target === false) return false | ||
if (isBooleanJSONSchema(source) && source === true) return true | ||
if (isBooleanJSONSchema(source) && source === false) return false | ||
|
||
if (!isJSONSchema(target)) return source | ||
if (!isJSONSchema(source)) return target | ||
|
||
/** | ||
* Merging properties from the source object into the target object | ||
* only if they do not already exist in the target object. | ||
*/ | ||
const merged = { ...source, ...target } | ||
|
||
// merging required keyword | ||
if (Array.isArray(source.required) && Array.isArray(target.required)) { | ||
merged.required = [...new Set([...target.required, ...source.required])] | ||
} | ||
|
||
// merging properties keyword | ||
if (source.properties && target.properties) { | ||
const allPropertyNames = new Set([ | ||
...Object.keys(source.properties), | ||
...Object.keys(target.properties), | ||
]) | ||
|
||
merged.properties = {} | ||
for (const name of allPropertyNames) { | ||
const sourceProperty = source.properties[name] || {} | ||
const targetProperty = target.properties[name] || {} | ||
|
||
if ( | ||
(sourceProperty.readOnly && !config.includeReadOnly) || | ||
(sourceProperty.writeOnly && !config.includeWriteOnly) | ||
) { | ||
merged.required = (merged.required || []).filter((p) => p !== name) | ||
} else { | ||
merged.properties[name] = merge(targetProperty, sourceProperty, config) | ||
} | ||
} | ||
} | ||
|
||
// merging items keyword | ||
if (isJSONSchema(source.items) && isJSONSchema(target.items)) { | ||
merged.items = merge(target.items, source.items, config) | ||
} | ||
|
||
// merging contains keyword | ||
if (isJSONSchema(source.contains) && isJSONSchema(target.contains)) { | ||
merged.contains = merge(target.contains, source.contains, config) | ||
} | ||
|
||
// merging contentSchema keyword | ||
if ( | ||
isJSONSchema(source.contentSchema) && | ||
isJSONSchema(target.contentSchema) | ||
) { | ||
merged.contentSchema = merge( | ||
target.contentSchema, | ||
source.contentSchema, | ||
config | ||
) | ||
} | ||
|
||
return merged | ||
} | ||
|
||
export default merge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters