xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / DeveloperAPI / patch.js
patch.js109 lines · 3.2 KiBRaw
| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | import fs from 'fs'; |
| 4 | |
| 5 | function update(schema, value) { |
| 6 | for (const key in value) { |
| 7 | const val = value[key]; |
| 8 | if (val === null) { |
| 9 | delete schema[key]; |
| 10 | } else if (Array.isArray(val)) { |
| 11 | schema[key] = (schema[key] ?? []).concat(val); |
| 12 | } else if (typeof val === 'object') { |
| 13 | if (schema[key] === undefined) { |
| 14 | schema[key] = {}; |
| 15 | } |
| 16 | update(schema[key], val); |
| 17 | } else { |
| 18 | schema[key] = val; |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // in-place update |
| 24 | function replace(schema, value) { |
| 25 | Object.keys(schema).forEach(k => delete schema[k]); |
| 26 | Object.assign(schema, value); |
| 27 | } |
| 28 | |
| 29 | function clone(schema) { |
| 30 | return JSON.parse(JSON.stringify(schema)); |
| 31 | } |
| 32 | |
| 33 | function makeOpen(enumSchema) { |
| 34 | replace(enumSchema, { |
| 35 | anyOf: [ |
| 36 | clone(enumSchema), |
| 37 | { type: 'string' }, |
| 38 | ] |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | function openAll(schema, path = []) { |
| 43 | if (Array.isArray(schema)) { |
| 44 | schema.forEach((s, i) => openAll(s, [...path, i])) |
| 45 | } else if (typeof schema === 'object' && schema !== null) { |
| 46 | const keys = new Set(Object.keys(schema)) |
| 47 | if (keys.size === 2 |
| 48 | && keys.has('type') |
| 49 | && keys.has('enum') |
| 50 | && schema.type === 'string' |
| 51 | && Array.isArray(schema.enum)) { |
| 52 | // several polymorphic schemas have a field literally named 'type', that always has a single case. |
| 53 | // skip that instance, but make all other enums open. |
| 54 | if (!(path[path.length - 1] === 'type' && schema.enum.length === 1)) { |
| 55 | makeOpen(schema); |
| 56 | } |
| 57 | } else { |
| 58 | for (const key in schema) { |
| 59 | openAll(schema[key], [...path, key]); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | function patch(schema) { |
| 66 | const schemas = schema.components.schemas; |
| 67 | |
| 68 | // this field is required iff using the private Xcode API |
| 69 | update(schemas.BundleIdCapabilityCreateRequest.properties.data.properties.relationships, { |
| 70 | properties: { |
| 71 | capability: { |
| 72 | type: 'object', |
| 73 | properties: { |
| 74 | data: { |
| 75 | type: 'object', |
| 76 | properties: { |
| 77 | type: { |
| 78 | type: 'string', |
| 79 | enum: ['capabilities'], |
| 80 | }, |
| 81 | id: { $ref: '#/components/schemas/CapabilityType' }, |
| 82 | }, |
| 83 | required: ['id', 'type'], |
| 84 | }, |
| 85 | }, |
| 86 | required: ['data'], |
| 87 | } |
| 88 | }, |
| 89 | }) |
| 90 | |
| 91 | // we don't use this but it triggers a deprecation warning. see: |
| 92 | // https://github.com/apple/swift-openapi-generator/issues/715 |
| 93 | schemas.App.properties.relationships.properties.inAppPurchases.deprecated = false; |
| 94 | |
| 95 | // openapi-generator expects response enums to be exhaustive. Apple's ASC OpenAPI spec |
| 96 | // misses some cases that they do, actually, return. So we make all bona fide enums in |
| 97 | // schema.components.schemas open. We don't do this on the schema.paths side because |
| 98 | // most (all?) of the enums in there are on the request side rather than the response. |
| 99 | // https://swiftpackageindex.com/apple/swift-openapi-generator/1.7.2/documentation/swift-openapi-generator/useful-openapi-patterns#Open-enums-and-oneOfs |
| 100 | openAll(schemas); |
| 101 | |
| 102 | return schema; |
| 103 | } |
| 104 | |
| 105 | const text = fs.readFileSync(process.stdin.fd, 'utf8'); |
| 106 | const json = JSON.parse(text); |
| 107 | const patched = patch(json); |
| 108 | const patchedText = JSON.stringify(patched); |
| 109 | console.log(patchedText); |