Skip to content

Commit 99783e5

Browse files
committed
[php-nextgen] Discriminator class detection uses wrong namespace - tests
1 parent 93c7b0b commit 99783e5

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,45 @@ public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception
119119
Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
120120
}
121121

122+
@Test
123+
public void testDiscriminatorUsesModelPackageNamespace() throws Exception {
124+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
125+
output.deleteOnExit();
126+
127+
OpenAPI openAPI = new OpenAPIParser()
128+
.readLocation("src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI();
129+
130+
codegen.setOutputDir(output.getAbsolutePath());
131+
// Set invokerPackage="MyApp" and modelPackage="Entities" (relative suffix).
132+
// AbstractPhpCodegen.processOpts() will produce final modelPackage = "MyApp\Entities".
133+
// The old bug would have emitted '\MyApp\Model\' (invokerPackage + \Model\).
134+
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "MyApp");
135+
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "Entities");
136+
137+
ClientOptInput input = new ClientOptInput()
138+
.openAPI(openAPI)
139+
.config(codegen);
140+
141+
DefaultGenerator generator = new DefaultGenerator();
142+
Map<String, File> files = generator.opts(input).generate().stream()
143+
.collect(Collectors.toMap(File::getName, Function.identity()));
144+
145+
List<String> objectSerializerContent = Files
146+
.readAllLines(files.get("ObjectSerializer.php").toPath())
147+
.stream()
148+
.map(String::trim)
149+
.collect(Collectors.toList());
150+
151+
// The discriminator subclass lookup must use modelPackage (\MyApp\Entities\),
152+
// NOT invokerPackage + '\Model' (\MyApp\Model\).
153+
Assert.assertListContains(objectSerializerContent,
154+
a -> a.contains("'\\MyApp\\Entities\\\\'"),
155+
"ObjectSerializer discriminator subclass lookup must use modelPackage namespace");
156+
Assert.assertListNotContains(objectSerializerContent,
157+
a -> a.contains("'\\MyApp\\Model\\\\'"),
158+
"ObjectSerializer discriminator must NOT use invokerPackage\\Model namespace");
159+
}
160+
122161
@Test
123162
public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception {
124163
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

modules/openapi-generator/src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,3 +2291,22 @@ components:
22912291
description: "Optional array of multiple errors encountered during processing"
22922292
required:
22932293
- error
2294+
2295+
DiscriminatorBase:
2296+
type: object
2297+
discriminator:
2298+
propertyName: type
2299+
required:
2300+
- type
2301+
properties:
2302+
type:
2303+
type: string
2304+
2305+
DiscriminatorChild:
2306+
allOf:
2307+
- $ref: '#/components/schemas/DiscriminatorBase'
2308+
- type: object
2309+
properties:
2310+
childProperty:
2311+
type: string
2312+

0 commit comments

Comments
 (0)