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

Commit 8acf004

Browse files
authored
Merge pull request #71 from Tucos/master
Apply NotNull bean-validation to allOf composition
2 parents 6365bce + c810c7b commit 8acf004

File tree

11 files changed

+223
-1
lines changed

11 files changed

+223
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,15 @@ class DataTypeConverter(
102102
return filtered.first()
103103
}
104104

105+
val constraints = DataTypeConstraints(
106+
required = items.flatMap { it.constraints?.required ?: emptyList() }
107+
)
108+
105109
objectType = AllOfObjectDataType(
106110
DataTypeName(schemaInfo.getName(), getTypeNameWithSuffix(schemaInfo.getName())),
107111
listOf(options.packageName, "model").joinToString ("."),
108112
items,
109-
null,
113+
constraints,
110114
schemaInfo.getDeprecated()
111115
)
112116
} else {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright © 2020 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.converter
7+
8+
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.matchers.collections.shouldContainAll
10+
import io.kotest.matchers.types.shouldBeInstanceOf
11+
import io.openapiprocessor.core.model.DataTypes
12+
import io.openapiprocessor.core.model.HttpMethod
13+
import io.openapiprocessor.core.model.datatypes.AllOfObjectDataType
14+
import io.openapiprocessor.core.support.getBodySchemaInfo
15+
import io.openapiprocessor.core.support.parse
16+
17+
class DataTypeConverterAllOfRequiredSpec: StringSpec({
18+
19+
val dataTypes = DataTypes()
20+
21+
"applies required to allOf composition" {
22+
val openApi = parse("""
23+
openapi: 3.0.2
24+
info:
25+
title: API
26+
version: 1.0.0
27+
28+
paths:
29+
/foo:
30+
patch:
31+
requestBody:
32+
content:
33+
application/json:
34+
schema:
35+
${'$'}ref: '#/components/schemas/Foo'
36+
responses:
37+
'204':
38+
description: empty
39+
40+
components:
41+
schemas:
42+
43+
Foo:
44+
description: a Foo
45+
type: object
46+
allOf:
47+
- type: object
48+
required: [ foo, bar ]
49+
properties:
50+
foo:
51+
type: string
52+
bar:
53+
type: string
54+
- type: object
55+
required: [ baz, qux ]
56+
properties:
57+
baz:
58+
type: string
59+
qux:
60+
type: string
61+
62+
""".trimIndent())
63+
64+
65+
val options = ApiOptions()
66+
67+
val schemaInfo = openApi.getBodySchemaInfo("Foo",
68+
"/foo", HttpMethod.PATCH, "application/json")
69+
70+
// when:
71+
val converter = DataTypeConverter(options)
72+
val datatype = converter.convert(schemaInfo, dataTypes)
73+
74+
// then:
75+
datatype.shouldBeInstanceOf<AllOfObjectDataType>()
76+
datatype.constraints!!.required shouldContainAll listOf("foo", "bar", "baz", "qux")
77+
}
78+
79+
})

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
items:
2+
- generated/api/Api.java
3+
- generated/model/QueryGetResponse200.java
4+
# - generated/model/QueryResponse200_AllOf_0.java
5+
# - generated/model/QueryResponse200_AllOf_1.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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_AllOf_0 {
12+
13+
@NotNull
14+
@JsonProperty("prop1")
15+
private String prop1;
16+
17+
public String getProp1() {
18+
return prop1;
19+
}
20+
21+
public void setProp1(String prop1) {
22+
this.prop1 = prop1;
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
10+
public class QueryGetResponse200_AllOf_1 {
11+
12+
@JsonProperty("prop2")
13+
private String prop2;
14+
15+
public String getProp2() {
16+
return prop2;
17+
}
18+
19+
public void setProp2(String prop2) {
20+
this.prop2 = prop2;
21+
}
22+
23+
}
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)