Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 16fe0be

Browse files
committed
convert nested types before checking the mapping, resolves #50
1 parent 0357dc7 commit 16fe0be

File tree

2 files changed

+126
-6
lines changed

2 files changed

+126
-6
lines changed

src/main/kotlin/io/openapiprocessor/core/converter/DataTypeConverter.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class DataTypeConverter(
7676
private fun createComposedDataType(schemaInfo: SchemaInfo, dataTypes: DataTypes): DataType {
7777
val objectType: DataType
7878

79+
val items: MutableList<DataType> = mutableListOf()
80+
schemaInfo.eachItemOf { itemSchemaInfo: SchemaInfo ->
81+
val itemType = convert(itemSchemaInfo, dataTypes)
82+
items.add (itemType)
83+
}
84+
7985
val targetType = getMappedDataType(schemaInfo)
8086
if (targetType != null) {
8187
objectType = MappedDataType(
@@ -88,12 +94,6 @@ class DataTypeConverter(
8894
return objectType
8995
}
9096

91-
val items: MutableList<DataType> = mutableListOf()
92-
schemaInfo.eachItemOf { itemSchemaInfo: SchemaInfo ->
93-
val itemType = convert(itemSchemaInfo, dataTypes)
94-
items.add (itemType)
95-
}
96-
9797
objectType = ComposedObjectDataType(
9898
schemaInfo.getName(),
9999
listOf(options.packageName, "model").joinToString ("."),
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)