OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Dart] Many keywords are implicitly reserved without being in the listed reserved keywords file
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
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 (7b08aa408d6b3e039746eb1c036675b4bea4317c) (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.
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 reading AbstractDartCodegen and the included dart-keywords.txt file, then reproduce the issue with the supplied OpenAPI specification and Dart generation command. Check the generated model and API files for collisions with Dart built-ins and generator-local names; done means the listed names are handled and the generated Dart no longer has the reported compile-time or recursive-reference failures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, java
- Domain
- devtools, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100