Demonstrates how the OpenAPI tools Dart SDK generator will incorrectly shadow field variables named json in the generated toJson() method.
This bug occurs as of April 13, 2022 using the current 6.0.0 master branch of openapi-generator
Tested using openapi-generator-cli-6.0.0-20220412.074015-127.jar (Tue Apr 12 07:40:21 UTC 2022)
- clone the repo
- cd into it
- Run the generator jar with the shadowspec.yaml as input and dart as the language
java -jar openapi-generator-cli-6.0.0-20220412.074015-127.jar generate -i .\shadowspec.yaml -g dart -o shadowed
- Open
./shadowed/lib/model/item_with_field_named_json.dartand find thetoJson()method
shadowspec.yaml
openapi: 3.0.3
info:
version: "1.0"
title: Dart Shadow Json Demo
servers:
- url: 'localhost'
variables:
host:
default: localhost
components:
schemas:
ItemWithFieldNamedJson:
type: object
properties:
json:
type: object
additionalProperties: {}
paths:
/:
get:
operationId: shadow
responses:
'200':
description: produces a shadowed json field when generated in dart
content:
application/json:
schema:
$ref: '#/components/schemas/ItemWithFieldNamedJson'
shadowed/lib/model/item_with_field_named_json.dart
Some fields from the generated file below have been omitted for brevity
class ItemWithFieldNamedJson {
ItemWithFieldNamedJson({
this.json = const {},
});
Map<String, Object> json;
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
json[r'json'] = json;
return json;
}
final json = <String, dynamic> is a shadowed variable over the field variable Map<String, Object> json causing serialization to end up in an cyclic pointer reference.