-
Notifications
You must be signed in to change notification settings - Fork 15
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
fix: Sort the macrofied schemas based on schema refs #90
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ const { isObject, isArray, propEq, pathSatisfies, propSatisfies } = predicates | |
|
||
import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, hasPublicAPIs } from '../shared/modules.mjs' | ||
import isEmpty from 'crocks/core/isEmpty.js' | ||
import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies } from '../shared/json-schema.mjs' | ||
import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema } from '../shared/json-schema.mjs' | ||
|
||
// util for visually debugging crocks ADTs | ||
const _inspector = obj => { | ||
|
@@ -564,6 +564,8 @@ function generateDefaults(json = {}, templates) { | |
return reducer(json) | ||
} | ||
|
||
const isEnum = x => x.type && x.type === 'string' && Array.isArray(x.enum) && x.title | ||
|
||
function generateSchemas(json, templates, options) { | ||
let results = [] | ||
|
||
|
@@ -609,8 +611,6 @@ function generateSchemas(json, templates, options) { | |
content = content.replace(/.*\$\{schema.seeAlso\}/, '') | ||
} | ||
|
||
const isEnum = x => x.type === 'string' && Array.isArray(x.enum) && x.title | ||
|
||
const result = uri ? { | ||
uri: uri, | ||
name: schema.title || name, | ||
|
@@ -625,21 +625,36 @@ function generateSchemas(json, templates, options) { | |
results.push(result) | ||
} | ||
|
||
const list = [] | ||
|
||
// schemas may be 1 or 2 levels deeps | ||
Object.entries(schemas).forEach( ([name, schema]) => { | ||
if (isSchema(schema)) { | ||
generate(name, schema) | ||
list.push([name, schema]) | ||
} | ||
else if (typeof schema === 'object') { | ||
const uri = schema.uri | ||
Object.entries(schema).forEach( ([name, schema]) => { | ||
if (name !== 'uri') { | ||
generate(name, schema, uri) | ||
list.push([name, schema, uri]) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
list.sort((a, b) => { | ||
const aInB = isDefinitionReferencedBySchema('#/components/schemas/' + a[0], b[1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to check for the presence of If it's present, then the path to the schema is I recommend removing the else above, to keep this code simpler. |
||
const bInA = isDefinitionReferencedBySchema('#/components/schemas/' + b[0], a[1]) | ||
if(isEnum(a[1]) || (aInB && !bInA)) { | ||
return -1 | ||
} else if(isEnum(b[1]) || (!aInB && bInA)) { | ||
return 1 | ||
} | ||
return 0; | ||
}) | ||
|
||
list.forEach(item => generate(...item)) | ||
|
||
return results | ||
} | ||
|
||
|
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.
Not directly related to your change, but I think we can get rid of this entire
else
block. It's for external schemas, that won't get output by the current module. Your code below doesn't account for these, but we actually filter them out immediately up at the top of the code after calling this methodProbably makes sense to leave your sorter method the way it is, and remove this else block.