|
| 1 | +import { outdent } from 'outdent'; |
| 2 | +import { lintDocument } from '../../../lint.js'; |
| 3 | +import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils.js'; |
| 4 | +import { BaseResolver } from '../../../resolve.js'; |
| 5 | +import { createConfig } from '../../../config/index.js'; |
| 6 | + |
| 7 | +describe('discriminator-default-mapping', () => { |
| 8 | + it('should pass when optional propertyName has defaultMapping', async () => { |
| 9 | + const document = parseYamlToDocument( |
| 10 | + outdent` |
| 11 | + openapi: 3.2.0 |
| 12 | + components: |
| 13 | + schemas: |
| 14 | + Base: |
| 15 | + type: object |
| 16 | + discriminator: |
| 17 | + propertyName: test |
| 18 | + defaultMapping: DefaultType |
| 19 | + properties: |
| 20 | + test: |
| 21 | + type: string |
| 22 | + TypeA: |
| 23 | + allOf: |
| 24 | + - $ref: '#/components/schemas/Base' |
| 25 | + DefaultType: |
| 26 | + allOf: |
| 27 | + - $ref: '#/components/schemas/Base' |
| 28 | + `, |
| 29 | + 'foobar.yaml' |
| 30 | + ); |
| 31 | + |
| 32 | + const results = await lintDocument({ |
| 33 | + externalRefResolver: new BaseResolver(), |
| 34 | + document, |
| 35 | + config: await createConfig({ rules: { 'discriminator-default-mapping': 'error' } }), |
| 36 | + }); |
| 37 | + |
| 38 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should pass when required propertyName does not need defaultMapping', async () => { |
| 42 | + const document = parseYamlToDocument( |
| 43 | + outdent` |
| 44 | + openapi: 3.2.0 |
| 45 | + components: |
| 46 | + schemas: |
| 47 | + Base: |
| 48 | + type: object |
| 49 | + discriminator: |
| 50 | + propertyName: test |
| 51 | + mapping: |
| 52 | + a: TypeA |
| 53 | + required: |
| 54 | + - test |
| 55 | + properties: |
| 56 | + test: |
| 57 | + type: string |
| 58 | + TypeA: |
| 59 | + allOf: |
| 60 | + - $ref: '#/components/schemas/Base' |
| 61 | + `, |
| 62 | + 'foobar.yaml' |
| 63 | + ); |
| 64 | + |
| 65 | + const results = await lintDocument({ |
| 66 | + externalRefResolver: new BaseResolver(), |
| 67 | + document, |
| 68 | + config: await createConfig({ rules: { 'discriminator-default-mapping': 'error' } }), |
| 69 | + }); |
| 70 | + |
| 71 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should fail when optional propertyName lacks defaultMapping', async () => { |
| 75 | + const document = parseYamlToDocument( |
| 76 | + outdent` |
| 77 | + openapi: 3.2.0 |
| 78 | + components: |
| 79 | + schemas: |
| 80 | + Base: |
| 81 | + type: object |
| 82 | + discriminator: |
| 83 | + propertyName: test |
| 84 | + mapping: |
| 85 | + a: TypeA |
| 86 | + properties: |
| 87 | + test: |
| 88 | + type: string |
| 89 | + TypeA: |
| 90 | + allOf: |
| 91 | + - $ref: '#/components/schemas/Base' |
| 92 | + `, |
| 93 | + 'foobar.yaml' |
| 94 | + ); |
| 95 | + |
| 96 | + const results = await lintDocument({ |
| 97 | + externalRefResolver: new BaseResolver(), |
| 98 | + document, |
| 99 | + config: await createConfig({ rules: { 'discriminator-default-mapping': 'error' } }), |
| 100 | + }); |
| 101 | + |
| 102 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(` |
| 103 | + [ |
| 104 | + { |
| 105 | + "location": [ |
| 106 | + { |
| 107 | + "pointer": "#/components/schemas/Base/discriminator", |
| 108 | + "reportOnKey": false, |
| 109 | + "source": "foobar.yaml", |
| 110 | + }, |
| 111 | + ], |
| 112 | + "message": "Discriminator with optional property 'test' must include a defaultMapping field.", |
| 113 | + "ruleId": "discriminator-default-mapping", |
| 114 | + "severity": "error", |
| 115 | + "suggest": [], |
| 116 | + }, |
| 117 | + ] |
| 118 | + `); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should fail when defaultMapping points to a non-existent component', async () => { |
| 122 | + const document = parseYamlToDocument( |
| 123 | + outdent` |
| 124 | + openapi: 3.2.0 |
| 125 | + components: |
| 126 | + schemas: |
| 127 | + Base: |
| 128 | + type: object |
| 129 | + discriminator: |
| 130 | + propertyName: test |
| 131 | + defaultMapping: TypeB |
| 132 | + mapping: |
| 133 | + a: TypeA |
| 134 | + properties: |
| 135 | + test: |
| 136 | + type: string |
| 137 | + TypeA: |
| 138 | + allOf: |
| 139 | + - $ref: '#/components/schemas/Base' |
| 140 | + `, |
| 141 | + 'foobar.yaml' |
| 142 | + ); |
| 143 | + |
| 144 | + const results = await lintDocument({ |
| 145 | + externalRefResolver: new BaseResolver(), |
| 146 | + document, |
| 147 | + config: await createConfig({ rules: { 'discriminator-default-mapping': 'error' } }), |
| 148 | + }); |
| 149 | + |
| 150 | + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(` |
| 151 | + [ |
| 152 | + { |
| 153 | + "location": [ |
| 154 | + { |
| 155 | + "pointer": "#/components/schemas/Base/discriminator/defaultMapping", |
| 156 | + "reportOnKey": false, |
| 157 | + "source": "foobar.yaml", |
| 158 | + }, |
| 159 | + ], |
| 160 | + "message": "defaultMapping value 'TypeB' does not point to an existing schema component.", |
| 161 | + "ruleId": "discriminator-default-mapping", |
| 162 | + "severity": "error", |
| 163 | + "suggest": [], |
| 164 | + }, |
| 165 | + ] |
| 166 | + `); |
| 167 | + }); |
| 168 | +}); |
0 commit comments