Skip to content

Commit 2db4bbf

Browse files
committed
[php][php-nextgen] enumUnknownDefaultCase true now return the correct value for unknown values
1 parent 8998d83 commit 2db4bbf

File tree

5 files changed

+186
-3
lines changed

5 files changed

+186
-3
lines changed

modules/openapi-generator/src/main/resources/php-nextgen/model_generic.mustache

+5
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,18 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
419419
$allowedValues = $this->{{getter}}AllowableValues();
420420
{{^isContainer}}
421421
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
422+
{{#enumUnknownDefaultCase}}
423+
${{name}} = {{#allowableValues}}{{#enumVars}}{{#-last}}self::{{enumName}}_{{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}
424+
{{/enumUnknownDefaultCase}}
425+
{{^enumUnknownDefaultCase}}
422426
throw new InvalidArgumentException(
423427
sprintf(
424428
"Invalid value '%s' for '{{name}}', must be one of '%s'",
425429
${{{name}}},
426430
implode("', '", $allowedValues)
427431
)
428432
);
433+
{{/enumUnknownDefaultCase}}
429434
}
430435
{{/isContainer}}
431436
{{#isContainer}}

modules/openapi-generator/src/main/resources/php/model_generic.mustache

+5
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,18 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
408408
$allowedValues = $this->{{getter}}AllowableValues();
409409
{{^isContainer}}
410410
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
411+
{{#enumUnknownDefaultCase}}
412+
${{name}} = {{#allowableValues}}{{#enumVars}}{{#-last}}self::{{enumName}}_{{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}
413+
{{/enumUnknownDefaultCase}}
414+
{{^enumUnknownDefaultCase}}
411415
throw new \InvalidArgumentException(
412416
sprintf(
413417
"Invalid value '%s' for '{{name}}', must be one of '%s'",
414418
${{{name}}},
415419
implode("', '", $allowedValues)
416420
)
417421
);
422+
{{/enumUnknownDefaultCase}}
418423
}
419424
{{/isContainer}}
420425
{{#isContainer}}

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

+77-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,33 @@
1717

1818
package org.openapitools.codegen.php;
1919

20+
import io.swagger.parser.OpenAPIParser;
2021
import io.swagger.v3.oas.models.OpenAPI;
21-
import org.openapitools.codegen.CodegenConstants;
22-
import org.openapitools.codegen.CodegenModel;
22+
import io.swagger.v3.parser.core.models.ParseOptions;
23+
import org.openapitools.codegen.*;
24+
import org.openapitools.codegen.java.assertions.JavaFileAssert;
25+
import org.openapitools.codegen.languages.JavaMicroprofileServerCodegen;
2326
import org.openapitools.codegen.languages.PhpClientCodegen;
24-
import org.openapitools.codegen.TestUtils;
2527
import org.testng.Assert;
28+
import org.testng.annotations.BeforeMethod;
2629
import org.testng.annotations.Test;
2730

31+
import java.io.File;
32+
import java.nio.file.Files;
33+
import java.util.List;
34+
import java.util.Map;
35+
import java.util.function.Function;
36+
import java.util.stream.Collectors;
37+
2838
public class PhpClientCodegenTest {
2939

40+
protected PhpClientCodegen codegen;
41+
42+
@BeforeMethod
43+
public void before() {
44+
codegen = new PhpClientCodegen();
45+
}
46+
3047
@Test
3148
public void testInitialConfigValues() throws Exception {
3249
final PhpClientCodegen codegen = new PhpClientCodegen();
@@ -90,4 +107,61 @@ public void modelTest() {
90107
Assert.assertEquals(simpleName.classname, "DollarModel");
91108
Assert.assertEquals(simpleName.classVarName, "dollar_model");
92109
}
110+
111+
@Test
112+
public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception {
113+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
114+
output.deleteOnExit();
115+
116+
OpenAPI openAPI = new OpenAPIParser()
117+
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
118+
119+
codegen.setOutputDir(output.getAbsolutePath());
120+
codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");
121+
122+
ClientOptInput input = new ClientOptInput()
123+
.openAPI(openAPI)
124+
.config(codegen);
125+
126+
DefaultGenerator generator = new DefaultGenerator();
127+
Map<String, File> files = generator.opts(input).generate().stream()
128+
.collect(Collectors.toMap(File::getName, Function.identity()));
129+
130+
List<String> modelContent = Files
131+
.readAllLines(files.get("Pet.php").toPath())
132+
.stream()
133+
.map(String::trim)
134+
.collect(Collectors.toList());
135+
136+
Assert.assertListContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
137+
Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
138+
}
139+
140+
@Test
141+
public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception {
142+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
143+
output.deleteOnExit();
144+
145+
OpenAPI openAPI = new OpenAPIParser()
146+
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
147+
148+
codegen.setOutputDir(output.getAbsolutePath());
149+
150+
ClientOptInput input = new ClientOptInput()
151+
.openAPI(openAPI)
152+
.config(codegen);
153+
154+
DefaultGenerator generator = new DefaultGenerator();
155+
Map<String, File> files = generator.opts(input).generate().stream()
156+
.collect(Collectors.toMap(File::getName, Function.identity()));
157+
158+
List<String> modelContent = Files
159+
.readAllLines(files.get("Pet.php").toPath())
160+
.stream()
161+
.map(String::trim)
162+
.collect(Collectors.toList());
163+
164+
Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
165+
Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
166+
}
93167
}

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

+79
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,35 @@
1616

1717
package org.openapitools.codegen.php;
1818

19+
import io.swagger.parser.OpenAPIParser;
20+
import io.swagger.v3.oas.models.OpenAPI;
21+
import io.swagger.v3.parser.core.models.ParseOptions;
22+
import org.openapitools.codegen.ClientOptInput;
23+
import org.openapitools.codegen.CodegenConstants;
24+
import org.openapitools.codegen.DefaultGenerator;
25+
import org.openapitools.codegen.languages.PhpClientCodegen;
1926
import org.openapitools.codegen.languages.PhpNextgenClientCodegen;
2027
import org.openapitools.codegen.testutils.ConfigAssert;
2128
import org.testng.Assert;
29+
import org.testng.annotations.BeforeMethod;
2230
import org.testng.annotations.Test;
2331

32+
import java.io.File;
33+
import java.nio.file.Files;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.function.Function;
37+
import java.util.stream.Collectors;
38+
2439
public class PhpNextgenClientCodegenTest {
2540

41+
protected PhpNextgenClientCodegen codegen;
42+
43+
@BeforeMethod
44+
public void before() {
45+
codegen = new PhpNextgenClientCodegen();
46+
}
47+
2648
@Test
2749
public void testInitialConfigValues() throws Exception {
2850
final PhpNextgenClientCodegen codegen = new PhpNextgenClientCodegen();
@@ -68,4 +90,61 @@ public void testAdditionalPropertiesPutForConfigValuesWithTrueValue() throws Exc
6890
configAssert.assertValue(PhpNextgenClientCodegen.SUPPORT_STREAMING, codegen::isSupportStreaming, Boolean.TRUE);
6991
Assert.assertEquals(codegen.isSupportStreaming(), true);
7092
}
93+
94+
@Test
95+
public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception {
96+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
97+
output.deleteOnExit();
98+
99+
OpenAPI openAPI = new OpenAPIParser()
100+
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
101+
102+
codegen.setOutputDir(output.getAbsolutePath());
103+
codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");
104+
105+
ClientOptInput input = new ClientOptInput()
106+
.openAPI(openAPI)
107+
.config(codegen);
108+
109+
DefaultGenerator generator = new DefaultGenerator();
110+
Map<String, File> files = generator.opts(input).generate().stream()
111+
.collect(Collectors.toMap(File::getName, Function.identity()));
112+
113+
List<String> modelContent = Files
114+
.readAllLines(files.get("Pet.php").toPath())
115+
.stream()
116+
.map(String::trim)
117+
.collect(Collectors.toList());
118+
119+
Assert.assertListContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
120+
Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
121+
}
122+
123+
@Test
124+
public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception {
125+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
126+
output.deleteOnExit();
127+
128+
OpenAPI openAPI = new OpenAPIParser()
129+
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
130+
131+
codegen.setOutputDir(output.getAbsolutePath());
132+
133+
ClientOptInput input = new ClientOptInput()
134+
.openAPI(openAPI)
135+
.config(codegen);
136+
137+
DefaultGenerator generator = new DefaultGenerator();
138+
Map<String, File> files = generator.opts(input).generate().stream()
139+
.collect(Collectors.toMap(File::getName, Function.identity()));
140+
141+
List<String> modelContent = Files
142+
.readAllLines(files.get("Pet.php").toPath())
143+
.stream()
144+
.map(String::trim)
145+
.collect(Collectors.toList());
146+
147+
Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
148+
Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
149+
}
71150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 2.0.0
4+
title: test
5+
paths:
6+
/pets:
7+
get:
8+
summary: List all pets
9+
operationId: listPets
10+
responses:
11+
'200':
12+
description: OK
13+
components:
14+
schemas:
15+
Pet:
16+
type: object
17+
properties:
18+
Color:
19+
type: string
20+
enum: [RED, BLUE, GREEN]

0 commit comments

Comments
 (0)