swagger-api / swagger-api/swagger-codegen

[codegen][JAVA] Generated model with both properties and additionalProperties of differing types cannot be deserialized

Open
#8,036 2 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

When a definition in Swagger contains both regular properties (with complex value types) and additionalProperties with type string, then the generated Java model class cannot deserialized by e.g. Jackson since the class extends HashMap<String,String> and it's complex values are being "set" as a String.

A better way of generating the class would probably be that the model class has a Map<String,String> field/member which additional properties are deserialized into.

Swagger-codegen version

2.3.1

Swagger declaration file content or url

Example swagger file where the generated Java class extends HashMap<String,String> which then breaks deserialization of the codes object:

swagger: '2.0'
info:
  version: 1.0.0
  title: Example
paths:
  /foo:
    get:
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/ExampleResponse'
definitions:
  ExampleResponse:
    type: object
    additionalProperties:
      type: string
    properties:
      codes:
        type: object
        description: An object containing various codes
        example:
          foo: bar
          hello: world
Maven config used for generation
			<plugin>
				<groupId>io.swagger</groupId>
				<artifactId>swagger-codegen-maven-plugin</artifactId>
				<version>2.3.1</version>
				<configuration>
					<groupId>${project.groupId}</groupId>
					<language>java</language>
					<generateApis>false</generateApis>
					<library>jersey2</library>
					<configOptions>
						<dateLibrary>java8</dateLibrary>
						<useBeanValidation>true</useBeanValidation>
						<useJaxbAnnotations>true</useJaxbAnnotations>
					</configOptions>
				</configuration>
			</plugin>
Suggest a fix/enhancement

I've worked around this issue in my project by having a customized mustache file without using the parent mustache property. I'm not sure what side-effects that may have for other projects though.

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 by comparing the generated Java model behavior with the customized swagger-model/src/swagger-codegen-templates/pojo.mustache template linked in the report, focusing on the parent mustache property. Reproduce generation from the supplied Swagger definition and verify that Jackson can deserialize both the complex codes property and string additional properties without conflicting types.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.