Skip to content

Commit

Permalink
discriminator.mapping for PHP template
Browse files Browse the repository at this point in the history
  • Loading branch information
netfarma committed Oct 24, 2018
1 parent 76aedca commit 8ac8020
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,9 @@ public CodegenModel fromModel(String name, Schema schema, Map<String, Schema> al
Map<String, Schema> allProperties;
List<String> allRequired;

//composed schema can also have properties
addVars(m, unaliasPropertySchema(allDefinitions, schema.getProperties()), schema.getRequired());

if (supportsInheritance || supportsMixins) {
allProperties = new LinkedHashMap<String, Schema>();
allRequired = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.mustache.UppercaseLambda;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -65,6 +66,7 @@ public PhpClientCodegen() {
Collections.sort(sortedLanguageSpecificPrimitives);
String primitives = "'" + StringUtils.join(sortedLanguageSpecificPrimitives, "', '") + "'";
additionalProperties.put("primitives", primitives);
additionalProperties.put("uppercase", new UppercaseLambda());

cliOptions.add(new CliOption(COMPOSER_VENDOR_NAME, "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. yaypets. IMPORTANT NOTE (2016/03): composerVendorName will be deprecated and replaced by gitUserId in the next openapi-generator release"));
cliOptions.add(new CliOption(COMPOSER_PROJECT_NAME, "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. petstore-client. IMPORTANT NOTE (2016/03): composerProjectName will be deprecated and replaced by gitRepoId in the next openapi-generator release"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,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 = $class::getClassByDiscriminatorValue($data->{$discriminator});
if (is_subclass_of($subclass, $class)) {
$class = $subclass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
*/
protected static $openAPIModelName = '{{name}}';

/**
* Array of property to discriminator type mappings. Used for (de)serialization
*
* @var string[]
*/
protected static $discriminatorMapping = [
{{#discriminator.mappedModels}}
'{{#uppercase}}{{mappingName}}{{/uppercase}}' => {{{modelName}}}::class,
{{/discriminator.mappedModels}}
];

/**
* Array of property to type mappings. Used for (de)serialization
*
Expand Down Expand Up @@ -399,6 +410,20 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
unset($this->container[$offset]);
}

/**
* @param $discriminator
* @return string
*/
public static function getClassByDiscriminatorValue($discriminator)
{
$discriminatorKey = strtoupper($discriminator);
if(isset(self::$discriminatorMapping[$discriminatorKey])) {
return self::$discriminatorMapping[$discriminatorKey];
}

return $discriminator;
}

/**
* Gets the string presentation of the object
*
Expand Down

0 comments on commit 8ac8020

Please sign in to comment.