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

Commit c31c3c2

Browse files
committed
1 parent 353bd61 commit c31c3c2

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ class DataTypeConverter(
162162
if (propSchemaInfo.getNullable()) {
163163
propDataType = nullWrapper.wrap(propDataType, schemaInfo)
164164
}
165+
166+
propDataType = PropertyDataType(
167+
propSchemaInfo.readOnly,
168+
propSchemaInfo.writeOnly,
169+
propDataType
170+
)
171+
165172
properties[propName] = propDataType
166173
}
167174

src/main/kotlin/io/openapiprocessor/core/model/DataTypeCollector.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class DataTypeCollector(
5353
is StringEnumDataType -> {
5454
dataTypes.addRef(dataType.getName())
5555
}
56+
is PropertyDataType -> {
57+
collect(dataType.dataType)
58+
}
5659
}
5760
}
5861

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2021 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.model.datatypes
7+
8+
/**
9+
* schema "properties" data type wrapper. readOnly/writeOnly may be different on each use of the
10+
* same schema as a property in another schema.
11+
*/
12+
open class PropertyDataType(
13+
val readOnly: Boolean,
14+
val writeOnly: Boolean,
15+
val dataType: DataType
16+
): DataType {
17+
18+
override fun getName(): String {
19+
return dataType.getName()
20+
}
21+
22+
override fun getPackageName(): String {
23+
return dataType.getPackageName()
24+
}
25+
26+
override fun getImports(): Set<String> {
27+
return dataType.getImports()
28+
}
29+
30+
}
31+

src/main/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriter.kt

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.openapiprocessor.core.converter.ApiOptions
99
import io.openapiprocessor.core.model.datatypes.DataType
1010
import io.openapiprocessor.core.model.datatypes.ModelDataType
1111
import io.openapiprocessor.core.model.datatypes.NullDataType
12+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
1213
import io.openapiprocessor.core.support.capitalizeFirstChar
1314
import java.io.Writer
1415

@@ -56,7 +57,7 @@ class DataTypeWriter(
5657

5758
dataType.forEach { propName, propDataType ->
5859
val javaPropertyName = toCamelCase(propName)
59-
target.write(getProp(propName, javaPropertyName, propDataType,
60+
target.write(getProp(propName, javaPropertyName, propDataType as PropertyDataType,
6061
dataType.isRequired(propName)))
6162
}
6263

@@ -70,8 +71,10 @@ class DataTypeWriter(
7071
}
7172

7273
private fun getProp(
73-
propertyName: String, javaPropertyName: String,
74-
propDataType: DataType, required: Boolean): String {
74+
propertyName: String,
75+
javaPropertyName: String,
76+
propDataType: PropertyDataType,
77+
required: Boolean): String {
7578

7679
var result = ""
7780

@@ -91,18 +94,44 @@ class DataTypeWriter(
9194
propTypeName = prop.dataTypeValue
9295
}
9396

94-
result += " @JsonProperty(\"$propertyName\")\n"
97+
result += " ${getPropertyAnnotation(propertyName, propDataType)}\n"
9598
result += " private $propTypeName $javaPropertyName"
9699

97-
// null may have an init value
98-
if (propDataType is NullDataType && propDataType.init != null) {
99-
result += " = ${propDataType.init}"
100+
// null (JsonNullable) may have an init value
101+
val dataType = propDataType.dataType
102+
if (dataType is NullDataType && dataType.init != null) {
103+
result += " = ${dataType.init}"
100104
}
101105

102106
result += ";\n\n"
103107
return result
104108
}
105109

110+
private fun getPropertyAnnotation(propertyName: String, propDataType: PropertyDataType): String {
111+
val access = getAccess(propDataType)
112+
113+
var result = "@JsonProperty("
114+
if (access != null) {
115+
result += "value = \"$propertyName\", access = JsonProperty.Access.${access.value}"
116+
} else {
117+
result += "\"$propertyName\""
118+
}
119+
120+
result += ")"
121+
return result
122+
}
123+
124+
private fun getAccess(propDataType: PropertyDataType): PropertyAccess? {
125+
if (!propDataType.readOnly && !propDataType.writeOnly)
126+
return null
127+
128+
return when {
129+
propDataType.readOnly -> PropertyAccess("READ_ONLY")
130+
propDataType.writeOnly -> PropertyAccess("WRITE_ONLY")
131+
else -> throw IllegalStateException()
132+
}
133+
}
134+
106135
private fun getGetter(propertyName: String, propDataType: DataType): String {
107136
var result = ""
108137
result += ifDeprecated(propDataType)
@@ -170,3 +199,5 @@ class DataTypeWriter(
170199
}
171200

172201
}
202+
203+
class PropertyAccess(val value: String)

0 commit comments

Comments
 (0)