|
2 | 2 |
|
3 | 3 | import fs from 'fs'; |
4 | 4 |
|
5 | | -function makeOpen(enumSchema) { |
6 | | - const copy = structuredClone(enumSchema); |
7 | | - Object.keys(enumSchema).forEach(k => delete enumSchema[k]); |
8 | | - Object.assign(enumSchema, { |
9 | | - anyOf: [ |
10 | | - copy, |
11 | | - { type: 'string' }, |
12 | | - ] |
13 | | - }) |
| 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)); |
14 | 31 | } |
15 | 32 |
|
16 | | -function format(schema) { |
17 | | - const schemas = schema.components.schemas; |
| 33 | +function makeOpen(enumSchema) { |
| 34 | + replace(enumSchema, { |
| 35 | + anyOf: [ |
| 36 | + clone(enumSchema), |
| 37 | + { type: 'string' }, |
| 38 | + ] |
| 39 | + }); |
| 40 | +} |
18 | 41 |
|
19 | | - // this field is required when using the private Xcode API |
20 | | - const capabilityCreateRelationships = schemas.BundleIdCapabilityCreateRequest.properties.data.properties.relationships; |
21 | | - capabilityCreateRelationships.properties.capability = { |
| 42 | +function patch(schema) { |
| 43 | + const schemas = schema.components.schemas; |
| 44 | + |
| 45 | + // this field is required when using the private Xcode API |
| 46 | + update(schemas.BundleIdCapabilityCreateRequest.properties.data.properties.relationships, { |
| 47 | + properties: { |
| 48 | + capability: { |
22 | 49 | type: 'object', |
23 | 50 | properties: { |
24 | | - data: { |
25 | | - type: 'object', |
26 | | - properties: { |
27 | | - type: { |
28 | | - type: 'string', |
29 | | - enum: ['capabilities'], |
30 | | - }, |
31 | | - id: { $ref: '#/components/schemas/CapabilityType' }, |
32 | | - }, |
33 | | - required: ['id', 'type'], |
| 51 | + data: { |
| 52 | + type: 'object', |
| 53 | + properties: { |
| 54 | + type: { |
| 55 | + type: 'string', |
| 56 | + enum: ['capabilities'], |
| 57 | + }, |
| 58 | + id: { $ref: '#/components/schemas/CapabilityType' }, |
34 | 59 | }, |
| 60 | + required: ['id', 'type'], |
| 61 | + }, |
35 | 62 | }, |
36 | 63 | required: ['data'], |
37 | | - } |
38 | | - capabilityCreateRelationships.required.push('capability'); |
39 | | - |
40 | | - // we don't use this but it triggers a deprecation warning. see: |
41 | | - // https://github.com/apple/swift-openapi-generator/issues/715 |
42 | | - schemas.App.properties.relationships.properties.inAppPurchases.deprecated = false; |
43 | | - |
44 | | - // openapi-generator expects response enums to be exhaustive. Apple's ASC OpenAPI spec |
45 | | - // misses some cases that they do, actually, return. |
46 | | - // https://swiftpackageindex.com/apple/swift-openapi-generator/1.7.2/documentation/swift-openapi-generator/useful-openapi-patterns#Open-enums-and-oneOfs |
47 | | - makeOpen(schemas.BundleIdPlatform); |
48 | | - makeOpen(schemas.CapabilityType); |
49 | | - makeOpen(schemas.CertificateType); |
50 | | - makeOpen(schemas.Device.properties.attributes.properties.deviceClass); |
51 | | - |
52 | | - return schema; |
| 64 | + } |
| 65 | + }, |
| 66 | + required: ['capability'], |
| 67 | + }) |
| 68 | + |
| 69 | + // we don't use this but it triggers a deprecation warning. see: |
| 70 | + // https://github.com/apple/swift-openapi-generator/issues/715 |
| 71 | + schemas.App.properties.relationships.properties.inAppPurchases.deprecated = false; |
| 72 | + |
| 73 | + // openapi-generator expects response enums to be exhaustive. Apple's ASC OpenAPI spec |
| 74 | + // misses some cases that they do, actually, return. |
| 75 | + // https://swiftpackageindex.com/apple/swift-openapi-generator/1.7.2/documentation/swift-openapi-generator/useful-openapi-patterns#Open-enums-and-oneOfs |
| 76 | + makeOpen(schemas.BundleIdPlatform); |
| 77 | + makeOpen(schemas.CapabilityType); |
| 78 | + makeOpen(schemas.CertificateType); |
| 79 | + makeOpen(schemas.Device.properties.attributes.properties.deviceClass); |
| 80 | + |
| 81 | + return schema; |
53 | 82 | } |
54 | 83 |
|
55 | 84 | const text = fs.readFileSync(process.stdin.fd, 'utf8'); |
56 | 85 | const json = JSON.parse(text); |
57 | | -const formatted = format(json); |
58 | | -const formattedText = JSON.stringify(formatted); |
59 | | -console.log(formattedText); |
| 86 | +const patched = patch(json); |
| 87 | +const patchedText = JSON.stringify(patched); |
| 88 | +console.log(patchedText); |
0 commit comments