Skip to content

Commit 7aafd32

Browse files
wing328kota65535
authored andcommitted
Fix attributes in allOf and $ref (OpenAPITools#17836)
* fix allOf and ref properties * add tests
1 parent 9492607 commit 7aafd32

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

+20
Original file line numberDiff line numberDiff line change
@@ -3979,6 +3979,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
39793979
} else {
39803980
LOGGER.error("Unknown type in allOf schema. Please report the issue via openapi-generator's Github issue tracker.");
39813981
}
3982+
} else if (p.get$ref() != null) { // it's a ref
3983+
original = p;
39823984
}
39833985

39843986
CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
@@ -4214,6 +4216,24 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42144216
if (original.getDescription() != null) {
42154217
property.description = p.getDescription();
42164218
}
4219+
if (original.getMaxLength() != null) {
4220+
property.setMaxLength(original.getMaxLength());
4221+
}
4222+
if (original.getMinLength() != null) {
4223+
property.setMinLength(original.getMinLength());
4224+
}
4225+
if (original.getMaxItems() != null) {
4226+
property.setMaxItems(original.getMaxItems());
4227+
}
4228+
if (original.getMinItems() != null) {
4229+
property.setMinItems(original.getMinItems());
4230+
}
4231+
if (original.getMaximum() != null) {
4232+
property.setMaximum(String.valueOf(original.getMaximum().doubleValue()));
4233+
}
4234+
if (original.getMinimum() != null) {
4235+
property.setMinimum(String.valueOf(original.getMinimum().doubleValue()));
4236+
}
42174237
}
42184238

42194239
// set the default value

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinServerCodegenTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.openapitools.codegen.DefaultGenerator;
66
import org.openapitools.codegen.TestUtils;
77
import org.openapitools.codegen.languages.KotlinServerCodegen;
8+
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;
89

910
import java.io.File;
1011
import java.io.IOException;
@@ -164,4 +165,26 @@ public void beanValidationJakartaEeImports() throws IOException {
164165
"import javax.validation.Valid"
165166
);
166167
}
168+
169+
// to test attributes in the $ref (OpenAPI 3.1 spec)
170+
@Test
171+
public void attributesInRef() throws IOException {
172+
File output = Files.createTempDirectory("test_attributes").toFile().getCanonicalFile();
173+
output.deleteOnExit();
174+
175+
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
176+
codegen.setOutputDir(output.getAbsolutePath());
177+
178+
new DefaultGenerator().opts(new ClientOptInput()
179+
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/issue_17726.yaml"))
180+
.config(codegen))
181+
.generate();
182+
183+
String outputPath = output.getAbsolutePath() + "src/main/kotlin/org/openapitools";
184+
Path order = Paths.get(outputPath + "/model/Order.kt");
185+
assertFileContains(
186+
order,
187+
"@get:Size(max=50)"
188+
);
189+
}
167190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
openapi: 3.1.0
2+
info:
3+
title: dummy
4+
version: "1.0"
5+
paths:
6+
/users/{id}:
7+
summary: Represents a user
8+
components:
9+
schemas:
10+
Order:
11+
type: object
12+
required:
13+
- name
14+
properties:
15+
name:
16+
type: string
17+
address:
18+
$ref: '#/components/schemas/Address'
19+
maxLength: 50
20+
Address:
21+
type: object
22+
properties:
23+
firstName:
24+
type: string

0 commit comments

Comments
 (0)