Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
12ba383
Dart2 : generate sample from more complex schema
agilob Oct 13, 2020
721b451
Delete old, unused dart samples
agilob Oct 13, 2020
7a30cb1
Merge branch 'master' of github.com:agilob/openapi-generator into dar…
agilob Oct 22, 2020
4151545
Re-add samples/client/petstore/dart2/petstore/pom.xml
agilob Oct 27, 2020
499f67d
Move dart-petstore pom.xml to new location
agilob Oct 27, 2020
63be2c5
Update root pom.xml
agilob Oct 29, 2020
04437d0
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Oct 31, 2020
6d61f8b
Sanitize dart class names
agilob Nov 1, 2020
1ed4eab
Merge branch 'master' of github.com:agilob/openapi-generator into dar…
agilob Nov 4, 2020
cc159f0
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Nov 4, 2020
1398db1
Update after multipart fixes
agilob Nov 4, 2020
7b86f57
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Nov 14, 2020
181845b
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Nov 22, 2020
066d113
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Nov 23, 2020
864abb9
Better dart name sanitaztion for enums
agilob Nov 23, 2020
4b72deb
Merge branch 'master' of github.com:OpenAPITools/openapi-generator in…
agilob Dec 3, 2020
043912f
Merge branch 'dart-more-complex-schema' of github.com:agilob/openapi-…
agilob Dec 3, 2020
24e42ba
Fix generation of enum from special characters
agilob Dec 3, 2020
0c87161
Fix dart model tests
agilob Dec 3, 2020
ada309c
Dart: Add more tests to enum name generator
agilob Dec 3, 2020
10d1af9
Dart: add mockito to dependencies
agilob Dec 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/configs/dart-petstore_client_lib.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generatorName: dart
outputDir: samples/client/petstore/dart2/petstore_client_lib
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml
outputDir: samples/client/petstore/dart2/petstore_client_lib_fake_endpoints
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/dart2
additionalProperties:
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import com.google.common.collect.Sets;

Expand Down Expand Up @@ -109,8 +104,8 @@ public DartClientCodegen() {
)
);

// clear import mapping (from default generator) as dart does not use it at the moment
importMapping.clear();
importMapping.put("List", "dart.core.*");
importMapping.put("Map", "dart.core.*");

