Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discriminator name not following variable naming convention #1742

Merged
merged 5 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -235,6 +235,7 @@ public Map<String, Object> updateAllModels(Map<String, Object> objs) {
allModels.put(modelName, cm);
}
}

// Fix up all parent and interface CodegenModel references.
for (CodegenModel cm : allModels.values()) {
if (cm.getParent() != null) {
Expand All @@ -250,6 +251,7 @@ public Map<String, Object> updateAllModels(Map<String, Object> objs) {
}
}
}

// Let parent know about all its children
for (String name : allModels.keySet()) {
CodegenModel cm = allModels.get(name);
Expand Down Expand Up @@ -1820,7 +1822,7 @@ private CodegenDiscriminator createDiscriminator(String schemaName, Schema
return null;
}
CodegenDiscriminator discriminator = new CodegenDiscriminator();
discriminator.setPropertyName(schema.getDiscriminator().getPropertyName());
discriminator.setPropertyName(toVarName(schema.getDiscriminator().getPropertyName()));
discriminator.setMapping(schema.getDiscriminator().getMapping());
if (schema.getDiscriminator().getMapping() != null && !schema.getDiscriminator().getMapping().isEmpty()) {
for (Entry<String, String> e : schema.getDiscriminator().getMapping().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void testLeadingSlashIsAddedIfMissing() {

private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
CodegenDiscriminator test = new CodegenDiscriminator();
test.setPropertyName("$_type");
test.setPropertyName("DollarUnderscoretype");
test.setMapping(new HashMap<>());
test.getMapping().put("a", "#/components/schemas/Adult");
test.getMapping().put("c", "#/components/schemas/Child");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ components:
firstName:
type: string
duplicated_optional:
type: string
type: integer
duplicated_required:
type: string
type: integer
person_required:
type: string
format: date-time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Org.OpenAPITools.Model
/// Animal
/// </summary>
[DataContract]
[JsonConverter(typeof(JsonSubtypes), "className")]
[JsonConverter(typeof(JsonSubtypes), "ClassName")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Dog")]
[JsonSubtypes.KnownSubType(typeof(Cat), "Cat")]
public partial class Animal : IEquatable<Animal>, IValidatableObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
class Animal implements ModelInterface, ArrayAccess
{
const DISCRIMINATOR = 'className';
const DISCRIMINATOR = 'class_name';

/**
* The original name of the model.
Expand Down Expand Up @@ -186,7 +186,7 @@ public function __construct(array $data = null)
$this->container['color'] = isset($data['color']) ? $data['color'] : 'red';

// Initialize discriminator property with the model name.
$discriminator = array_search('className', self::$attributeMap, true);
$discriminator = array_search('class_name', self::$attributeMap, true);
$this->container[$discriminator] = static::$openAPIModelName;
}

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/ruby/lib/petstore/models/animal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.openapi_types

# discriminator's property name in OpenAPI v3
def self.openapi_discriminator_name
:'className'
:'class_name'
end

# Initializes the object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
class Animal implements ModelInterface, ArrayAccess
{
const DISCRIMINATOR = 'className';
const DISCRIMINATOR = 'class_name';

/**
* The original name of the model.
Expand Down Expand Up @@ -186,7 +186,7 @@ public function __construct(array $data = null)
$this->container['color'] = isset($data['color']) ? $data['color'] : 'red';

// Initialize discriminator property with the model name.
$discriminator = array_search('className', self::$attributeMap, true);
$discriminator = array_search('class_name', self::$attributeMap, true);
$this->container[$discriminator] = static::$openAPIModelName;
Copy link
Contributor

@ackintosh ackintosh Dec 23, 2018

Choose a reason for hiding this comment

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

https://travis-ci.org/OpenAPITools/openapi-generator/builds/471525982?utm_source=github_status&utm_medium=notification

1) OpenAPI\Client\ModelInheritanceTest::testDiscriminatorInitialization
Failed asserting that null is identical to 'Dog'.
/home/travis/build/OpenAPITools/openapi-generator/samples/client/petstore/php/OpenAPIClient-php/tests/ModelInheritanceTest.php:70

+       $discriminator = array_search('class_name', self::$attributeMap, true);
        $this->container[$discriminator] = static::$openAPIModelName;	

Due to this PR, array_search may become unnecessary. We can pass {{discriminatorName}} to $this->container directly as following.

  • Mustache template

$this->container[{{discriminatorName}}] = static::$openAPIModelName;

  • PHP code
-       $discriminator = array_search('class_name', self::$attributeMap, true);
+       $this->container['class_name'] = static::$openAPIModelName;	

Copy link
Member Author

Choose a reason for hiding this comment

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

👌 I'll make the change later tonight.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def self.openapi_types

# discriminator's property name in OpenAPI v3
def self.openapi_discriminator_name
:'className'
:'class_name'
end

# Initializes the object
Expand Down