-
Notifications
You must be signed in to change notification settings - Fork 91
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
refactor: work-in-progress proposed refactor of composed schema restrictions #506
Draft
hudlow
wants to merge
5
commits into
IBM:main
Choose a base branch
from
hudlow:composed-schema-redux
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
04da2d1
feat(composed-schema-restrictions): add new spectral-style rule
padamstx 8dbe0ff
fix: address review comments
padamstx 3adcba4
refactor: work-in-progress proposed refactor of composed schema restr…
hudlow 4309684
Merge remote-tracking branch 'upstream/main' into composed-schema-redux
hudlow 727bad8
refactor(schema-type): make schema-type much more sophisticated
hudlow 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
89 changes: 89 additions & 0 deletions
89
packages/ruleset/src/functions/composed-schema-restrictions.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,89 @@ | ||
const { | ||
validateSubschemas, | ||
getPropertySchemasByName, | ||
getSchemaType, | ||
isObjectSchema, | ||
isArraySchema, | ||
isEnumerationSchema, | ||
isObject, | ||
SchemaType | ||
} = require('../utils'); | ||
|
||
module.exports = function(schema, options, { path }) { | ||
return validateSubschemas(schema, path, composedSchemaRestrictions); | ||
}; | ||
|
||
/** | ||
* Checks to make sure properties across a composed object schema have types that are deemed | ||
* compatible for modeling that schema. | ||
* | ||
* @param {*} schema the schema to check | ||
* @param {*} path the array of path segments indicating the location of "schema" within the API definition | ||
* @returns an array containing the violations found or [] if no violations | ||
*/ | ||
function composedSchemaRestrictions(schema, path) { | ||
const errors = []; | ||
|
||
// Only object schemas have properties | ||
if (isObjectSchema(schema)) { | ||
// Collects all composed property schemas indexed by the name of the property they define | ||
const schemasByName = getPropertySchemasByName(schema); | ||
|
||
for (const propertyName in schemasByName) { | ||
// The reducer will result in a `false` sentinel if two schemas for the same property | ||
// are not deemed compatible | ||
if ( | ||
schemasByName[propertyName].reduce(schemaCompatibilityReducer) === false | ||
) { | ||
errors.push({ | ||
message: `SDK generation may fail due to incompatible types for property across composite object schema: ${propertyName}`, | ||
path | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
/** | ||
* Reducer for an array of schemas; for each pair of schemas returns one of them if they're deemed | ||
* compatible and a `false` sentinel otherwise. The `false` sentinel is guaranteed to propagate. | ||
* | ||
* @param {*} left the "left" schema in the comparison | ||
* @param {*} right the "right" schema in the comparison | ||
* @returns a schema for the next comparison if the two are compatible, `false` otherwise | ||
*/ | ||
function schemaCompatibilityReducer(left, right) { | ||
return getComparandum(left) === getComparandum(right) ? left : false; | ||
} | ||
|
||
/** | ||
* Returns the value appropriate to compare a schema to another schema. This is dependent on type. | ||
* - For indeterminate schemas, deem incomparable and return a unique value | ||
* - For object schemas, deem compatible only for an exact match and return the schema itself | ||
* - For array schemas, deem compatible if their items are compatible (recursive) | ||
* - For enumeration schemas, deem them compatible with strings | ||
* - For all other schemas, deem them compatible with schemas of the same derived type and return it | ||
* | ||
* @param {*} schema the schema from which to derive a "comparandum" to compare with other schemas | ||
* @returns a "comparandum" value to compare with other schemas | ||
*/ | ||
function getComparandum(schema) { | ||
if (!isObject(schema) || getSchemaType(schema) === SchemaType.UNKNOWN) { | ||
// not compatible with any other schema | ||
return Symbol(); | ||
} | ||
if (isObjectSchema(schema)) { | ||
// only compatible with same exact schema | ||
return schema; | ||
} else if (isArraySchema(schema)) { | ||
// compatible with other arrays whose values are compatible | ||
return getComparandum(schema.items); | ||
} else if (isEnumerationSchema(schema)) { | ||
// compatible with all strings | ||
return SchemaType.STRING; | ||
} else { | ||
return getSchemaType(schema); | ||
} | ||
} |
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
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
16 changes: 16 additions & 0 deletions
16
packages/ruleset/src/rules/composed-schema-restrictions.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,16 @@ | ||
const { oas3 } = require('@stoplight/spectral-formats'); | ||
const { composedSchemaRestrictions } = require('../functions'); | ||
const { schemas } = require('../collections'); | ||
|
||
module.exports = { | ||
description: | ||
'Composed schemas using oneOf or anyOf should comply with SDK generation best practices', | ||
message: '{{error}}', | ||
given: schemas, | ||
severity: 'warn', | ||
formats: [oas3], | ||
resolved: true, | ||
then: { | ||
function: composedSchemaRestrictions | ||
} | ||
}; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be easier to read (for me, at least) if this comment elaborated slightly more on the data structure returned by
getPropertySchemasByName
. Something to let the reader know the map values are lists of schemas all defining properties of common names.