swagger-api / swagger-api/swagger-codegen

[Python] Discriminator value is converted to lower case

Open
#8,858 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Data deserialization based on a discriminator property does not seem to work correctly if the discriminator values are not all lower case. The discriminator_value_class_map maps the discriminator values as they are to class names (https://github.com/swagger-api/swagger-codegen/blob/7a049041e237099db112d3652ac67220a2566a0f/modules/swagger-codegen/src/main/resources/python/model.mustache#L51), while get_real_child_model converts the discriminator value to lower case and then tries to get the mapping (https://github.com/swagger-api/swagger-codegen/blob/7a049041e237099db112d3652ac67220a2566a0f/modules/swagger-codegen/src/main/resources/python/model.mustache#L172).

Using a swagger file that contains definitions similar to those below, the generated client does not succeed in deserializing Applications with 'PostgreSQL' type as PostgreSQL instances. Removing the call to lower from get_real_child_model seems to solve the problem.

I'm not sure If I'm missing something. Are the discriminators supposed to be all lower case? Is it intended behaviour? Clients generated in other languages do not seem to have the same problem. If this behaviour is not intended removing the call to lower from get_real_child_model could fix the issue.

Swagger-codegen version

2.3.1

Swagger declaration file content or url
"definitions": {
	"Application": {
		"type": "object",
		"discriminator": "type",
		"properties": {
			"name": {
				"type": "string"
			}
		},
		"title": "Application"
	},
	"PostgreSQL": {
		"title": "PostgreSQL",
		"allOf": [
			{
				"$ref": "#/definitions/Application"
			},
			{
				"type": "object",
				"properties": {
					"name": {
						"type": "string"
					}
				},
				"title": "PostgreSQL"
			}
		]
	}
}
Command line used for generation

swagger-codegen generate -l python -i swagger -o client

Suggest a fix/enhancement

Remove the call to lower from get_real_child_model (https://github.com/swagger-api/swagger-codegen/blob/7a049041e237099db112d3652ac67220a2566a0f/modules/swagger-codegen/src/main/resources/python/model.mustache#L172).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/resources/python/model.mustache, especially the get_real_child_model logic around line 172 and the discriminator_value_class_map around line 51. Generate a Python client with the provided discriminator definitions using the shown command, then verify that a 'PostgreSQL' discriminator deserializes to the PostgreSQL class without lowercasing the value.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.