Skip to content

Commit c613deb

Browse files
committed
extract factory for json annotations
1 parent fd93951 commit c613deb

6 files changed

Lines changed: 66 additions & 41 deletions

File tree

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ abstract class DataTypeWriterBase(
4444
protected val identifier: Identifier,
4545
protected val generatedWriter: GeneratedWriter,
4646
protected val validationAnnotations: BeanValidationFactory,
47-
protected val jacksonAnnotations: JacksonAnnotations,
47+
protected val jsonAnnotations: JsonAnnotationFactory,
4848
protected val javadocFactory: JavaDocFactory
4949
): DataTypeWriter {
5050
protected val annotationWriter = AnnotationWriter()
@@ -126,7 +126,9 @@ abstract class DataTypeWriterBase(
126126
result += extBuilder.toString()
127127

128128
if (requiresJsonPropertyAnnotation(propData)) {
129-
result += " ${getPropertyAnnotation(propData.srcPropName, propDataType)}"
129+
jsonAnnotations.createPropertyAnnotations(propData.srcPropName, propDataType).forEach {
130+
result += " $it\n"
131+
}
130132
}
131133

132134
result += if (access == Access.PRIVATE) {
@@ -186,28 +188,6 @@ abstract class DataTypeWriterBase(
186188
}
187189
}
188190

189-
private fun getPropertyAnnotation(propertyName: String, propDataType: PropertyDataType): String {
190-
val access = getAccess(propDataType)
191-
192-
var result = "@JsonProperty("
193-
result += if (access != null) {
194-
"value = \"$propertyName\", access = JsonProperty.Access.${access.value}"
195-
} else {
196-
"\"$propertyName\""
197-
}
198-
199-
result += ")\n"
200-
return result
201-
}
202-
203-
private fun getAccess(propDataType: PropertyDataType): PropertyAccess? {
204-
return when {
205-
propDataType.readOnly -> PropertyAccess("READ_ONLY")
206-
propDataType.writeOnly -> PropertyAccess("WRITE_ONLY")
207-
else -> null
208-
}
209-
}
210-
211191
protected fun ifDeprecated(propDataType: DataType): String {
212192
return if (propDataType.deprecated) {
213193
" $deprecated\n"
@@ -324,7 +304,7 @@ abstract class DataTypeWriterBase(
324304
val target = getTarget(propData.propDataType)
325305

326306
if (requiresJsonPropertyAnnotation(propData)) {
327-
imports.addAll(jacksonAnnotations.jsonProperty.imports)
307+
imports.addAll(jsonAnnotations.createPropertyImports(propData.propDataType))
328308
}
329309

330310
if (apiOptions.beanValidation) {
@@ -391,4 +371,3 @@ abstract class DataTypeWriterBase(
391371
}
392372
}
393373

394-
class PropertyAccess(val value: String)

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

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.converter.ApiOptions
99
import io.openapiprocessor.core.model.Annotation
10+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
1011

1112
/**
1213
* TODO remove v3
@@ -15,16 +16,17 @@ import io.openapiprocessor.core.model.Annotation
1516
*
1617
* mapping jon schema definition to configure the jackson version.
1718
*
18-
* "jackson": {
19-
* "description": "jackson annotations version.",
20-
* "default": "v2",
21-
* "enum": ["v2", "v3"]
22-
* },
19+
* "jackson": {
20+
* "description": "jackson annotations version.",
21+
* "default": "v2",
22+
* "enum": ["v2", "v3"]
23+
* }
2324
*/
24-
class JacksonAnnotations(apiOptions: ApiOptions) {
25-
val jsonProperty: Annotation
26-
val jsonCreator: Annotation
27-
val jsonValue: Annotation
25+
class JacksonAnnotations(apiOptions: ApiOptions): JsonAnnotationFactory {
26+
@Deprecated("use methods")
27+
override val jsonProperty: Annotation // todo make private
28+
override val jsonCreator: Annotation
29+
override val jsonValue: Annotation
2830

2931
init {
3032
when (getJacksonFormat(apiOptions)) {
@@ -41,11 +43,42 @@ class JacksonAnnotations(apiOptions: ApiOptions) {
4143
}
4244
}
4345

46+
override fun createPropertyImports(propDataType: PropertyDataType): Collection<String> {
47+
return jsonProperty.imports
48+
}
49+
50+
override fun createPropertyAnnotations(propertyName: String, propDataType: PropertyDataType): Collection<String> {
51+
val params = createPropertyParameter(propertyName, getAccess(propDataType))
52+
val result = "${jsonProperty.annotationName}(${params.joinToString(", ")})"
53+
return listOf(result)
54+
}
55+
56+
private fun createPropertyParameter(propertyName: String, access: PropertyAccess?): Collection<String> {
57+
return if (access != null) {
58+
listOf(
59+
"""value = "$propertyName"""",
60+
"access = JsonProperty.Access.${access.value}"
61+
)
62+
} else {
63+
listOf(""""$propertyName"""")
64+
}
65+
}
66+
67+
private fun getAccess(propDataType: PropertyDataType): PropertyAccess? {
68+
return when {
69+
propDataType.readOnly -> PropertyAccess("READ_ONLY")
70+
propDataType.writeOnly -> PropertyAccess("WRITE_ONLY")
71+
else -> null
72+
}
73+
}
74+
4475
private fun getJacksonFormat(options: ApiOptions): JacksonFormat {
4576
return when (options.jackson) {
4677
"v2" -> JacksonFormat.V2
47-
"v3" -> JacksonFormat.V3
78+
"v3" -> JacksonFormat.V2 /* v3 is currently not needed */
4879
else -> JacksonFormat.V2
4980
}
5081
}
5182
}
83+
84+
class PropertyAccess(val value: String)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ import io.openapiprocessor.core.model.EndpointResponse
1212
* mapping annotation factory.
1313
*/
1414
interface MappingAnnotationFactory {
15+
//fun imports(endpoint: Endpoint, endpointResponse: EndpointResponse): List<String> { return emptyList() }
1516
fun create(endpoint: Endpoint, endpointResponse: EndpointResponse): List<String>
1617
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,10 @@ class DataTypeWriterPojoSpec: StringSpec({
881881

882882
writer.write(target, dataType)
883883

884-
verify { jackson.jsonProperty }
884+
verify {
885+
jackson.createPropertyImports(any())
886+
jackson.createPropertyAnnotations(any(), any())
887+
}
885888
}
886889

887890
})

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,10 @@ class DataTypeWriterRecordSpec: StringSpec({
587587

588588
writer.write(target, dataType)
589589

590-
verify { jackson.jsonProperty }
590+
verify {
591+
jackson.createPropertyImports(any())
592+
jackson.createPropertyAnnotations(any(), any())
593+
}
591594
}
592595
})
593596

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package io.openapiprocessor.core.writer.java
77

88
import io.kotest.core.spec.style.StringSpec
99
import io.kotest.matchers.collections.shouldContainExactly
10+
import io.mockk.mockk
1011
import io.openapiprocessor.core.converter.ApiOptions
12+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
1113

1214
class JacksonAnnotationsSpec: StringSpec({
1315

@@ -17,18 +19,22 @@ class JacksonAnnotationsSpec: StringSpec({
1719

1820
val jackson = JacksonAnnotations(options)
1921

20-
jackson.jsonProperty.imports shouldContainExactly setOf("com.fasterxml.jackson.annotation.JsonProperty")
22+
jackson.createPropertyImports(mockk<PropertyDataType>())
23+
.shouldContainExactly(setOf("com.fasterxml.jackson.annotation.JsonProperty"))
24+
2125
jackson.jsonCreator.imports shouldContainExactly setOf("com.fasterxml.jackson.annotation.JsonCreator")
2226
jackson.jsonValue.imports shouldContainExactly setOf("com.fasterxml.jackson.annotation.JsonValue")
2327
}
2428

25-
"provides jackson 3 annotations" {
29+
"provides jackson 3 annotations".config(enabled = false) {
2630
val options = ApiOptions()
2731
options.jackson = "v3"
2832

2933
val jackson = JacksonAnnotations(options)
3034

31-
jackson.jsonProperty.imports shouldContainExactly setOf("tools.jackson.annotation.JsonProperty")
35+
jackson.createPropertyImports(mockk<PropertyDataType>())
36+
.shouldContainExactly(setOf("tools.jackson.annotation.JsonProperty"))
37+
3238
jackson.jsonCreator.imports shouldContainExactly setOf("tools.jackson.annotation.JsonCreator")
3339
jackson.jsonValue.imports shouldContainExactly setOf("tools.jackson.annotation.JsonValue")
3440
}

0 commit comments

Comments
 (0)