outputFolder = "generated-code/dart";
modelTemplateFiles.put("model.mustache", ".dart");
Expand Down Expand Up @@ -139,11 +134,11 @@ public DartClientCodegen() {
setReservedWordsLowerCase(reservedWordsList);

languageSpecificPrimitives = Sets.newHashSet(
"String",
"bool",
"int",
"num",
"double"
"String",
"bool",
"int",
"num",
"double"
);
instantiationTypes.put("array", "List");
instantiationTypes.put("map", "Map");
Expand Down Expand Up @@ -344,6 +339,11 @@ public String toVarName(String name) {
return name;
}

// numbers are not allowed at the beginning
if (name.matches("^\\d.*")) {
name = "_" + name;
}

// camelize (lower first character) the variable name
// pet_id => petId
name = camelize(name, true);
Expand Down Expand Up @@ -420,7 +420,7 @@ public String toDefaultValue(Schema schema) {

if (schema.getDefault() != null) {
if (ModelUtils.isStringSchema(schema)) {
return "'" + schema.getDefault().toString().replaceAll("'", "\\'") + "'";
return surroundWithApostrophe(schema.getDefault().toString().replaceAll("'", "\\'"));
}
return schema.getDefault().toString();
} else {
Expand Down Expand Up @@ -509,11 +509,7 @@ private boolean buildEnumFromVendorExtension(CodegenModel cm) {
List<Map<String, String>> enumVars = new ArrayList<>();
for (Map<String, Object> value : values) {
Map<String, String> enumVar = new HashMap<>();
String name = camelize((String) value.get("identifier"), true);
if (isReservedWord(name)) {
name = escapeReservedWord(name);
}
enumVar.put("name", name);
enumVar.put("name", toEnumVarName((String) value.get("identifier"), cm.dataType));
enumVar.put("value", toEnumValue(value.get("numericValue").toString(), cm.dataType));
if (value.containsKey("description")) {
enumVar.put("description", value.get("description").toString());
Expand All @@ -525,28 +521,58 @@ private boolean buildEnumFromVendorExtension(CodegenModel cm) {
}

@Override
public String toEnumVarName(String value, String datatype) {
if (value.length() == 0) {
return "empty";
public String toEnumVarName(String name, String datatype) {
if (name.length() == 0) {
return camelize("empty");
}
String var = value.replaceAll("\\W+", "_");
if ("number".equalsIgnoreCase(datatype) ||
"int".equalsIgnoreCase(datatype)) {
var = "Number" + var;

// for symbol, e.g. $, #
if (getSymbolName(name) != null) {
String symbolName = getSymbolName(name);
symbolName = camelize(underscore(symbolName));
return symbolName;
}

// number
if ("number".equals(datatype)) {
String varName = "NUMBER_" + name;

varName = varName.replaceAll("-", "MINUS_");
varName = varName.replaceAll("\\+", "PLUS_");
varName = varName.replaceAll("\\.", "_DOT_");
return varName;
}

// string
String enumName = sanitizeName(name);
enumName = enumName.replaceFirst("^_", "");
enumName = enumName.replaceFirst("_$", "");

enumName = camelize(underscore((enumName)));

if (enumName.matches("\\d.*")) { // starts with number
return "_" + enumName;
} else {
return enumName;
}
return escapeReservedWord(camelize(var, true));
}

@Override
public String toEnumValue(String value, String datatype) {
if ("number".equalsIgnoreCase(datatype) ||
"int".equalsIgnoreCase(datatype)) {
return value;
} else if(getSymbolName(value) != null) {
return surroundWithApostrophe(getSymbolName(value));
} else {
return "'" + escapeText(value) + "'";
return surroundWithApostrophe(escapeText(value));
}
}

private String surroundWithApostrophe(final String value) {
return "'" + value + "'";
}

@Override
public String toOperationId(String operationId) {
// method name cannot use reserved keyword, e.g. return
Expand All @@ -556,7 +582,7 @@ public String toOperationId(String operationId) {
return newOperationId;
}

return camelize(operationId, true);
return camelize(toVarName(sanitizeName(operationId)), true);
}

public void setPubLibrary(String pubLibrary) {
Expand Down Expand Up @@ -617,13 +643,13 @@ public void postProcessFile(File file, String fileType) {
return; // skip if DART_POST_PROCESS_FILE env variable is not defined
}

// only process the following type (or we can simply rely on the file extension to check if it's a Dart file)
// only procees the following type (or we can simply rely on the file extension to check if it's a Dart file)
Set<String> supportedFileType = Sets.newHashSet(
"supporting-mustache",
"model-test",
"model",
"api-test",
"api");
"supporting-mustache",
"model-test",
"model",
"api-test",
"api");
if (!supportedFileType.contains(fileType)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class {{{classname}}} {
Object postBody{{#bodyParam}} = {{{paramName}}}{{/bodyParam}};

final queryParams = <QueryParam>[];
final headerParams = <String, String>{};
final headerParams = <String, dynamic>{};
final formParams = <String, String>{};
{{#hasQueryParams}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ dependencies:
meta: '^1.1.8'
dev_dependencies:
test: '^1.3.0'
mockito: '^4.1.3'
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,16 @@ public void modelNameTest(String name, String expectedName) {
}

@Test(description = "test enum variable names for reserved words")
public void testReservedWord() throws Exception {
public void testReservedWordInEnumVarName() {
final DefaultCodegen codegen = new DartClientCodegen();
Assert.assertEquals(codegen.toEnumVarName("public", null), "public_");
Assert.assertEquals(codegen.toEnumVarName("Private", null), "private_");
Assert.assertEquals(codegen.toEnumVarName("IF", null), "iF_");
// should not escape non-reserved
Assert.assertEquals(codegen.toEnumVarName("hello", null), "hello_");
Assert.assertEquals(codegen.toEnumVarName("public", null), "Public");
Assert.assertEquals(codegen.toEnumVarName("Private", null), "Private");
Assert.assertEquals(codegen.toEnumVarName("IF", null), "If");
Assert.assertEquals(codegen.toEnumVarName("hello", null), "Hello");
Assert.assertEquals(codegen.toEnumVarName("2", "number"), "NUMBER_2");
Assert.assertEquals(codegen.toEnumVarName(">=", "string"), "GreaterThanOrEqualTo");
Assert.assertEquals(codegen.toEnumVarName("null", "string"), "Null");
Assert.assertEquals(codegen.toEnumVarName("", "string"), "Empty");
}

// datetime (or primitive type) not yet supported in HTTP request body
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@
<module>samples/client/petstore/javascript-promise-es6</module>
<module>samples/server/petstore/go-api-server</module>
<module>samples/server/petstore/go-gin-api-server</module>
<module>samples/client/petstore/dart2/petstore</module>
<module>samples/client/petstore/dart2/petstore_client_lib_fake_endpoints</module>
<module>samples/client/petstore/dart-dio</module>
<module>samples/client/petstore/dart-jaguar/openapi</module>
<module>samples/client/petstore/dart-jaguar/flutter_petstore/openapi</module>
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/dart-dio/lib/model/pet.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart.core.*';
import 'package:openapi/model/tag.dart';
import 'package:built_collection/built_collection.dart';
import 'package:openapi/model/category.dart';
Expand Down
32 changes: 0 additions & 32 deletions samples/client/petstore/dart2/openapi/.openapi-generator/FILES

This file was deleted.

117 changes: 0 additions & 117 deletions samples/client/petstore/dart2/openapi/README.md

This file was deleted.

Loading