Skip to content

Commit ac31667

Browse files
committed
#33, do not use annotation mapping on unmapped object property
1 parent afa66ad commit ac31667

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriterPojo.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@ class DataTypeWriterPojo(
167167
propTypeName = prop.dataTypeValue
168168
}
169169

170-
val annotationTypeMappings = MappingFinder(apiOptions.typeMappings)
171-
.findTypeAnnotations(propDataType.dataType.getSourceName())
170+
if (propDataType.dataType !is ObjectDataType) {
171+
val annotationTypeMappings = MappingFinder(apiOptions.typeMappings)
172+
.findTypeAnnotations(propDataType.dataType.getSourceName())
172173

173-
annotationTypeMappings.forEach {
174-
val annotation = StringWriter()
175-
annotationWriter.write(annotation, Annotation(it.annotation.type, it.annotation.parameters))
176-
result += " $annotation\n"
174+
annotationTypeMappings.forEach {
175+
val annotation = StringWriter()
176+
annotationWriter.write(annotation, Annotation(it.annotation.type, it.annotation.parameters))
177+
result += " $annotation\n"
178+
}
177179
}
178180

179181
result += " ${getPropertyAnnotation(propertyName, propDataType)}\n"
@@ -308,6 +310,11 @@ class DataTypeWriterPojo(
308310
dataType.forEach { _, propDataType ->
309311
imports.add("com.fasterxml.jackson.annotation.JsonProperty")
310312

313+
// do not annotate unmapped, i.e. generated pojo property
314+
if ((propDataType is PropertyDataType) && (propDataType.dataType is ObjectDataType)) {
315+
return@forEach
316+
}
317+
311318
val target = getTarget(propDataType)
312319
val annotationTypeMappings = MappingFinder(apiOptions.typeMappings)
313320
.findTypeAnnotations(target.getSourceName())

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriterPojoSpec.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package io.openapiprocessor.core.writer.java
88
import io.kotest.core.spec.IsolationMode
99
import io.kotest.core.spec.style.StringSpec
1010
import io.kotest.matchers.collections.shouldContain
11+
import io.kotest.matchers.shouldBe
1112
import io.kotest.matchers.string.shouldContain
1213
import io.openapiprocessor.core.converter.ApiOptions
1314
import io.openapiprocessor.core.converter.mapping.Annotation as MappingAnnotation
@@ -20,6 +21,7 @@ import io.openapiprocessor.core.support.datatypes.ListDataType
2021
import io.openapiprocessor.core.support.datatypes.propertyDataType
2122
import io.openapiprocessor.core.support.datatypes.propertyDataTypeString
2223
import java.io.StringWriter
24+
import java.util.*
2325

2426
class DataTypeWriterPojoSpec: StringSpec({
2527
this.isolationMode = IsolationMode.InstancePerTest
@@ -387,5 +389,51 @@ class DataTypeWriterPojoSpec: StringSpec({
387389
""".trimMargin()
388390
}
389391

392+
"skips additional annotation from annotation mapping for un-mapped object datatype property" {
393+
options.typeMappings = listOf(
394+
AnnotationTypeMapping(
395+
"Foo", annotation = MappingAnnotation("foo.Bar", linkedMapOf())
396+
)
397+
)
398+
399+
writer = DataTypeWriterPojo(options, generatedWriter, BeanValidationFactory())
400+
401+
val dataType = ObjectDataType("Object",
402+
"pkg", linkedMapOf(
403+
"foo" to propertyDataType(ObjectDataType("Foo", "model")))
404+
)
405+
406+
// when:
407+
writer.write(target, dataType)
408+
409+
// then:
410+
val t1 = target.toString()
411+
val t2 =
412+
"""package pkg;
413+
|
414+
|import com.fasterxml.jackson.annotation.JsonProperty;
415+
|import io.openapiprocessor.generated.support.Generated;
416+
|import model.Foo;
417+
|
418+
|@Generated
419+
|public class Object {
420+
|
421+
| @JsonProperty("foo")
422+
| private Foo foo;
423+
|
424+
| public Foo getFoo() {
425+
| return foo;
426+
| }
427+
|
428+
| public void setFoo(Foo foo) {
429+
| this.foo = foo;
430+
| }
431+
|
432+
|}
433+
|
434+
""".trimMargin()
435+
436+
Arrays.mismatch(t1.toByteArray(), t2.toByteArray()).shouldBe(-1)
437+
}
390438

391439
})

0 commit comments

Comments
 (0)