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

Commit 23314da

Browse files
authored
Merge pull request #83 from openapi-processor/read-write-only
Read write only
2 parents 5ef63e7 + 5eb9273 commit 23314da

33 files changed

Lines changed: 549 additions & 125 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,20 @@ class DataTypeConverter(
155155
}
156156

157157
private fun createObjectDataType(schemaInfo: SchemaInfo, dataTypes: DataTypes): DataType {
158-
val properties = LinkedHashMap<String, DataType>()
158+
val properties = LinkedHashMap<String, PropertyDataType>()
159159
schemaInfo.eachProperty { propName: String, propSchemaInfo: SchemaInfo ->
160160
var propDataType = convert(propSchemaInfo, dataTypes)
161161

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/converter/SchemaInfo.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SchemaInfo(
5353
* schema.
5454
*
5555
* The swagger parser (resolve option) creates schemas for intermediate $refs where the name is
56-
* based on the filename. This breaks code generation because the original/public name of the
56+
* based on the filename. It breaks code generation because the original/public name of the
5757
* schema is lost.
5858
*/
5959
private var refName: Boolean = false
@@ -219,6 +219,12 @@ class SchemaInfo(
219219
val pattern: String?
220220
get() = schema?.pattern
221221

222+
val readOnly: Boolean
223+
get() = schema?.readOnly ?: false
224+
225+
val writeOnly: Boolean
226+
get() = schema?.writeOnly ?: false
227+
222228
/**
223229
* iterate over properties
224230
*/

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

src/main/kotlin/io/openapiprocessor/core/model/datatypes/DataType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface DataType {
3131
}
3232

3333
/**
34-
* The "package" of this type without [typeName].
34+
* The "package" of this type without [getTypeName].
3535
*/
3636
fun getPackageName(): String
3737

src/main/kotlin/io/openapiprocessor/core/model/datatypes/NullDataType.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
package io.openapiprocessor.core.model.datatypes
77

88
/**
9-
* Null(able) data type wrapper. Assumes a single generic parameter.
9+
* Null(able) data type wrapper, i.e. [org.openapitools.jackson.nullable.JsonNullable]. Assumes a
10+
* single generic parameter.
1011
*/
1112
class NullDataType(
1213
private val name: String,

src/main/kotlin/io/openapiprocessor/core/model/datatypes/ObjectDataType.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class ObjectDataType(
1414
private val name: DataTypeName,
1515
private val pkg: String,
1616
/** linked map to preserve order */
17-
private val properties: LinkedHashMap<String, DataType> = linkedMapOf(),
17+
private val properties: LinkedHashMap<String, PropertyDataType> = linkedMapOf(),
1818
override val constraints: DataTypeConstraints? = null,
1919
override val deprecated: Boolean = false,
2020
override val documentation: Documentation? = null
@@ -44,15 +44,15 @@ open class ObjectDataType(
4444
.toSet()
4545
}
4646

47-
fun addObjectProperty(name: String, type: DataType) {
47+
fun addObjectProperty(name: String, type: PropertyDataType) {
4848
properties[name] = type
4949
}
5050

51-
fun getObjectProperty(name: String): DataType {
51+
fun getObjectProperty(name: String): PropertyDataType {
5252
return properties[name]!!
5353
}
5454

55-
fun getProperties(): Map<String, DataType> {
55+
fun getProperties(): Map<String, PropertyDataType> {
5656
return properties
5757
}
5858

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import io.openapiprocessor.core.model.Documentation
9+
10+
/**
11+
* schema "properties" data type wrapper. readOnly/writeOnly may be different on each use of the
12+
* same schema as a property in another schema.
13+
*/
14+
open class PropertyDataType(
15+
val readOnly: Boolean,
16+
val writeOnly: Boolean,
17+
val dataType: DataType
18+
): DataType {
19+
20+
override fun getName(): String {
21+
return dataType.getName()
22+
}
23+
24+
override fun getTypeName(): String {
25+
return dataType.getTypeName()
26+
}
27+
28+
override fun getPackageName(): String {
29+
return dataType.getPackageName()
30+
}
31+
32+
override fun getImports(): Set<String> {
33+
return dataType.getImports()
34+
}
35+
36+
override val referencedImports: Set<String>
37+
get() = dataType.referencedImports
38+
39+
override val constraints: DataTypeConstraints?
40+
get() = dataType.constraints
41+
42+
override val deprecated: Boolean
43+
get() = dataType.deprecated
44+
45+
override val documentation: Documentation?
46+
get() = dataType.documentation
47+
48+
}
49+

src/main/kotlin/io/openapiprocessor/core/parser/Schema.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ interface Schema {
5858

5959
val pattern: String?
6060

61+
val readOnly: Boolean
62+
val writeOnly: Boolean
6163
}

src/main/kotlin/io/openapiprocessor/core/parser/openapi4j/Schema.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,10 @@ class Schema(val schema: O4jSchema) : ParserSchema {
106106
override val pattern: String?
107107
get() = schema.pattern
108108

109+
override val readOnly: Boolean
110+
get() = schema.readOnly ?: false
111+
112+
override val writeOnly: Boolean
113+
get() = schema.writeOnly ?: false
114+
109115
}

src/main/kotlin/io/openapiprocessor/core/parser/swagger/Schema.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,10 @@ class Schema(private val schema: SwaggerSchema<*>): ParserSchema {
116116
override val pattern: String?
117117
get() = schema.pattern
118118

119+
override val readOnly: Boolean
120+
get() = schema.readOnly ?: false
121+
122+
override val writeOnly: Boolean
123+
get() = schema.writeOnly ?: false
119124

120125
}

0 commit comments

Comments
 (0)