Skip to content

Commit 6fedd29

Browse files
committed
Fixed issue #20139 with additionalProperties in the Java Client
1 parent 4b2abdf commit 6fedd29

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,6 +2868,8 @@ protected void updateModelForObject(CodegenModel m, Schema schema) {
28682868
if (ModelUtils.isMapSchema(schema)) {
28692869
// an object or anyType composed schema that has additionalProperties set
28702870
addAdditionPropertiesToCodeGenModel(m, schema);
2871+
} else if(!ModelUtils.isDisallowAdditionalPropertiesIfNotPresent()) {
2872+
addAdditionPropertiesToCodeGenModel(m, schema);
28712873
} else if (ModelUtils.isFreeFormObject(schema, openAPI)) {
28722874
// non-composed object type with no properties + additionalProperties
28732875
// additionalProperties must be null, ObjectSchema, or empty Schema

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.openapitools.codegen.java.assertions.JavaFileAssert;
3535
import org.openapitools.codegen.languages.AbstractJavaCodegen;
3636
import org.openapitools.codegen.languages.JavaClientCodegen;
37+
import org.openapitools.codegen.languages.KotlinServerCodegen;
3738
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
3839
import org.openapitools.codegen.languages.features.CXFServerFeatures;
3940
import org.openapitools.codegen.meta.features.SecurityFeature;
@@ -56,14 +57,17 @@
5657
import java.util.regex.Matcher;
5758
import java.util.regex.Pattern;
5859
import java.util.stream.Collectors;
60+
import java.util.stream.Stream;
5961

6062
import static org.assertj.core.api.Assertions.assertThat;
6163
import static org.assertj.core.api.Assertions.entry;
6264
import static org.assertj.core.api.InstanceOfAssertFactories.FILE;
63-
import static org.openapitools.codegen.CodegenConstants.SERIALIZATION_LIBRARY;
64-
import static org.openapitools.codegen.TestUtils.newTempFolder;
65-
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
65+
import static org.openapitools.codegen.CodegenConstants.*;
66+
import static org.openapitools.codegen.TestUtils.*;
67+
import static org.openapitools.codegen.languages.AbstractKotlinCodegen.USE_JAKARTA_EE;
6668
import static org.openapitools.codegen.languages.JavaClientCodegen.*;
69+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.INTERFACE_ONLY;
70+
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.JAXRS_SPEC;
6771
import static org.testng.Assert.*;
6872

6973
public class JavaClientCodegenTest {
@@ -3592,4 +3596,27 @@ public void testClassesAreValidJavaOkHttpGson() {
35923596
"public some.pkg.B getsomepkgB() throws ClassCastException {"
35933597
);
35943598
}
3599+
3600+
@Test(description = "Issue #20139")
3601+
public void givenAdditionalPropertiesNotDefinedAndIsDisallowAdditionalPropertiesIfNotPresentIsFalseWhenGenerateModelThenAdditionalPropertiesTypeIsObject() throws Exception {
3602+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
3603+
output.deleteOnExit();
3604+
3605+
JavaClientCodegen codegen = new JavaClientCodegen();
3606+
codegen.setOutputDir(output.getAbsolutePath());
3607+
codegen.setUseBeanValidation(true);
3608+
codegen.additionalProperties().put(DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, false);
3609+
// The default, okhttp-gson, relies on isAdditionalPropertiesTrue and doesn't use additionalPropertiesType
3610+
codegen.additionalProperties().put(LIBRARY, JERSEY2);
3611+
new DefaultGenerator().opts(new ClientOptInput()
3612+
.openAPI(TestUtils.parseSpec("src/test/resources/2_0/issue_20139.yaml"))
3613+
.config(codegen))
3614+
.generate();
3615+
3616+
String outputPath = output.getAbsolutePath() + "/src/main/java/org/openapitools/client";
3617+
Path jsonWebKey = Paths.get(outputPath + "/model/JsonWebKey.java");
3618+
JavaFileAssert.assertThat(jsonWebKey)
3619+
.assertProperty("additionalProperties")
3620+
.withType("Map<String, Object>");
3621+
}
35953622
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
swagger: '2.0'
2+
info:
3+
version: '1.0'
4+
title: FooBar
5+
paths:
6+
/jwks:
7+
get:
8+
operationId: getJwks
9+
responses:
10+
200:
11+
description: "Success"
12+
schema:
13+
type: "array"
14+
items:
15+
$ref: "#/definitions/JsonWebKey"
16+
definitions:
17+
JsonWebKey:
18+
title: JSON Web Key
19+
type: object
20+
properties:
21+
kid:
22+
type: string
23+
# No additionalProperties specified

0 commit comments

Comments
 (0)