Skip to content

Commit 91478a5

Browse files
mook-asjandubois
authored andcommitted
moby-openapi: Add more fixups
Upstream is using more instances of types that can't be validated; rather than listing it all individually, walk the definition and find all instances of types known to have issues and do fixups directly. Signed-off-by: Mark Yen <mark.yen@suse.com> (cherry picked from commit b44b4a2)
1 parent 7228122 commit 91478a5

1 file changed

Lines changed: 40 additions & 13 deletions

File tree

scripts/dependencies/moby-openapi.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,46 @@ export class MobyOpenAPISpec extends GlobalDependency(VersionedDependency) {
3838
for (const key of Object.keys(contents.definitions ?? {}).filter(k => /^Plugin./.test(k))) {
3939
delete contents.definitions[key]?.['x-go-name'];
4040
}
41-
// This forces a go type that isn't defined; delete the override and just
42-
// use strings instead.
43-
if (_.get(contents, 'definitions.Plugin.properties.Config.properties.Interface.properties.Types.items.x-go-type.type') === 'CapabilityID') {
44-
delete contents.definitions.Plugin.properties.Config.properties.Interface.properties.Types.items['x-go-type'];
45-
}
46-
// Overriding the `dateTime` to `time.Time` means we can't use the validation function.
47-
if (_.get(contents, 'definitions.Network.properties.Created.x-go-type.import.package') === 'time') {
48-
_.set(contents, 'definitions.Network.properties.Created.x-go-type.hints.noValidation', true);
49-
}
50-
// Having the x-go-type here confuses swagger.
51-
if (_.has(contents, 'definitions.IPAMStatus.properties.Subnets.x-go-type')) {
52-
delete _.get(contents, 'definitions.IPAMStatus.properties.Subnets')['x-go-type'];
53-
}
41+
42+
// Some type overrides end up with errors, override them here:
43+
// noTypeOverride: This type does not actually exist in go, delete the override.
44+
// noValidate: This does not implement the .Validate() method; add a 'noValidation' hint.
45+
const perTypeActions: Record<string, 'noTypeOverride' | 'noValidate'> = {
46+
'net/netip.Addr': 'noValidate',
47+
'net/netip.Prefix': 'noValidate',
48+
'time.Time': 'noValidate',
49+
'undefined.int': 'noValidate',
50+
'undefined.CapabilityID': 'noTypeOverride', // This type is not defined anywhere.
51+
'undefined.SubnetStatuses': 'noTypeOverride', // This type is not defined anywhere.
52+
};
53+
54+
(function checkTypes(obj: object, prefix = '') {
55+
for (const [k, v] of Object.entries(obj)) {
56+
if (k === 'x-go-type') {
57+
const typeName = `${ v.import?.package }.${ v.type }`;
58+
if (typeName in perTypeActions) {
59+
switch (perTypeActions[typeName]) {
60+
case 'noTypeOverride':
61+
console.log(`\x1B[34m${ prefix } has invalid type ${ typeName }, removing.\x1B[0m`);
62+
delete (obj as any)[k];
63+
break;
64+
case 'noValidate':
65+
console.log(`\x1B[34m${ prefix } has type ${ typeName }, disabling validation.\x1B[0m`);
66+
_.set(v, 'hints.noValidation', true);
67+
break;
68+
}
69+
} else {
70+
console.log(`\x1B[34m${ prefix } has unknown type ${ typeName }, ignoring.\x1B[0m`);
71+
}
72+
} else if (_.isPlainObject(v)) {
73+
checkTypes(v, `${ prefix }.${ k }`.replace(/^\./, ''));
74+
} else if (Array.isArray(v)) {
75+
for (const [i, element] of Object.entries(v)) {
76+
checkTypes(element, `${ prefix }[${ i }]`);
77+
}
78+
}
79+
}
80+
})(contents);
5481

5582
await fs.promises.writeFile(modifiedPath, yaml.stringify(contents), 'utf-8');
5683

0 commit comments

Comments
 (0)