Skip to content

Commit 2f0a55d

Browse files
committed
extract common code
1 parent 24ccd3c commit 2f0a55d

3 files changed

Lines changed: 95 additions & 159 deletions

File tree

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import java.io.Writer
1616

1717
private const val deprecated = "@Deprecated"
1818

19+
enum class Access {
20+
NONE, PRIVATE
21+
}
22+
1923
abstract class DataTypeWriterBase(
2024
protected val apiOptions: ApiOptions,
2125
protected val generatedWriter: GeneratedWriter,
@@ -37,6 +41,86 @@ abstract class DataTypeWriterBase(
3741
writeAnnotationsGenerated(target)
3842
}
3943

44+
protected fun getProp(
45+
propertyName: String,
46+
javaPropertyName: String,
47+
propDataType: PropertyDataType,
48+
required: Boolean,
49+
access: Access): String {
50+
51+
var result = ""
52+
53+
if (apiOptions.javadoc) {
54+
result += javadocWriter.convert(propDataType)
55+
}
56+
57+
result += ifDeprecated(propDataType)
58+
59+
var propTypeName = propDataType.getTypeName()
60+
if(apiOptions.beanValidation) {
61+
val info = validationAnnotations.validate(propDataType.dataType, required)
62+
val prop = info.prop
63+
prop.annotations.forEach {
64+
result += " ${it}\n"
65+
}
66+
propTypeName = prop.dataTypeValue
67+
}
68+
69+
if (propDataType.dataType !is ObjectDataType) {
70+
val annotationTypeMappings = MappingFinder(apiOptions.typeMappings)
71+
.findTypeAnnotations(propDataType.dataType.getSourceName())
72+
73+
annotationTypeMappings.forEach {
74+
val annotation = StringWriter()
75+
annotationWriter.write(annotation, Annotation(it.annotation.type, it.annotation.parameters))
76+
result += " $annotation\n"
77+
}
78+
}
79+
80+
result += " ${getPropertyAnnotation(propertyName, propDataType)}\n"
81+
82+
result += if (access == Access.PRIVATE) {
83+
" private $propTypeName $javaPropertyName"
84+
} else {
85+
" $propTypeName $javaPropertyName"
86+
}
87+
88+
return result
89+
}
90+
91+
private fun getPropertyAnnotation(propertyName: String, propDataType: PropertyDataType): String {
92+
val access = getAccess(propDataType)
93+
94+
var result = "@JsonProperty("
95+
if (access != null) {
96+
result += "value = \"$propertyName\", access = JsonProperty.Access.${access.value}"
97+
} else {
98+
result += "\"$propertyName\""
99+
}
100+
101+
result += ")"
102+
return result
103+
}
104+
105+
private fun getAccess(propDataType: PropertyDataType): PropertyAccess? {
106+
if (!propDataType.readOnly && !propDataType.writeOnly)
107+
return null
108+
109+
return when {
110+
propDataType.readOnly -> PropertyAccess("READ_ONLY")
111+
propDataType.writeOnly -> PropertyAccess("WRITE_ONLY")
112+
else -> throw IllegalStateException()
113+
}
114+
}
115+
116+
protected fun ifDeprecated(propDataType: DataType): String {
117+
return if (propDataType.deprecated) {
118+
" $deprecated\n"
119+
} else {
120+
""
121+
}
122+
}
123+
40124
private fun writePackage(target: Writer, dataType: ModelDataType) {
41125
target.write("package ${dataType.getPackageName()};\n\n")
42126
}

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

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.converter.ApiOptions
9-
import io.openapiprocessor.core.converter.MappingFinder
10-
import io.openapiprocessor.core.model.datatypes.*
9+
import io.openapiprocessor.core.model.datatypes.DataType
10+
import io.openapiprocessor.core.model.datatypes.ModelDataType
11+
import io.openapiprocessor.core.model.datatypes.NullDataType
12+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
1113
import io.openapiprocessor.core.support.capitalizeFirstChar
12-
import java.io.StringWriter
1314
import java.io.Writer
1415

15-
private const val deprecated = "@Deprecated"
16-
1716
/**
1817
* Writer for POJO classes.
1918
*/
@@ -54,7 +53,8 @@ class DataTypeWriterPojo(
5453
propName,
5554
javaPropertyName,
5655
propDataType as PropertyDataType,
57-
dataType.isRequired(propName))
56+
dataType.isRequired(propName),
57+
Access.PRIVATE)
5858

5959
// null (JsonNullable) may have an init value
6060
val pDataType = propDataType.dataType
@@ -98,72 +98,6 @@ class DataTypeWriterPojo(
9898
target.write("public class ${dataType.getTypeName()} {\n\n")
9999
}
100100

