|
| 1 | +/* |
| 2 | + * Copyright 2019 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.IsolationMode |
| 9 | +import io.kotest.core.spec.style.StringSpec |
| 10 | +import io.kotest.matchers.shouldBe |
| 11 | +import io.openapiprocessor.core.converter.mapping.TypeMapping |
| 12 | +import io.openapiprocessor.core.framework.FrameworkBase |
| 13 | +import io.openapiprocessor.core.model.Api |
| 14 | +import io.openapiprocessor.core.support.parse |
| 15 | + |
| 16 | +class ApiConverterSpec: StringSpec({ |
| 17 | + isolationMode = IsolationMode.InstancePerTest |
| 18 | + |
| 19 | + "generates model if it is only referenced in the mapping" { |
| 20 | + val openApi = parse (""" |
| 21 | + openapi: 3.0.2 |
| 22 | + info: |
| 23 | + title: API |
| 24 | + version: 1.0.0 |
| 25 | + |
| 26 | + paths: |
| 27 | + /foo: |
| 28 | + get: |
| 29 | + responses: |
| 30 | + '200': |
| 31 | + description: ... |
| 32 | + content: |
| 33 | + application/json: |
| 34 | + schema: |
| 35 | + ${'$'}ref: '#/components/schemas/WrappedFoo' |
| 36 | + |
| 37 | + components: |
| 38 | + schemas: |
| 39 | + |
| 40 | + WrappedFoo: |
| 41 | + description: ... |
| 42 | + type: array |
| 43 | + items: |
| 44 | + ${'$'}ref: '#/components/schemas/Foo' |
| 45 | + |
| 46 | + Foo: |
| 47 | + description: a Foo |
| 48 | + type: object |
| 49 | + properties: |
| 50 | + foo: |
| 51 | + type: string |
| 52 | + |
| 53 | + """.trimIndent()) |
| 54 | + |
| 55 | + val options = ApiOptions() |
| 56 | + options.typeMappings = listOf( |
| 57 | + TypeMapping( |
| 58 | + "WrappedFoo", |
| 59 | + "io.openapiprocessor.test.Wrapped", |
| 60 | + listOf("io.openapiprocessor.generated.model.Foo") |
| 61 | + ) |
| 62 | + ) |
| 63 | + |
| 64 | + val api: Api = ApiConverter (options, FrameworkBase()) |
| 65 | + .convert(openApi) |
| 66 | + |
| 67 | + api.getDataTypes().getModelDataTypes().size shouldBe 1 |
| 68 | + } |
| 69 | + |
| 70 | + "generates model if it is only referenced in the mapping of a composed type" { |
| 71 | + val openApi = parse (""" |
| 72 | + openapi: 3.0.2 |
| 73 | + info: |
| 74 | + title: API |
| 75 | + version: 1.0.0 |
| 76 | + |
| 77 | + paths: |
| 78 | + /foo: |
| 79 | + get: |
| 80 | + responses: |
| 81 | + '200': |
| 82 | + description: ... |
| 83 | + content: |
| 84 | + application/json: |
| 85 | + schema: |
| 86 | + ${'$'}ref: '#/components/schemas/ComposedFoo' |
| 87 | + |
| 88 | + components: |
| 89 | + schemas: |
| 90 | + |
| 91 | + ComposedFoo: |
| 92 | + description: ... |
| 93 | + allOf: |
| 94 | + - ${'$'}ref: '#/components/schemas/Foo' |
| 95 | + |
| 96 | + Foo: |
| 97 | + description: a Foo |
| 98 | + type: object |
| 99 | + properties: |
| 100 | + foo: |
| 101 | + type: string |
| 102 | + |
| 103 | + """.trimIndent()) |
| 104 | + |
| 105 | + val options = ApiOptions() |
| 106 | + options.typeMappings = listOf( |
| 107 | + TypeMapping( |
| 108 | + "ComposedFoo", |
| 109 | + "io.openapiprocessor.test.Wrapped", |
| 110 | + listOf("io.openapiprocessor.generated.model.Foo") |
| 111 | + ) |
| 112 | + ) |
| 113 | + |
| 114 | + val api: Api = ApiConverter (options, FrameworkBase()) |
| 115 | + .convert(openApi) |
| 116 | + |
| 117 | + api.getDataTypes().getModelDataTypes().size shouldBe 1 |
| 118 | + } |
| 119 | + |
| 120 | +}) |
0 commit comments