-
Notifications
You must be signed in to change notification settings - Fork 2
feat: implement numeric exclusive bounds handling for JSON schema draft 07 and above #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a5c9fe
feat: implement numeric exclusive bounds handling for OpenAPI 3.1
b41ex 5e67acd
refactor: add compatibility suite tests for exclusiveMinimum/exclusiv…
b41ex 0a4206b
refactor: improved naming for exclusiveBooleanClassifier
b41ex 88108f2
refactor: code formatting
b41ex 2f12893
feat: classify changes to number validation keywords that result in a…
b41ex 45878cc
refactor: rename file
b41ex ede2ad9
refactor: remove excessive check
b41ex 44f96ce
refactor: use constants for keyword names
b41ex d62fffe
chore: merge branch 'develop' into feature/exclusive-min-max
b41ex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,214 @@ | ||
| import { | ||
| cleanOrigins, | ||
| copyOrigins, | ||
| JSON_SCHEMA_PROPERTY_MINIMUM, | ||
| JSON_SCHEMA_PROPERTY_MAXIMUM, | ||
| JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM, | ||
| JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM, | ||
| JsonSchemaNumericValidationKeywordsType, | ||
| } from '@netcracker/qubership-apihub-api-unifier' | ||
| import { isNumber, isObject } from '../utils' | ||
| import { AdapterResolver, ClassifyRule } from '../types' | ||
| import { CompareContext, DiffType } from '../types' | ||
| import { breaking, nonBreaking, risky } from '../core' | ||
|
|
||
| const hasBooleanExclusiveBounds = (value: Record<PropertyKey, unknown>): boolean => | ||
| typeof value[JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM] === 'boolean' || | ||
| typeof value[JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM] === 'boolean' | ||
|
|
||
| const adaptBooleanExclusiveBound = ( | ||
| value: Record<PropertyKey, unknown>, | ||
| boundaryKey: typeof JSON_SCHEMA_PROPERTY_MINIMUM | typeof JSON_SCHEMA_PROPERTY_MAXIMUM, | ||
| exclusiveKey: typeof JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM | typeof JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM, | ||
|
makeev-pavel marked this conversation as resolved.
|
||
| originsFlag: symbol, | ||
| ): void => { | ||
| const exclusive = value[exclusiveKey] | ||
| if (typeof exclusive !== 'boolean') { | ||
| return | ||
| } | ||
|
|
||
| const boundary = value[boundaryKey] | ||
|
JayLim2 marked this conversation as resolved.
|
||
| if (exclusive && isNumber(boundary)) { | ||
| value[exclusiveKey] = boundary | ||
| copyOrigins(value, value, boundaryKey, exclusiveKey, originsFlag) | ||
| delete value[boundaryKey] | ||
| cleanOrigins(value, boundaryKey, originsFlag) | ||
| return | ||
| } | ||
|
|
||
| delete value[exclusiveKey] | ||
| cleanOrigins(value, exclusiveKey, originsFlag) | ||
| } | ||
|
|
||
| export const booleanExclusiveBoundsOas30to31Adapter: AdapterResolver = (value, _reference, valueContext) => { | ||
| if (!isObject(value) || !hasBooleanExclusiveBounds(value)) { | ||
| return value | ||
| } | ||
|
|
||
| const { originsFlag } = valueContext.options | ||
|
|
||
| return valueContext.transformer(value, 'boolean-exclusive-bounds-to-numeric', (current) => { | ||
| if (!isObject(current)) { | ||
| return current | ||
| } | ||
|
|
||
| const result = { ...current } | ||
| adaptBooleanExclusiveBound(result, JSON_SCHEMA_PROPERTY_MINIMUM, JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM, originsFlag) | ||
| adaptBooleanExclusiveBound(result, JSON_SCHEMA_PROPERTY_MAXIMUM, JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM, originsFlag) | ||
| return result | ||
| }) | ||
| } | ||
|
|
||
| type EffectiveBound = { | ||
| propertyName: JsonSchemaNumericValidationKeywordsType | ||
| value: number | ||
| exclusive: boolean | ||
| } | ||
|
|
||
| const getNumericProperty = (schema: unknown, property: string): number | undefined => { | ||
| if (!isObject(schema)) { | ||
| return undefined | ||
| } | ||
| const value = schema[property] | ||
| return isNumber(value) ? value : undefined | ||
| } | ||
|
|
||
| const getEffectiveLowerBound = (schema: unknown): EffectiveBound | undefined => { | ||
| const minimumValue = getNumericProperty(schema, JSON_SCHEMA_PROPERTY_MINIMUM) | ||
| const exclusiveMinimumValue = getNumericProperty(schema, JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM) | ||
|
|
||
| if (minimumValue === undefined && exclusiveMinimumValue === undefined) { | ||
| return undefined | ||
| } | ||
| if (minimumValue === undefined) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM, | ||
| value: exclusiveMinimumValue!, | ||
| exclusive: true, | ||
| } | ||
| } | ||
| if (exclusiveMinimumValue === undefined) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_MINIMUM, | ||
| value: minimumValue, | ||
| exclusive: false, | ||
| } | ||
| } | ||
|
|
||
| if (exclusiveMinimumValue > minimumValue) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_EXCLUSIVE_MINIMUM, | ||
| value: exclusiveMinimumValue, | ||
| exclusive: true, | ||
| } | ||
| } | ||
| if (exclusiveMinimumValue < minimumValue) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_MINIMUM, | ||
| value: minimumValue, | ||
| exclusive: false, | ||
| } | ||
| } | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_MINIMUM, | ||
| value: minimumValue, | ||
| exclusive: true, | ||
| } | ||
| } | ||
|
JayLim2 marked this conversation as resolved.
|
||
|
|
||
| const getEffectiveUpperBound = (schema: unknown): EffectiveBound | undefined => { | ||
| const maximumValue = getNumericProperty(schema, JSON_SCHEMA_PROPERTY_MAXIMUM) | ||
| const exclusiveMaximumValue = getNumericProperty(schema, JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM) | ||
|
|
||
| if (maximumValue === undefined && exclusiveMaximumValue === undefined) { | ||
| return undefined | ||
| } | ||
| if (maximumValue === undefined) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM, | ||
| value: exclusiveMaximumValue!, | ||
| exclusive: true, | ||
| } | ||
| } | ||
| if (exclusiveMaximumValue === undefined) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_MAXIMUM, | ||
| value: maximumValue, | ||
| exclusive: false, | ||
| } | ||
| } | ||
|
|
||
| if (exclusiveMaximumValue < maximumValue) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_EXCLUSIVE_MAXIMUM, | ||
| value: exclusiveMaximumValue, | ||
| exclusive: true, | ||
| } | ||
| } | ||
| if (exclusiveMaximumValue > maximumValue) { | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_MAXIMUM, | ||
| value: maximumValue, | ||
| exclusive: false, | ||
| } | ||
| } | ||
| return { | ||
| propertyName: JSON_SCHEMA_PROPERTY_MAXIMUM, | ||
| value: maximumValue, | ||
| exclusive: true, | ||
| } | ||
| } | ||
|
JayLim2 marked this conversation as resolved.
|
||
|
|
||
| const classifyByEffectiveBound = ( | ||
| ctx: CompareContext, | ||
| getEffectiveBound: (schema: unknown) => EffectiveBound | undefined, | ||
| isAfterStricter: (before: EffectiveBound, after: EffectiveBound) => boolean, | ||
| stricterType: DiffType, | ||
| looserType: DiffType, | ||
| ): DiffType => { | ||
| const beforeBound = getEffectiveBound(ctx.before.parent) | ||
| const afterBound = getEffectiveBound(ctx.after.parent) | ||
|
|
||
| if (!beforeBound) { | ||
| return stricterType | ||
| } | ||
| if (!afterBound) { | ||
| return looserType | ||
| } | ||
| if (beforeBound.value === afterBound.value && beforeBound.exclusive === afterBound.exclusive) { | ||
| return nonBreaking | ||
| } | ||
|
|
||
| return isAfterStricter(beforeBound, afterBound) ? stricterType : looserType | ||
| } | ||
|
|
||
| const isAfterLowerStricter = (before: EffectiveBound, after: EffectiveBound) => | ||
| after.value > before.value || (after.value === before.value && after.exclusive && !before.exclusive) | ||
|
|
||
| const isAfterUpperStricter = (before: EffectiveBound, after: EffectiveBound) => | ||
| after.value < before.value || (after.value === before.value && after.exclusive && !before.exclusive) | ||
|
|
||
| const createPropertyBoundClassifier = ( | ||
| propertyName: JsonSchemaNumericValidationKeywordsType, | ||
| getEffectiveBound: (schema: unknown) => EffectiveBound | undefined, | ||
| isAfterStricter: (before: EffectiveBound, after: EffectiveBound) => boolean, | ||
| stricterType: DiffType, | ||
| looserType: DiffType, | ||
| ) => (ctx: CompareContext): DiffType => { | ||
| const definingBound = getEffectiveBound(ctx.after.parent) ?? getEffectiveBound(ctx.before.parent) | ||
| if (!definingBound || definingBound.propertyName !== propertyName) return nonBreaking | ||
| return classifyByEffectiveBound(ctx, getEffectiveBound, isAfterStricter, stricterType, looserType) | ||
| } | ||
|
|
||
| export const createEffectiveLowerBoundClassifier = (propertyName: JsonSchemaNumericValidationKeywordsType): ClassifyRule => { | ||
| const requestClassifier = createPropertyBoundClassifier(propertyName, getEffectiveLowerBound, isAfterLowerStricter, breaking, nonBreaking) | ||
| const responseClassifier = createPropertyBoundClassifier(propertyName, getEffectiveLowerBound, isAfterLowerStricter, nonBreaking, risky) | ||
| return [requestClassifier, requestClassifier, requestClassifier, responseClassifier, responseClassifier, responseClassifier] | ||
| } | ||
|
|
||
| export const createEffectiveUpperBoundClassifier = (propertyName: JsonSchemaNumericValidationKeywordsType): ClassifyRule => { | ||
| const requestClassifier = createPropertyBoundClassifier(propertyName, getEffectiveUpperBound, isAfterUpperStricter, breaking, nonBreaking) | ||
| const responseClassifier = createPropertyBoundClassifier(propertyName, getEffectiveUpperBound, isAfterUpperStricter, nonBreaking, risky) | ||
| return [requestClassifier, requestClassifier, requestClassifier, responseClassifier, responseClassifier, responseClassifier] | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.