Skip to content

Fix attributes in allOf and $ref #17836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3979,6 +3979,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
} else {
LOGGER.error("Unknown type in allOf schema. Please report the issue via openapi-generator's Github issue tracker.");
}
} else if (p.get$ref() != null) { // it's a ref
original = p;
}

CodegenProperty property = CodegenModelFactory.newInstance(CodegenModelType.PROPERTY);
Expand Down Expand Up @@ -4214,6 +4216,24 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
if (original.getDescription() != null) {
property.description = p.getDescription();
}
if (original.getMaxLength() != null) {
property.setMaxLength(original.getMaxLength());
}
if (original.getMinLength() != null) {
property.setMinLength(original.getMinLength());
}
if (original.getMaxItems() != null) {
property.setMaxItems(original.getMaxItems());
}
if (original.getMinItems() != null) {
property.setMinItems(original.getMinItems());
}
if (original.getMaximum() != null) {
property.setMaximum(String.valueOf(original.getMaximum().doubleValue()));
}
if (original.getMinimum() != null) {
property.setMinimum(String.valueOf(original.getMinimum().doubleValue()));
}
}

// set the default value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.KotlinServerCodegen;
import org.openapitools.codegen.languages.KotlinSpringServerCodegen;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -164,4 +165,26 @@ public void beanValidationJakartaEeImports() throws IOException {
"import javax.validation.Valid"
);
}

// to test attributes in the $ref (OpenAPI 3.1 spec)
@Test
public void attributesInRef() throws IOException {
File output = Files.createTempDirectory("test_attributes").toFile().getCanonicalFile();
output.deleteOnExit();

KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());

new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_1/issue_17726.yaml"))
.config(codegen))
.generate();

String outputPath = output.getAbsolutePath() + "src/main/kotlin/org/openapitools";
Path order = Paths.get(outputPath + "/model/Order.kt");
assertFileContains(
order,
"@get:Size(max=50)"
);
}
}
24 changes: 24 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/issue_17726.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.1.0
info:
title: dummy
version: "1.0"
paths:
/users/{id}:
summary: Represents a user
components:
schemas:
Order:
type: object
required:
- name
properties:
name:
type: string
address:
$ref: '#/components/schemas/Address'
maxLength: 50
Address:
type: object
properties:
firstName:
type: string