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

Commit 7ee2db9

Browse files
committed
fix missing required constraint with allOf, #72
1 parent c3a5d00 commit 7ee2db9

File tree

10 files changed

+136
-3
lines changed

10 files changed

+136
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class DataTypeConverter(
106106
DataTypeName(schemaInfo.getName(), getTypeNameWithSuffix(schemaInfo.getName())),
107107
listOf(options.packageName, "model").joinToString ("."),
108108
items,
109-
null,
110109
schemaInfo.getDeprecated()
111110
)
112111
} else {
@@ -310,7 +309,8 @@ class DataTypeConverter(
310309

311310
// in case of an inline definition the name may be lowercase, make sure the enum
312311
// class gets an uppercase name!
313-
val enumName = schemaInfo.getName().capitalize ()
312+
val enumName = schemaInfo.getName()
313+
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
314314

315315
val found = dataTypes.find(enumName)
316316
if (found != null) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class AllOfObjectDataType(
1212
private val name: DataTypeName,
1313
private val pkg: String,
1414
private val items: List<DataType> = emptyList(),
15-
override val constraints: DataTypeConstraints? = null,
1615
override val deprecated: Boolean = false
1716
): DataType, ModelDataType {
1817

@@ -39,6 +38,18 @@ class AllOfObjectDataType(
3938
.toSet()
4039
}
4140

41+
override val constraints: DataTypeConstraints?
42+
get() {
43+
val required = items.filterIsInstance<ObjectDataType>()
44+
.flatMap { it.constraints?.required ?: emptyList() }
45+
46+
if (required.isEmpty()) {
47+
return null
48+
}
49+
50+
return DataTypeConstraints(required = required)
51+
}
52+
4253
override fun isRequired(prop: String): Boolean {
4354
return constraints?.isRequired(prop) ?: false
4455
}

src/test/kotlin/io/openapiprocessor/core/model/datatypes/ComposedObjectDataTypeSpec.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.openapiprocessor.core.model.datatypes
77

88
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.matchers.collections.shouldContainAll
910
import io.kotest.matchers.shouldBe
1011
import io.openapiprocessor.core.support.datatypes.ObjectDataType
1112

@@ -68,4 +69,36 @@ class ComposedObjectDataTypeSpec : StringSpec({
6869
composed.referencedImports shouldBe setOf("java.lang.String")
6970
}
7071

72+
"allOf handles 'required' constraint of all items" {
73+
val composed = AllOfObjectDataType(DataTypeName("AllOf"), "pkg", listOf(
74+
ObjectDataType("Foo", "pkg", linkedMapOf(
75+
"foo" to StringDataType(),
76+
"fux" to StringDataType()
77+
), constraints = DataTypeConstraints(required = listOf("foo", "fux"))),
78+
ObjectDataType(
79+
"Bar", "pkg", linkedMapOf(
80+
"bar" to StringDataType(),
81+
"bux" to StringDataType()
82+
), constraints = DataTypeConstraints(required = listOf("bar", "bux")))
83+
))
84+
85+
composed.constraints!!.required shouldContainAll listOf("foo", "fux", "bar", "bux")
86+
}
87+
88+
"allOf without 'required' has null constraints" {
89+
val composed = AllOfObjectDataType(DataTypeName("AllOf"), "pkg", listOf(
90+
ObjectDataType("Foo", "pkg", linkedMapOf(
91+
"foo" to StringDataType(),
92+
"fux" to StringDataType()
93+
), constraints = DataTypeConstraints()),
94+
ObjectDataType(
95+
"Bar", "pkg", linkedMapOf(
96+
"bar" to StringDataType(),
97+
"bux" to StringDataType()
98+
), constraints = null)
99+
))
100+
101+
composed.constraints shouldBe null
102+
}
103+
71104
})

src/testInt/groovy/io/openapiprocessor/core/TestSets.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TestSets {
1010
static def ALL = [
1111
'bean-validation',
1212
'bean-validation-iterable',
13+
'bean-validation-allof-required',
1314
'deprecated',
1415
'endpoint-exclude',
1516
'endpoint-http-mapping', // framework specific
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
items:
2+
- generated/api/Api.java
3+
- generated/model/QueryGetResponse200.java
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
3+
* TEST ONLY.
4+
*/
5+
6+
package generated.api;
7+
8+
import annotation.Mapping;
9+
import generated.model.QueryGetResponse200;
10+
11+
public interface Api {
12+
13+
@Mapping("/query")
14+
QueryGetResponse200 getQuery();
15+
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
3+
* TEST ONLY.
4+
*/
5+
6+
package generated.model;
7+
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
import javax.validation.constraints.NotNull;
10+
11+
public class QueryGetResponse200 {
12+
13+
@NotNull
14+
@JsonProperty("prop1")
15+
private String prop1;
16+
17+
@JsonProperty("prop2")
18+
private String prop2;
19+
20+
public String getProp1() {
21+
return prop1;
22+
}
23+
24+
public void setProp1(String prop1) {
25+
this.prop1 = prop1;
26+
}
27+
28+
public String getProp2() {
29+
return prop2;
30+
}
31+
32+
public void setProp2(String prop2) {
33+
this.prop2 = prop2;
34+
}
35+
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
items:
2+
- inputs/openapi.yaml
3+
- inputs/mapping.yaml
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
openapi-processor-spring: v2
2+
3+
options:
4+
package-name: generated
5+
bean-validation: true
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0.2
2+
info:
3+
title: merge allOf into same object
4+
version: 1.0.0
5+
6+
paths:
7+
/query:
8+
get:
9+
responses:
10+
'200':
11+
description: create result from allOff object
12+
content:
13+
application/json:
14+
schema:
15+
allOf:
16+
- type: object
17+
properties:
18+
prop1:
19+
type: string
20+
required:
21+
- prop1
22+
- type: object
23+
properties:
24+
prop2:
25+
type: string

0 commit comments

Comments
 (0)