swagger-api / swagger-api/swagger-codegen
[JAVA] using `additionalProperties` does not generate a Map
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When I generate a schemas model with additionalProperties in addition to other defined properties, there is no ability to set the additional properties in the generated client code - i.e. there's no Map.
In previous versions, I understand that the generated model object would extend HashMap.
The code that's generated is:
public class StorageNode {
@SerializedName("lib")
private String lib = null;
@SerializedName("path")
private String path = null;
@SerializedName("fields")
private StorageNodeFields fields = null;
...
}
public class StorageNodeFields {
@SerializedName("NAME")
private String NAME = null;
@SerializedName("DESC")
private String DESC = null;
@SerializedName("COMPLEX")
private StorageNodeFieldsCOMPLEX COMPLEX = null;
...
}
Swagger-codegen version
swagger-codegen-cli 3.0.5
Swagger declaration file content or url
openapi: 3.0.1
info:
title: Map-test
description: 'fdsa'
version: 0.0.0
components:
schemas:
StorageNode:
type: object
properties:
lib:
type: string
path:
type: string
fields:
type: object
properties:
NAME:
type: string
DESC:
type: string
COMPLEX:
type: object
properties:
x:
type: integer
y:
type: integer
size:
type: integer
desc:
type: string
additionalProperties:
type: object
(Note that an NPE may be thrown because there are no paths defined - however the schema should still generate. With paths defined the problem is still exhibited - I just wanted to cut this down to the minimum reproducible case).
Command line used for generation
I used gradle generateSwaggerCodeMapTest using https://github.com/int128/gradle-swagger-generator-plugin
with:
swaggerSources {
maptest {
inputFile = file('src/main/resources/map-test.yaml')
code {
language = 'java'
}
}
}
Steps to reproduce
gradle generateSwaggerCodeMapTest generates the code the same every time.
Related issues/PRs
There appear to be many questions regarding Maps generation, but they change with different versions. It looks like in previous versions StorageNodeFields would extend HashMap.
Suggest a fix/enhancement
Either this is achieved via inheritance (as before, it seems) or composition (include a Map inside StorageNodeFields and add getters/setters to write to it).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run gradle generateSwaggerCodeMapTest with the provided map-test.yaml and inspect the generated StorageNodeFields model. Trace how the Java generator handles additionalProperties; done means the generated model exposes those properties through a usable Map while retaining its defined fields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100