101-
private fun getProp(
102-
propertyName: String,
103-
javaPropertyName: String,
104-
propDataType: PropertyDataType,
105-
required: Boolean): String {
106-
107-
var result = ""
108-
109-
if (apiOptions.javadoc) {
110-
result += javadocWriter.convert(propDataType)
111-
}
112-
113-
result += ifDeprecated(propDataType)
114-
115-
var propTypeName = propDataType.getTypeName()
116-
if(apiOptions.beanValidation) {
117-
val info = validationAnnotations.validate(propDataType.dataType, required)
118-
val prop = info.prop
119-
prop.annotations.forEach {
120-
result += " ${it}\n"
121-
}
122-
propTypeName = prop.dataTypeValue
123-
}
124-
125-
if (propDataType.dataType !is ObjectDataType) {
126-
val annotationTypeMappings = MappingFinder(apiOptions.typeMappings)
127-
.findTypeAnnotations(propDataType.dataType.getSourceName())
128-
129-
annotationTypeMappings.forEach {
130-
val annotation = StringWriter()
131-
annotationWriter.write(annotation, Annotation(it.annotation.type, it.annotation.parameters))
132-
result += " $annotation\n"
133-
}
134-
}
135-
136-
result += " ${getPropertyAnnotation(propertyName, propDataType)}\n"
137-
result += " private $propTypeName $javaPropertyName"
138-
139-
return result
140-
}
141-
142-
private fun getPropertyAnnotation(propertyName: String, propDataType: PropertyDataType): String {
143-
val access = getAccess(propDataType)
144-
145-
var result = "@JsonProperty("
146-
if (access != null) {
147-
result += "value = \"$propertyName\", access = JsonProperty.Access.${access.value}"
148-
} else {
149-
result += "\"$propertyName\""
150-
}
151-
152-
result += ")"
153-
return result
154-
}
155-
156-
private fun getAccess(propDataType: PropertyDataType): PropertyAccess? {
157-
if (!propDataType.readOnly && !propDataType.writeOnly)
158-
return null
159-
160-
return when {
161-
propDataType.readOnly -> PropertyAccess("READ_ONLY")
162-
propDataType.writeOnly -> PropertyAccess("WRITE_ONLY")
163-
else -> throw IllegalStateException()
164-
}
165-
}
166-
167101
private fun getGetter(propertyName: String, propDataType: DataType): String {
168102
var result = ""
169103
result += ifDeprecated(propDataType)
@@ -195,12 +129,4 @@ class DataTypeWriterPojo(
195129

196130
return result
197131
}
198-
199-
private fun ifDeprecated(propDataType: DataType): String {
200-
return if (propDataType.deprecated) {
201-
" $deprecated\n"
202-
} else {
203-
""
204-
}
205-
}
206132
}

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

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.converter.ApiOptions
9-
import io.openapiprocessor.core.converter.MappingFinder
10-
import io.openapiprocessor.core.model.datatypes.*
11-
import java.io.StringWriter
9+
import io.openapiprocessor.core.model.datatypes.DataType
10+
import io.openapiprocessor.core.model.datatypes.ModelDataType
11+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
1212
import java.io.Writer
1313

14-
private const val deprecated = "@Deprecated"
15-
1614
/**
1715
* Writer for POJO classes with java 14 records.
1816
*/
@@ -48,7 +46,8 @@ class DataTypeWriterRecord(
4846
propName,
4947
javaPropertyName,
5048
propDataType as PropertyDataType,
51-
dataType.isRequired(propName))
49+
dataType.isRequired(propName),
50+
Access.NONE)
5251

5352
// todo can't init record parameter here
5453
// null (JsonNullable) may have an init value
@@ -82,77 +81,4 @@ class DataTypeWriterRecord(
8281
) {
8382
target.write("public record ${dataType.getTypeName()}")
8483
}
85-
86-
private fun getProp(
87-
propertyName: String,
88-
javaPropertyName: String,
89-
propDataType: PropertyDataType,
90-
required: Boolean): String {
91-
92-
var result = ""
93-
94-
if (apiOptions.javadoc) {
95-
result += javadocWriter.convert(propDataType)
96-
}
97-
98-
result += ifDeprecated(propDataType)
99-
100-
var propTypeName = propDataType.getTypeName()
101-
if(apiOptions.beanValidation) {
102-
val info = validationAnnotations.validate(propDataType.dataType, required)
103-
val prop = info.prop
104-
prop.annotations.forEach {
105-
result += " ${it}\n"
106-
}
107-
propTypeName = prop.dataTypeValue
108-
}
109-
110-
val annotationTypeMappings = MappingFinder(apiOptions.typeMappings)
111-
.findTypeAnnotations(propDataType.dataType.getSourceName())
112-
113-
annotationTypeMappings.forEach {
114-
val annotation = StringWriter()
115-
annotationWriter.write(annotation, Annotation(it.annotation.type, it.annotation.parameters))
116-
result += " $annotation\n"
117-
}
118-
119-
result += " ${getPropertyAnnotation(propertyName, propDataType)}\n"
120-
result += " $propTypeName $javaPropertyName"
121-
122-
return result
123-
}
124-
125-
private fun getPropertyAnnotation(propertyName: String, propDataType: PropertyDataType): String {
126-
val access = getAccess(propDataType)
127-
128-
var result = "@JsonProperty("
129-
if (access != null) {
130-
result += "value = \"$propertyName\", access = JsonProperty.Access.${access.value}"
131-
} else {
132-
result += "\"$propertyName\""
133-
}
134-
135-
result += ")"
136-
return result
137-
}
138-
139-
private fun getAccess(propDataType: PropertyDataType): PropertyAccess? {
140-
if (!propDataType.readOnly && !propDataType.writeOnly)
141-
return null
142-
143-
return when {
144-
propDataType.readOnly -> PropertyAccess("READ_ONLY")
145-
propDataType.writeOnly -> PropertyAccess("WRITE_ONLY")
146-
else -> throw IllegalStateException()
147-
}
148-
}
149-
150-
private fun ifDeprecated(propDataType: DataType): String {
151-
return if (propDataType.deprecated) {
152-
" $deprecated\n"
153-
} else {
154-
""
155-
}
156-
}
15784
}
158-

0 commit comments

Comments
 (0)