Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class ObjectSerializer
// If a discriminator is defined and points to a valid subclass, use it.
$discriminator = $class::DISCRIMINATOR;
if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) {
$subclass = '\{{invokerPackage}}\Model\\' . $data->{$discriminator};
$subclass = '\{{modelPackage}}\\' . $data->{$discriminator};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR

what about adding a test schema in modules/openapi-generator/src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml to cover this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry for the late reply. My poor brain forgot about it completly. Tests commited.

if (is_subclass_of($subclass, $class)) {
$class = $subclass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,45 @@ public void testEnumUnknownDefaultCaseDeserializationEnabled() throws Exception
Assert.assertListNotContains(modelContent, a -> a.equals("\"Invalid value '%s' for 'color', must be one of '%s'\","), "");
}

@Test
public void testDiscriminatorUsesModelPackageNamespace() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/php-nextgen/petstore-with-fake-endpoints-models-for-testing.yaml", null, new ParseOptions()).getOpenAPI();

codegen.setOutputDir(output.getAbsolutePath());
// Set invokerPackage="MyApp" and modelPackage="Entities" (relative suffix).
// AbstractPhpCodegen.processOpts() will produce final modelPackage = "MyApp\Entities".
// The old bug would have emitted '\MyApp\Model\' (invokerPackage + \Model\).
codegen.additionalProperties().put(CodegenConstants.INVOKER_PACKAGE, "MyApp");
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, "Entities");

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> objectSerializerContent = Files
.readAllLines(files.get("ObjectSerializer.php").toPath())
.stream()
.map(String::trim)
.collect(Collectors.toList());

// The discriminator subclass lookup must use modelPackage (\MyApp\Entities\),
// NOT invokerPackage + '\Model' (\MyApp\Model\).
Assert.assertListContains(objectSerializerContent,
a -> a.contains("'\\MyApp\\Entities\\\\'"),
"ObjectSerializer discriminator subclass lookup must use modelPackage namespace");
Assert.assertListNotContains(objectSerializerContent,
a -> a.contains("'\\MyApp\\Model\\\\'"),
"ObjectSerializer discriminator must NOT use invokerPackage\\Model namespace");
}

@Test
public void testEnumUnknownDefaultCaseDeserializationDisabled() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2291,3 +2291,22 @@ components:
description: "Optional array of multiple errors encountered during processing"
required:
- error

DiscriminatorBase:
type: object
discriminator:
propertyName: type
required:
- type
properties:
type:
type: string

DiscriminatorChild:
allOf:
- $ref: '#/components/schemas/DiscriminatorBase'
- type: object
properties:
childProperty:
type: string

Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,29 @@ import {
export class TestApi extends runtime.BaseAPI {

/**
* Creates request options for test without sending the request
*/
async testRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Array<TestBaseDto>>> {
async testRequestOpts(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.RequestOpts> {
const queryParameters: any = {};

const headerParameters: runtime.HTTPHeaders = {};


let urlPath = `/api/v1/test`;

const response = await this.request({
return {
path: urlPath,
method: 'GET',
headers: headerParameters,
query: queryParameters,
}, initOverrides);
};
}

/**
*/
async testRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<Array<TestBaseDto>>> {
const requestConfig = await this.testRequestConfig(initOverrides);
const response = await this.request(requestConfig, initOverrides);

return new runtime.JSONApiResponse(response, (jsonValue) => jsonValue.map(TestBaseDtoFromJSON));
}
Expand Down