|
| 1 | +/* |
| 2 | + * Copyright © 2021 https://github.com/openapi-processor/openapi-processor-core |
| 3 | + * PDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.openapiprocessor.core.converter |
| 7 | + |
| 8 | +import io.kotest.core.spec.style.StringSpec |
| 9 | +import io.kotest.matchers.shouldBe |
| 10 | +import io.kotest.matchers.types.shouldBeInstanceOf |
| 11 | +import io.kotest.matchers.types.shouldBeSameInstanceAs |
| 12 | +import io.openapiprocessor.core.model.DataTypes |
| 13 | +import io.openapiprocessor.core.model.HttpMethod |
| 14 | +import io.openapiprocessor.core.model.datatypes.ObjectDataType |
| 15 | +import io.openapiprocessor.core.model.datatypes.StringEnumDataType |
| 16 | +import io.openapiprocessor.core.support.getBodySchemaInfo |
| 17 | +import io.openapiprocessor.core.support.parse |
| 18 | + |
| 19 | +class DataTypeConverterSuffixSpec: StringSpec({ |
| 20 | + val dataTypes = DataTypes() |
| 21 | + |
| 22 | + "adds suffix to model data type name" { |
| 23 | + val options = ApiOptions() |
| 24 | + options.modelNameSuffix = "Suffix" |
| 25 | + |
| 26 | + val openApi = parse(""" |
| 27 | + openapi: 3.0.2 |
| 28 | + info: |
| 29 | + title: API |
| 30 | + version: 1.0.0 |
| 31 | + |
| 32 | + paths: |
| 33 | + /foo: |
| 34 | + post: |
| 35 | + requestBody: |
| 36 | + content: |
| 37 | + application/json: |
| 38 | + schema: |
| 39 | + ${'$'}ref: '#/components/schemas/Foo' |
| 40 | + responses: |
| 41 | + '204': |
| 42 | + description: empty |
| 43 | + |
| 44 | + components: |
| 45 | + schemas: |
| 46 | + |
| 47 | + Foo: |
| 48 | + description: a Foo |
| 49 | + type: object |
| 50 | + properties: |
| 51 | + foo: |
| 52 | + type: string |
| 53 | + |
| 54 | + """.trimIndent()) |
| 55 | + |
| 56 | + val schemaInfo = openApi.getBodySchemaInfo("Foo", |
| 57 | + "/foo", HttpMethod.POST, "application/json") |
| 58 | + |
| 59 | + // when: |
| 60 | + val converter = DataTypeConverter(options) |
| 61 | + val datatype = converter.convert(schemaInfo, dataTypes) |
| 62 | + |
| 63 | + // then: |
| 64 | + dataTypes.find("Foo") shouldBeSameInstanceAs datatype |
| 65 | + |
| 66 | + datatype.shouldBeInstanceOf<ObjectDataType>() |
| 67 | + datatype.getName().shouldBe("FooSuffix") |
| 68 | + } |
| 69 | + |
| 70 | + "ignores suffix if model data type name already ends with the suffix" { |
| 71 | + val options = ApiOptions() |
| 72 | + options.modelNameSuffix = "Suffix" |
| 73 | + |
| 74 | + val openApi = parse(""" |
| 75 | + openapi: 3.0.2 |
| 76 | + info: |
| 77 | + title: API |
| 78 | + version: 1.0.0 |
| 79 | + |
| 80 | + paths: |
| 81 | + /foo: |
| 82 | + post: |
| 83 | + requestBody: |
| 84 | + content: |
| 85 | + application/json: |
| 86 | + schema: |
| 87 | + ${'$'}ref: '#/components/schemas/FooWithSuffix' |
| 88 | + responses: |
| 89 | + '204': |
| 90 | + description: empty |
| 91 | + |
| 92 | + components: |
| 93 | + schemas: |
| 94 | + |
| 95 | + FooWithSuffix: |
| 96 | + description: a Foo |
| 97 | + type: object |
| 98 | + properties: |
| 99 | + foo: |
| 100 | + type: string |
| 101 | + |
| 102 | + """.trimIndent()) |
| 103 | + |
| 104 | + val schemaInfo = openApi.getBodySchemaInfo("FooWithSuffix", |
| 105 | + "/foo", HttpMethod.POST, "application/json") |
| 106 | + |
| 107 | + // when: |
| 108 | + val converter = DataTypeConverter(options) |
| 109 | + val datatype = converter.convert(schemaInfo, dataTypes) |
| 110 | + |
| 111 | + // then: |
| 112 | + dataTypes.find("FooWithSuffix") shouldBeSameInstanceAs datatype |
| 113 | + |
| 114 | + datatype.shouldBeInstanceOf<ObjectDataType>() |
| 115 | + datatype.getName().shouldBe("FooWithSuffix") |
| 116 | + } |
| 117 | + |
| 118 | + "adds suffix to model enum data type name" { |
| 119 | + val options = ApiOptions() |
| 120 | + options.modelNameSuffix = "Suffix" |
| 121 | + |
| 122 | + val openApi = parse(""" |
| 123 | + openapi: 3.0.2 |
| 124 | + info: |
| 125 | + title: API |
| 126 | + version: 1.0.0 |
| 127 | + |
| 128 | + paths: |
| 129 | + /foo: |
| 130 | + post: |
| 131 | + requestBody: |
| 132 | + content: |
| 133 | + application/json: |
| 134 | + schema: |
| 135 | + ${'$'}ref: '#/components/schemas/Bar' |
| 136 | + responses: |
| 137 | + '204': |
| 138 | + description: empty |
| 139 | + |
| 140 | + components: |
| 141 | + schemas: |
| 142 | + |
| 143 | + Bar: |
| 144 | + type: string |
| 145 | + enum: |
| 146 | + - bar-1 |
| 147 | + - bar-2 |
| 148 | + |
| 149 | + """.trimIndent()) |
| 150 | + |
| 151 | + val schemaInfo = openApi.getBodySchemaInfo("Bar", |
| 152 | + "/foo", HttpMethod.POST, "application/json") |
| 153 | + |
| 154 | + // when: |
| 155 | + val converter = DataTypeConverter(options) |
| 156 | + val datatype = converter.convert(schemaInfo, dataTypes) |
| 157 | + |
| 158 | + // then: |
| 159 | + dataTypes.find("Bar") shouldBeSameInstanceAs datatype |
| 160 | + |
| 161 | + datatype.shouldBeInstanceOf<StringEnumDataType>() |
| 162 | + datatype.getName().shouldBe("BarSuffix") |
| 163 | + } |
| 164 | + |
| 165 | + "ignores suffix if model enum data type name already ends with the suffix" { |
| 166 | + val options = ApiOptions() |
| 167 | + options.modelNameSuffix = "Suffix" |
| 168 | + |
| 169 | + val openApi = parse(""" |
| 170 | + openapi: 3.0.2 |
| 171 | + info: |
| 172 | + title: API |
| 173 | + version: 1.0.0 |
| 174 | + |
| 175 | + paths: |
| 176 | + /foo: |
| 177 | + post: |
| 178 | + requestBody: |
| 179 | + content: |
| 180 | + application/json: |
| 181 | + schema: |
| 182 | + ${'$'}ref: '#/components/schemas/BarWithSuffix' |
| 183 | + responses: |
| 184 | + '204': |
| 185 | + description: empty |
| 186 | + |
| 187 | + components: |
| 188 | + schemas: |
| 189 | + |
| 190 | + BarWithSuffix: |
| 191 | + type: string |
| 192 | + enum: |
| 193 | + - bar-1 |
| 194 | + - bar-2 |
| 195 | + |
| 196 | + """.trimIndent()) |
| 197 | + |
| 198 | + val schemaInfo = openApi.getBodySchemaInfo("BarWithSuffix", |
| 199 | + "/foo", HttpMethod.POST, "application/json") |
| 200 | + |
| 201 | + // when: |
| 202 | + val converter = DataTypeConverter(options) |
| 203 | + val datatype = converter.convert(schemaInfo, dataTypes) |
| 204 | + |
| 205 | + // then: |
| 206 | + dataTypes.find("BarWithSuffix") shouldBeSameInstanceAs datatype |
| 207 | + |
| 208 | + datatype.shouldBeInstanceOf<StringEnumDataType>() |
| 209 | + datatype.getName().shouldBe("BarWithSuffix") |
| 210 | + } |
| 211 | + |
| 212 | +}) |
0 commit comments