Issue #10244 fix php enum default value initialization (#10273)

Co-authored-by: Eric Durand-Tremblay <etremblay@kronostechnologies.com>
This commit is contained in:
Eric Durand-Tremblay 2021-09-01 23:03:31 -04:00 committed by GitHub
parent c148539ce3
commit f9fa62a79e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 2 deletions

View File

@ -631,7 +631,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg
@Override
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "_" + value;
return "self::" + datatype + "_" + value;
}
@Override

View File

@ -32,6 +32,7 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
public class AbstractPhpCodegenTest {
@ -146,6 +147,26 @@ public class AbstractPhpCodegenTest {
Assert.assertTrue(cp1.isPrimitiveType);
}
@Test(description = "Issue #10244")
public void testEnumPropertyWithDefaultValue() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/php/issue_10244.yaml");
final AbstractPhpCodegen codegen = new P_AbstractPhpCodegen();
Schema test1 = openAPI.getComponents().getSchemas().get("ModelWithEnumPropertyHavingDefault");
CodegenModel cm1 = codegen.fromModel("ModelWithEnumPropertyHavingDefault", test1);
// Make sure we got the container object.
Assert.assertEquals(cm1.getDataType(), "object");
Assert.assertEquals(codegen.getTypeDeclaration("MyResponse"), "\\php\\Model\\MyResponse");
// We need to postProcess the model for enums to be processed
codegen.postProcessModels(Collections.singletonMap("models", Collections.singletonList(Collections.singletonMap("model", cm1))));
// Assert the enum default value is properly generated
CodegenProperty cp1 = cm1.vars.get(0);
Assert.assertEquals(cp1.getDefaultValue(), "self::PROPERTY_NAME_VALUE");
}
private static class P_AbstractPhpCodegen extends AbstractPhpCodegen {
@Override
public CodegenType getTag() {

View File

@ -0,0 +1,26 @@
openapi: 3.0.0
info:
title: 'Issue 10224 Enum default value'
version: latest
paths:
'/':
get:
operationId: operation
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/ModelWithEnumPropertyHavingDefault'
components:
schemas:
ModelWithEnumPropertyHavingDefault:
required:
- propertyName
properties:
propertyName:
type: string
default: VALUE
enum:
- VALUE