Skip to content
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 discriminator entries by mapping order #1216

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class SchemaModel {

const inversedMapping = { ...implicitInversedMapping, ...explicitInversedMapping };

const refs: Array<{ $ref; name }> = [];
let refs: Array<{ $ref; name }> = [];

for (const $ref of Object.keys(inversedMapping)) {
const names = inversedMapping[$ref];
Expand All @@ -267,6 +267,35 @@ export class SchemaModel {
}
}

// Make the listing respects the mapping
// in case a mapping is defined, the user usually wants to have the order shown
// as it was defined in the yaml. This will sort the names given the provided
// mapping (if provided).
// The logic is:
// - If a name is among the mapping, promote it to first
// - Names among the mapping are sorted by their order in the mapping
// - Names outside the mapping are sorted alphabetically
const names = Object.keys(mapping);
if (names.length !== 0) {
refs = refs.sort((left, right) => {
const indexLeft = names.indexOf(left.name);
const indexRight = names.indexOf(right.name);

if (indexLeft < 0 && indexRight < 0) {
// out of mapping, order by name
return left.name.localCompare(right.name);
} else if (indexLeft < 0) {
// the right is found, so mapping wins
return 1;
} else if (indexRight < 0) {
// left wins as it's in mapping
return -1;
} else {
return indexLeft - indexRight;
}
});
}

this.oneOf = refs.map(({ $ref, name }) => {
const innerSchema = new SchemaModel(parser, parser.byRef($ref)!, $ref, this.options, true);
innerSchema.title = name;
Expand Down