forked from loafle/openapi-generator-original
[php][php-nextgen] enumUnknownDefaultCase true now return the correct value for unknown values (#20594)
This commit is contained in:
parent
53f7e471c4
commit
97c805f0e0
@ -419,6 +419,10 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
|
||||
$allowedValues = $this->{{getter}}AllowableValues();
|
||||
{{^isContainer}}
|
||||
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
|
||||
{{#enumUnknownDefaultCase}}
|
||||
${{name}} = {{#allowableValues}}{{#enumVars}}{{#-last}}self::{{enumName}}_{{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
{{/enumUnknownDefaultCase}}
|
||||
{{^enumUnknownDefaultCase}}
|
||||
throw new InvalidArgumentException(
|
||||
sprintf(
|
||||
"Invalid value '%s' for '{{name}}', must be one of '%s'",
|
||||
@ -426,6 +430,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
|
||||
implode("', '", $allowedValues)
|
||||
)
|
||||
);
|
||||
{{/enumUnknownDefaultCase}}
|
||||
}
|
||||
{{/isContainer}}
|
||||
{{#isContainer}}
|
||||
|
@ -408,6 +408,10 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
|
||||
$allowedValues = $this->{{getter}}AllowableValues();
|
||||
{{^isContainer}}
|
||||
if ({{#isNullable}}!is_null(${{name}}) && {{/isNullable}}!in_array(${{{name}}}, $allowedValues, true)) {
|
||||
{{#enumUnknownDefaultCase}}
|
||||
${{name}} = {{#allowableValues}}{{#enumVars}}{{#-last}}self::{{enumName}}_{{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}
|
||||
{{/enumUnknownDefaultCase}}
|
||||
{{^enumUnknownDefaultCase}}
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf(
|
||||
"Invalid value '%s' for '{{name}}', must be one of '%s'",
|
||||
@ -415,6 +419,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}}{{/parentSchema}}{{^par
|
||||
implode("', '", $allowedValues)
|
||||
)
|
||||
);
|
||||
{{/enumUnknownDefaultCase}}
|
||||
}
|
||||
{{/isContainer}}
|
||||
{{#isContainer}}
|
||||
|
@ -17,16 +17,33 @@
|
||||
|
||||
package org.openapitools.codegen.php;
|
||||
|
||||
import io.swagger.parser.OpenAPIParser;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import io.swagger.v3.parser.core.models.ParseOptions;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.java.assertions.JavaFileAssert;
|
||||
import org.openapitools.codegen.languages.JavaMicroprofileServerCodegen;
|
||||
import org.openapitools.codegen.languages.PhpClientCodegen;
|
||||
import org.openapitools.codegen.TestUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PhpClientCodegenTest {
|
||||
|
||||
protected PhpClientCodegen codegen;
|
||||
|
||||
@BeforeMethod
|
||||
public void before() {
|
||||
codegen = new PhpClientCodegen();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialConfigValues() throws Exception {
|
||||
final PhpClientCodegen codegen = new PhpClientCodegen();
|
||||
@ -90,4 +107,61 @@ public class PhpClientCodegenTest {
|
||||
Assert.assertEquals(simpleName.classname, "DollarModel");
|
||||
Assert.assertEquals(simpleName.classVarName, "dollar_model");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception {
|
||||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
||||
output.deleteOnExit();
|
||||
|
||||
OpenAPI openAPI = new OpenAPIParser()
|
||||
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
|
||||
|
||||
codegen.setOutputDir(output.getAbsolutePath());
|
||||
codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");
|
||||
|
||||
ClientOptInput input = new ClientOptInput()
|
||||
.openAPI(openAPI)
|
||||
.config(codegen);
|
||||
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
Map<String, File> files = generator.opts(input).generate().stream()
|
||||
.collect(Collectors.toMap(File::getName, Function.identity()));
|
||||
|
||||
List<String> modelContent = Files
|
||||
.readAllLines(files.get("Pet.php").toPath())
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertListContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
|
||||
Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception {
|
||||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
||||
output.deleteOnExit();
|
||||
|
||||
OpenAPI openAPI = new OpenAPIParser()
|
||||
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
|
||||
|
||||
codegen.setOutputDir(output.getAbsolutePath());
|
||||
|
||||
ClientOptInput input = new ClientOptInput()
|
||||
.openAPI(openAPI)
|
||||
.config(codegen);
|
||||
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
Map<String, File> files = generator.opts(input).generate().stream()
|
||||
.collect(Collectors.toMap(File::getName, Function.identity()));
|
||||
|
||||
List<String> modelContent = Files
|
||||
.readAllLines(files.get("Pet.php").toPath())
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
|
||||
Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
|
||||
}
|
||||
}
|
||||
|
@ -16,13 +16,35 @@
|
||||
|
||||
package org.openapitools.codegen.php;
|
||||
|
||||
import io.swagger.parser.OpenAPIParser;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.parser.core.models.ParseOptions;
|
||||
import org.openapitools.codegen.ClientOptInput;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.DefaultGenerator;
|
||||
import org.openapitools.codegen.languages.PhpClientCodegen;
|
||||
import org.openapitools.codegen.languages.PhpNextgenClientCodegen;
|
||||
import org.openapitools.codegen.testutils.ConfigAssert;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PhpNextgenClientCodegenTest {
|
||||
|
||||
protected PhpNextgenClientCodegen codegen;
|
||||
|
||||
@BeforeMethod
|
||||
public void before() {
|
||||
codegen = new PhpNextgenClientCodegen();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialConfigValues() throws Exception {
|
||||
final PhpNextgenClientCodegen codegen = new PhpNextgenClientCodegen();
|
||||
@ -68,4 +90,61 @@ public class PhpNextgenClientCodegenTest {
|
||||
configAssert.assertValue(PhpNextgenClientCodegen.SUPPORT_STREAMING, codegen::isSupportStreaming, Boolean.TRUE);
|
||||
Assert.assertEquals(codegen.isSupportStreaming(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception {
|
||||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
||||
output.deleteOnExit();
|
||||
|
||||
OpenAPI openAPI = new OpenAPIParser()
|
||||
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
|
||||
|
||||
codegen.setOutputDir(output.getAbsolutePath());
|
||||
codegen.additionalProperties().put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");
|
||||
|
||||
ClientOptInput input = new ClientOptInput()
|
||||
.openAPI(openAPI)
|
||||
.config(codegen);
|
||||
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
Map<String, File> files = generator.opts(input).generate().stream()
|
||||
.collect(Collectors.toMap(File::getName, Function.identity()));
|
||||
|
||||
List<String> modelContent = Files
|
||||
.readAllLines(files.get("Pet.php").toPath())
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertListContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
|
||||
Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception {
|
||||
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
|
||||
output.deleteOnExit();
|
||||
|
||||
OpenAPI openAPI = new OpenAPIParser()
|
||||
.readLocation("src/test/resources/bugs/issue_20593.yaml", null, new ParseOptions()).getOpenAPI();
|
||||
|
||||
codegen.setOutputDir(output.getAbsolutePath());
|
||||
|
||||
ClientOptInput input = new ClientOptInput()
|
||||
.openAPI(openAPI)
|
||||
.config(codegen);
|
||||
|
||||
DefaultGenerator generator = new DefaultGenerator();
|
||||
Map<String, File> files = generator.opts(input).generate().stream()
|
||||
.collect(Collectors.toMap(File::getName, Function.identity()));
|
||||
|
||||
List<String> modelContent = Files
|
||||
.readAllLines(files.get("Pet.php").toPath())
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertListNotContains(modelContent, a -> a.equals("$color = self::COLOR_UNKNOWN_DEFAULT_OPEN_API;"), "");
|
||||
Assert.assertListContains(modelContent, a -> a.equalsIgnoreCase("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 2.0.0
|
||||
title: test
|
||||
paths:
|
||||
/pets:
|
||||
get:
|
||||
summary: List all pets
|
||||
operationId: listPets
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
components:
|
||||
schemas:
|
||||
Pet:
|
||||
type: object
|
||||
properties:
|
||||
Color:
|
||||
type: string
|
||||
enum: [RED, BLUE, GREEN]
|
Loading…
x
Reference in New Issue
Block a user