Bug Report Checklist
Description
Many keywords used by functions that handle input from an OpenAPI specification file are used without being in the reserved keywords list. This causes issues throughout the stack, from compiler errors due to mismatched types, to runtime errors due to recursive references.
In specific, the following local variables are either declared by the Dart mustache files, or are built-in to Dart itself.
- parameterToString
- mapValueOfType
- mapCastOfType
- mapDateTime
- apiClient
- path
- queryParams
- headerParams
- formParams
- contentTypes
- postBody
- requiredKeys
- mp
- hasFields
- other
- hashCode
- fromJson
- toJson
- toString
- encode
- decode
- allowNull
openapi-generator version
Latest commit (7b08aa4) (May 25 2022)
OpenAPI declaration file content or url
openapi: 3.0.3
info:
version: "1.1"
title: Dart Reserved Words
servers:
- url: 'localhost'
variables:
host:
default: localhost
components:
schemas:
ItemWithReservedKeywords:
type: object
properties:
# Dart "Reserved" Keywords
apiClient:
type: int
parameterToString:
type: int
mapValueOfType:
type: int
mapCastOfType:
type: int
mapDateTime:
type: int
json:
type: int
requiredKeys:
type: int
path:
type: int
postBody:
type: int
queryParams:
type: int
headerParams:
type: int
formParams:
type: int
contentTypes:
type: int
mp:
type: int
type:
type: int
hasFields:
type: int
responses:
type: int
responseBody:
type: int
other:
type: int
hashCode:
type: boolean
Map:
type: boolean
values:
type: int
value:
type: int
fromJson:
type: int
toJson:
type: int
result:
type: int
toString:
type: int
encode:
type: int
decode:
type: int
allowNull:
type: int
paths:
/items:
get:
operationId: GetItemWithMapStringObjects
responses:
"200":
description: get the item
content:
application/json:
schema:
$ref: '#/components/schemas/ItemWithReservedKeywords'
post:
operationId: UpdateItemWithMapStringObjects
description: Updates the item
parameters:
- in: query
required: true
name: path
description: path
schema:
type: int
- in: query
required: true
name: postBody
description: path
schema:
type: int
- in: query
required: true
name: contentTypes
description: path
schema:
type: int
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ItemWithReservedKeywords'
responses:
"200":
description: "success"
content:
application/json:
schema:
$ref: '#/components/schemas/ItemWithReservedKeywords'
Generation Details
java -jar \modules\openapi-generator-cli\target\openapi-generator-cli.jar generate -i .\spec.yaml -g dart -o ./keywords
Steps to reproduce
The generated files will be full of compile-time errors, and more insidiously, also full of potentially recursive reference stack overflow errors:
In the following sample alone...
- fromJson will be invalid because it's already a defined instance member
- requiredKeys will be attempting to call .forEach on an integer
- mapValueOfType of will attempt to be called, even though it's not a function anymore
/// Returns a new [ItemWithReservedKeywords] instance and imports its values from
/// [value] if it's a [Map], null otherwise.
// ignore: prefer_constructors_over_static_methods
static ItemWithReservedKeywords? fromJson(dynamic value) {
if (value is Map) {
final json = value.cast<String, dynamic>();
// Ensure that the map contains the required keys.
// Note 1: the values aren't checked for validity beyond being non-null.
// Note 2: this code is stripped in release mode!
assert(() {
requiredKeys.forEach((key) {
assert(json.containsKey(key), 'Required key "ItemWithReservedKeywords[$key]" is missing from JSON.');
assert(json[key] != null, 'Required key "ItemWithReservedKeywords[$key]" has a null value in JSON.');
});
return true;
}());
return ItemWithReservedKeywords(
switch_: mapValueOfType<int>(json, r'switch'),
apiClient: mapValueOfType<int>(json, r'apiClient'),
parameterToString: mapValueOfType<int>(json, r'parameterToString'),
mapValueOfType: mapValueOfType<int>(json, r'mapValueOfType'),
mapCastOfType: mapValueOfType<int>(json, r'mapCastOfType'),
mapDateTime: mapValueOfType<int>(json, r'mapDateTime'),
json: mapValueOfType<int>(json, r'json'),
requiredKeys: mapValueOfType<int>(json, r'requiredKeys'),
path: mapValueOfType<int>(json, r'path'),
postBody: mapValueOfType<int>(json, r'postBody'),
queryParams: mapValueOfType<int>(json, r'queryParams'),
headerParams: mapValueOfType<int>(json, r'headerParams'),
formParams: mapValueOfType<int>(json, r'formParams'),
contentTypes: mapValueOfType<int>(json, r'contentTypes'),
mp: mapValueOfType<int>(json, r'mp'),
type: mapValueOfType<int>(json, r'type'),
hasFields: mapValueOfType<int>(json, r'hasFields'),
responses: mapValueOfType<int>(json, r'responses'),
responseBody: mapValueOfType<int>(json, r'responseBody'),
other: mapValueOfType<int>(json, r'other'),
hashCode: mapValueOfType<bool>(json, r'hashCode'),
map: mapValueOfType<bool>(json, r'Map'),
values: mapValueOfType<int>(json, r'values'),
value: mapValueOfType<int>(json, r'value'),
fromJson: mapValueOfType<int>(json, r'fromJson'),
toJson: mapValueOfType<int>(json, r'toJson'),
result: mapValueOfType<int>(json, r'result'),
toString: mapValueOfType<int>(json, r'toString'),
encode: mapValueOfType<int>(json, r'encode'),
decode: mapValueOfType<int>(json, r'decode'),
allowNull: mapValueOfType<int>(json, r'allowNull'),
);
}
return null;
}
And there's many other stuff obvious and not so obvious errors that will occur.
Suggest a fix
I looked at how csharp-netcore works around the same problem, and they declare all their reserved names in the AbstractCSharpCodegen.java file:
// NOTE: C# uses camel cased reserved words, while models are title cased. We don't want lowercase comparisons.
reservedWords.addAll(
Arrays.asList(
// set "client" as a reserved word to avoid conflicts with Org.OpenAPITools.Client
// this is a workaround and can be removed if c# api client is updated to use
// fully qualified name
"Client", "client", "parameter", "Configuration", "Version",
// local variable names in API methods (endpoints)
"localVarPath", "localVarPathParams", "localVarQueryParams", "localVarHeaderParams",
"localVarFormParams", "localVarFileParams", "localVarStatusCode", "localVarResponse",
"localVarPostBody", "localVarHttpHeaderAccepts", "localVarHttpHeaderAccept",
"localVarHttpContentTypes", "localVarHttpContentType",
"localVarStatusCode",
// C# reserved words
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
"class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else",
"enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for",
"foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock",
"long", "namespace", "new", "null", "object", "operator", "out", "override", "params",
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed",
"short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw",
"true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using",
"virtual", "void", "volatile", "while")
);
(btw, even C# is missing some cases when I tried it. Creating an object named StringBuilder will conflict with System.StringBuilder, for instance.)
AbstractDartCodegen doesn't do this via encoding it into the java array directly, but it does have its own way to handle this using an included dart-keywords.txt file.
At the moment that file only includes keywords owned by the Dart language itself (like return, class, etc), but we should consider adding all other keywords to this file as well.
Bug Report Checklist
Description
Many keywords used by functions that handle input from an OpenAPI specification file are used without being in the reserved keywords list. This causes issues throughout the stack, from compiler errors due to mismatched types, to runtime errors due to recursive references.
In specific, the following local variables are either declared by the Dart mustache files, or are built-in to Dart itself.
openapi-generator version
Latest commit (7b08aa4) (May 25 2022)
OpenAPI declaration file content or url
Generation Details
Steps to reproduce
The generated files will be full of compile-time errors, and more insidiously, also full of potentially recursive reference stack overflow errors:
In the following sample alone...
And there's many other stuff obvious and not so obvious errors that will occur.
Suggest a fix
I looked at how
csharp-netcoreworks around the same problem, and they declare all their reserved names in theAbstractCSharpCodegen.javafile:(btw, even C# is missing some cases when I tried it. Creating an object named
StringBuilderwill conflict with System.StringBuilder, for instance.)AbstractDartCodegendoesn't do this via encoding it into the java array directly, but it does have its own way to handle this using an includeddart-keywords.txtfile.At the moment that file only includes keywords owned by the Dart language itself (like
return,class, etc), but we should consider adding all other keywords to this file as well.