-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
Description
I'm seeing unexpected behavior when Swagger generated client code parses a response. The response object is defined to have a set of fixed properties. It also has a set of dynamic properties (not known until run time). I'm using additionalProperties to indicate the object has dynamic properties.
For example, consider the following response object:
{
"@entryid": "xxx",
"@noteid": "1139E",
"fldText": "This is a test"
}The first two properties (@entryid and @noteid) are a fixed part of the schema. The third property (fldText) is dynamic. I expect Swagger Codegen to generate getter methods for the first properties (which it does) and I expect to use the HashMap interface to read any dynamic properties.
The problem is the generated getter methods don't work. I have to read all properties via the HashMap. More details below in the Swagger declaration section.
Swagger-codegen version
Version 2.2.2. I don't know if this is a regression.
Swagger declaration file content or url
Here's an excerpt from my OpenAPI spec:
viewEntry:
type: object
properties:
'@entryid':
description: |
Position of the entry in the view or folder and the universal
ID of any associated document.
type: string
'@noteid':
description: |
The note ID of the document associated with the entry, or an
empty string if the entry is a category or total.
type: string
additionalProperties:
type: objectWhen I generate a Java client I get a ViewEntry class. As expected it extends HashMap so it can hold dynamic properties:
public class ViewEntry extends HashMap<String, Object> {Also as expected it includes getters and setters for the fixed properties:
public String getEntryid() {
return entryid;
}
public void setEntryid(String entryid) {
this.entryid = entryid;
}However, when the Swagger client receives a response it doesn't parse the fixed properties as expected. In other words entry.getEntryid() always returns null. To read the value of @entryid I have to use the HashMap interface -- entry.get("@entryid").
If I remove additionalProperties from the API definition and generate the client again, the getters do work as expected. Of course, this isn't a suitable work-around because ViewEntry no longer extends HashMap and my dynamic properties are lost during response parsing.
Command line used for generation
generate -i /{specs}/data.yaml -l java -o /{clients}/data/java
Steps to reproduce
Since this is a runtime problem you need my API to reproduce. Unfortunately, the API isn't publicly available. Hopefully, you can diagnose the problem based on the information already provided.
Related issues
I couldn't find a similar issue.
Suggest a Fix
One way to fix the problem would be to change the generated getters to delegate to the HashMap. For example, instead of this:
public String getEntryid() {
return entryid;
}Generate this:
public String getEntryid() {
return get("@entryid");
}And of course make a similar change to the setters.