swagger-api / swagger-api/swagger-codegen
[Dart] Object type not treated in a sensible manner
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Swagger-codegen version
2.0
Swagger declaration file content or url
Abc:
type: object
required:
- A
- B
- C
properties:
A:
type: integer
example: 0
B:
type: integer
example: 0
C:
type: object
-->
Command line used for generation
java -jar swagger-codegen-cli.jar generate -l dart -i swagger.yaml -o . --additional-properties browserClient=false
Steps to reproduce
Run command above
Observe the class generated has Object.fromJson() (which is invalid)
Suggest a fix/enhancement
I have a fix in my codebase to work around this where instead of an object being mapped to a dart 'object' I map it to a 'dynamic' type, and make dynamic a complex type - this results in the following syntax being generated:
class ABC {
int A = null;
int B = null;
dynamic C = {};
ABC.fromJson(Map<String, dynamic> json) {
if (json == null) return;
A =
json['A']
;
B =
json['B']
;
C =
json['C']
;
}
Which makes more sense to me as I can access the raw object and do any additional parsing by getting a reference to ABC.C and looking at the data as a map. In this specific example C can be one of a number of different classes so it means I can do
if (c['type'] == "A") {
return A.fromJson(c as Map<String, dynamic>)
}
As I say - I have the fix already in a branch, looking to see if this is a problematic approach for anyone
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
Start by running the listed swagger-codegen command with the provided swagger.yaml declaration and inspect the generated ABC class. Confirm how an untyped object property is represented and whether the generated Dart is valid; done means the output handles property C without an invalid Object.fromJson() call.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100