-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generation fails when schemas have an $id field or array of obje…
…cts (#287) * fix: is now being handled by changing the value to the classname where appropriate as part of the preprocess hook; items arrays are also working with the changes; updated tests * chore: renamed new custom attribute to be more generic; fixed linting issues; const javaPackage instead of let
- Loading branch information
1 parent
db808d4
commit 95fccd4
Showing
8 changed files
with
2,070 additions
and
282 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
test/temp | ||
coverage | ||
coverage | ||
**/.DS_Store | ||
**/.vscode |
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 |
---|---|---|
@@ -1,15 +1,48 @@ | ||
const ApplicationModel = require('../lib/applicationModel.js'); | ||
const _ = require('lodash'); | ||
|
||
module.exports = { | ||
'generate:before': generator => { | ||
generator.asyncapi.allSchemas().forEach((schema, schemaName) => { | ||
// The generator will create file names based on the schema's $id. Instead of guessing what the generator named the file so we can fix it in post, | ||
// ... it's easier to process $id here first. Since we don't use it, removing it is easiest. | ||
if (schema.$id()) { | ||
delete schema._json.$id; | ||
function setSchemaIdsForFileName(asyncapi) { | ||
asyncapi.allSchemas().forEach((schema, schemaName) => { | ||
// If we leave the $id the way it is, the generator will name the schema files what their $id is, which is always a bad idea. | ||
// So we leave it in, but $id is going to be changed to be the class name we want. | ||
// If we remove the $id and there's no x-parser-schema-id, then it wont be returned by allSchemas(). | ||
if (schema.$id()) { | ||
// Assuming one of x-parser-schema-id and $id must be present. | ||
let classNameForGenerator; | ||
const parserSchemaId = schema.ext('x-parser-schema-id'); | ||
classNameForGenerator = parserSchemaId ? parserSchemaId : _.camelCase(schema.$id().substring(schema.$id().lastIndexOf('/') + 1)); | ||
|
||
if (classNameForGenerator === 'items') { | ||
const parentSchema = schema.options?.parent; | ||
const parentSchemaItems = parentSchema?.items(); | ||
if (parentSchemaItems?._json?.$id === schema.$id()) { | ||
const parentParserSchemaId = parentSchema.ext('x-parser-schema-id'); | ||
classNameForGenerator = parentParserSchemaId ? parentParserSchemaId : _.camelCase(parentSchema.$id().substring(parentSchema.$id().lastIndexOf('/') + 1)); | ||
// If we come across this schema later in the code generator, we'll know to rename it to its parent because the proper settings will be set in the model class. | ||
schema._json['x-model-class-name'] = classNameForGenerator; | ||
classNameForGenerator += 'Items'; | ||
} | ||
} | ||
}); | ||
schema._json.$id = classNameForGenerator; | ||
} | ||
}); | ||
} | ||
|
||
function setSchemaIdsForFileNameIncludingDuplicates(asyncapi) { | ||
// We do this multiple times because allSchemas() returns a list of deduplicated schemas, so if we change the $id of a schema, | ||
// we wont change any of the duplicates. We continue until there are no more duplicates to change. | ||
let numSchemas; | ||
let newNumSchemas; | ||
do { | ||
numSchemas = asyncapi.allSchemas().size; | ||
setSchemaIdsForFileName(asyncapi); | ||
newNumSchemas = asyncapi.allSchemas().size; | ||
} while (numSchemas !== newNumSchemas); | ||
} | ||
|
||
module.exports = { | ||
'generate:before': generator => { | ||
setSchemaIdsForFileNameIncludingDuplicates(generator.asyncapi); | ||
ApplicationModel.asyncapi = generator.asyncapi; | ||
} | ||
}; |
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.