Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
[dart] Support/Fix sourceFolder parameter and docs
* allow passing the sourceFolder to generators
* not sure how this translates to the dart2 generator due to the `part` files
* fix doc generation not showing default value of CLI options
  • Loading branch information
kuhnroyal committed Apr 13, 2022
commit d76df7ef3cf1973a429afbce3e30eb41d7bec9d6
18 changes: 9 additions & 9 deletions docs/generators/dart-dio-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C#have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|pubAuthor|Author name in generated pubspec| |null|
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
|pubDescription|Description in generated pubspec| |null|
|pubHomepage|Homepage in generated pubspec| |null|
|pubLibrary|Library name in generated code| |null|
|pubName|Name in generated pubspec| |null|
|pubVersion|Version in generated pubspec| |null|
|pubAuthor|Author name in generated pubspec| |Author|
|pubAuthorEmail|Email address of the author in generated pubspec| |author@homepage|
|pubDescription|Description in generated pubspec| |OpenAPI API client|
|pubHomepage|Homepage in generated pubspec| |homepage|
|pubLibrary|Library name in generated code| |openapi.api|
|pubName|Name in generated pubspec| |openapi|
|pubVersion|Version in generated pubspec| |1.0.0|
|serializationLibrary|Specify serialization library|<dl><dt>**built_value**</dt><dd>[DEFAULT] built_value</dd></dl>|built_value|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|sourceFolder|Source folder for generated code| |null|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |null|
|sourceFolder|source folder for generated code| |src|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |false|

## IMPORT MAPPING

Expand Down
18 changes: 9 additions & 9 deletions docs/generators/dart-dio.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C#have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
|nullableFields|Make all fields nullable in the JSON payload| |null|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|pubAuthor|Author name in generated pubspec| |null|
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
|pubDescription|Description in generated pubspec| |null|
|pubHomepage|Homepage in generated pubspec| |null|
|pubLibrary|Library name in generated code| |null|
|pubName|Name in generated pubspec| |null|
|pubVersion|Version in generated pubspec| |null|
|pubAuthor|Author name in generated pubspec| |Author|
|pubAuthorEmail|Email address of the author in generated pubspec| |author@homepage|
|pubDescription|Description in generated pubspec| |OpenAPI API client|
|pubHomepage|Homepage in generated pubspec| |homepage|
|pubLibrary|Library name in generated code| |openapi.api|
|pubName|Name in generated pubspec| |openapi|
|pubVersion|Version in generated pubspec| |1.0.0|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|sourceFolder|Source folder for generated code| |null|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |null|
|sourceFolder|source folder for generated code| |src|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |false|

## IMPORT MAPPING

Expand Down
18 changes: 9 additions & 9 deletions docs/generators/dart.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
|legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C#have this enabled by default).|<dl><dt>**true**</dt><dd>The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.</dd><dt>**false**</dt><dd>The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.</dd></dl>|true|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|pubAuthor|Author name in generated pubspec| |null|
|pubAuthorEmail|Email address of the author in generated pubspec| |null|
|pubDescription|Description in generated pubspec| |null|
|pubHomepage|Homepage in generated pubspec| |null|
|pubLibrary|Library name in generated code| |null|
|pubName|Name in generated pubspec| |null|
|pubVersion|Version in generated pubspec| |null|
|pubAuthor|Author name in generated pubspec| |Author|
|pubAuthorEmail|Email address of the author in generated pubspec| |author@homepage|
|pubDescription|Description in generated pubspec| |OpenAPI API client|
|pubHomepage|Homepage in generated pubspec| |homepage|
|pubLibrary|Library name in generated code| |openapi.api|
|pubName|Name in generated pubspec| |openapi|
|pubVersion|Version in generated pubspec| |1.0.0|
|serializationLibrary|Specify serialization library|<dl><dt>**native_serialization**</dt><dd>Use native serializer, backwards compatible</dd></dl>|native_serialization|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|sourceFolder|Source folder for generated code| |null|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |null|
|sourceFolder|source folder for generated code| |src|
|useEnumExtension|Allow the 'x-enum-values' extension for enums| |false|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public abstract class AbstractDartCodegen extends DefaultCodegen {
protected String pubAuthorEmail = "author@homepage";
protected String pubHomepage = "homepage";
protected boolean useEnumExtension = false;
protected String sourceFolder = "";
protected String sourceFolder = "src";
protected String libPath = "lib" + File.separator;
protected String apiDocPath = "doc" + File.separator;
protected String modelDocPath = "doc" + File.separator;
protected String apiTestPath = "test" + File.separator;
Expand Down Expand Up @@ -99,8 +100,8 @@ public AbstractDartCodegen() {
modelTemplateFiles.put("model.mustache", ".dart");
apiTemplateFiles.put("api.mustache", ".dart");
embeddedTemplateDir = templateDir = "dart2";
apiPackage = "lib.api";
modelPackage = "lib.model";
apiPackage = "api";
modelPackage = "model";
modelDocTemplateFiles.put("object_doc.mustache", ".md");
apiDocTemplateFiles.put("api_doc.mustache", ".md");

Expand Down Expand Up @@ -182,15 +183,15 @@ public AbstractDartCodegen() {
imports.put("Object", "dart:core");
imports.put("MultipartFile", "package:http/http.dart");

cliOptions.add(new CliOption(PUB_LIBRARY, "Library name in generated code"));
cliOptions.add(new CliOption(PUB_NAME, "Name in generated pubspec"));
cliOptions.add(new CliOption(PUB_VERSION, "Version in generated pubspec"));
cliOptions.add(new CliOption(PUB_DESCRIPTION, "Description in generated pubspec"));
cliOptions.add(new CliOption(PUB_AUTHOR, "Author name in generated pubspec"));
cliOptions.add(new CliOption(PUB_AUTHOR_EMAIL, "Email address of the author in generated pubspec"));
cliOptions.add(new CliOption(PUB_HOMEPAGE, "Homepage in generated pubspec"));
cliOptions.add(new CliOption(USE_ENUM_EXTENSION, "Allow the 'x-enum-values' extension for enums"));
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "Source folder for generated code"));
addOption(PUB_LIBRARY, "Library name in generated code", pubLibrary);
addOption(PUB_NAME, "Name in generated pubspec", pubName);
addOption(PUB_VERSION, "Version in generated pubspec", pubVersion);
addOption(PUB_DESCRIPTION, "Description in generated pubspec", pubDescription);
addOption(PUB_AUTHOR, "Author name in generated pubspec", pubAuthor);
addOption(PUB_AUTHOR_EMAIL, "Email address of the author in generated pubspec", pubAuthorEmail);
addOption(PUB_HOMEPAGE, "Homepage in generated pubspec", pubHomepage);
addOption(USE_ENUM_EXTENSION, "Allow the 'x-enum-values' extension for enums", String.valueOf(useEnumExtension));
addOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder);
}

@Override
Expand All @@ -212,6 +213,13 @@ public String getHelp() {
public void processOpts() {
super.processOpts();

// Fix a couple Java notation properties
modelPackage = modelPackage.replace('.', '/');
apiPackage = apiPackage.replace('.', '/');
// And overwrite them in the additional properties
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);

if (StringUtils.isEmpty(System.getenv("DART_POST_PROCESS_FILE"))) {
LOGGER.info("Environment variable DART_POST_PROCESS_FILE not defined so the Dart code may not be properly formatted. To define it, try `export DART_POST_PROCESS_FILE=\"/usr/local/bin/dartfmt -w\"` (Linux/Mac)");
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
Expand Down Expand Up @@ -274,8 +282,10 @@ public void processOpts() {
}

if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
String srcFolder = (String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER);
this.setSourceFolder(srcFolder.replace('/', File.separatorChar));
}
additionalProperties.put(CodegenConstants.SOURCE_FOLDER, sourceFolder);

// make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath);
Expand Down Expand Up @@ -307,32 +317,32 @@ public String escapeReservedWord(String name) {

@Override
public String apiFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar);
return (outputFolder + File.separator + libPath + sourceFolder + File.separator + apiPackage()).replace('/', File.separatorChar);
}

@Override
public String modelFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar);
return (outputFolder + File.separator + libPath + sourceFolder + File.separator + modelPackage()).replace('/', File.separatorChar);
}

@Override
public String apiTestFileFolder() {
return outputFolder + File.separator + apiTestPath.replace('/', File.separatorChar);
return outputFolder + File.separator + apiTestPath;
}

@Override
public String modelTestFileFolder() {
return outputFolder + File.separator + modelTestPath.replace('/', File.separatorChar);
return outputFolder + File.separator + modelTestPath;
}

@Override
public String apiDocFileFolder() {
return outputFolder + File.separator + apiDocPath.replace('/', File.separatorChar);
return outputFolder + File.separator + apiDocPath;
}

@Override
public String modelDocFileFolder() {
return outputFolder + File.separator + modelDocPath.replace('/', File.separatorChar);
return outputFolder + File.separator + modelDocPath;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

package org.openapitools.codegen.languages;

import com.google.common.collect.Sets;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.SupportingFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -45,6 +43,8 @@ public DartClientCodegen() {
serializationOptions.put(SERIALIZATION_LIBRARY_NATIVE, "Use native serializer, backwards compatible");
serializationLibrary.setEnum(serializationOptions);
cliOptions.add(serializationLibrary);

sourceFolder = "";
}

@Override
Expand All @@ -61,15 +61,14 @@ public void processOpts() {

this.setSerializationLibrary();

final String libFolder = sourceFolder + File.separator + "lib";
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", "analysis_options.yaml"));
supportingFiles.add(new SupportingFile("api_client.mustache", libFolder, "api_client.dart"));
supportingFiles.add(new SupportingFile("api_exception.mustache", libFolder, "api_exception.dart"));
supportingFiles.add(new SupportingFile("api_helper.mustache", libFolder, "api_helper.dart"));
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));
supportingFiles.add(new SupportingFile("api_client.mustache", libPath, "api_client.dart"));
supportingFiles.add(new SupportingFile("api_exception.mustache", libPath, "api_exception.dart"));
supportingFiles.add(new SupportingFile("api_helper.mustache", libPath, "api_helper.dart"));
supportingFiles.add(new SupportingFile("apilib.mustache", libPath, "api.dart"));

final String authFolder = sourceFolder + File.separator + "lib" + File.separator + "auth";
final String authFolder = libPath + "auth";
supportingFiles.add(new SupportingFile("auth/authentication.mustache", authFolder, "authentication.dart"));
supportingFiles.add(new SupportingFile("auth/http_basic_auth.mustache", authFolder, "http_basic_auth.dart"));
supportingFiles.add(new SupportingFile("auth/http_bearer_auth.mustache", authFolder, "http_bearer_auth.dart"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public DartDioNextClientCodegen() {
embeddedTemplateDir = "dart/libraries/dio";
this.setTemplateDir(embeddedTemplateDir);

apiPackage = "lib.src.api";
modelPackage = "lib.src.model";

supportedLibraries.put(SERIALIZATION_LIBRARY_BUILT_VALUE, "[DEFAULT] built_value");
final CliOption serializationLibrary = CliOption.newString(CodegenConstants.SERIALIZATION_LIBRARY, "Specify serialization library");
serializationLibrary.setEnum(supportedLibraries);
Expand Down Expand Up @@ -162,10 +159,9 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

final String libFolder = sourceFolder + File.separator + "lib";
supportingFiles.add(new SupportingFile("lib.mustache", libFolder, pubName + ".dart"));
supportingFiles.add(new SupportingFile("lib.mustache", libPath, pubName + ".dart"));

final String srcFolder = libFolder + File.separator + "src";
final String srcFolder = libPath + sourceFolder;
supportingFiles.add(new SupportingFile("api_client.mustache", srcFolder, "api.dart"));

final String authFolder = srcFolder + File.separator + "auth";
Expand Down Expand Up @@ -253,8 +249,8 @@ private void configureDateLibrary(String srcFolder) {
if (SERIALIZATION_LIBRARY_BUILT_VALUE.equals(library)) {
typeMapping.put("date", "Date");
typeMapping.put("Date", "Date");
importMapping.put("Date", "package:" + pubName + "/src/model/date.dart");
supportingFiles.add(new SupportingFile("serialization/built_value/date.mustache", srcFolder + File.separator + "model", "date.dart"));
importMapping.put("Date", "package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/date.dart");
supportingFiles.add(new SupportingFile("serialization/built_value/date.mustache", srcFolder + File.separator + modelPackage(), "date.dart"));
supportingFiles.add(new SupportingFile("serialization/built_value/date_serializer.mustache", srcFolder, "date_serializer.dart"));
}
break;
Expand Down Expand Up @@ -382,7 +378,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo

resultImports.addAll(rewriteImports(op.imports, false));
if (op.getHasFormParams() || op.getHasQueryParams()) {
resultImports.add("package:" + pubName + "/src/api_util.dart");
resultImports.add("package:" + pubName + "/" + sourceFolder + "/api_util.dart");
}

// Generate serializer factories for response types.
Expand Down Expand Up @@ -437,7 +433,7 @@ private Set<String> rewriteImports(Set<String> originalImports, boolean isModel)
} else if (importMapping().containsKey(modelImport)) {
resultImports.add(importMapping().get(modelImport));
} else {
resultImports.add("package:" + pubName + "/src/model/" + underscore(modelImport) + ".dart");
resultImports.add("package:" + pubName + "/" + sourceFolder + "/" + modelPackage() + "/" + underscore(modelImport) + ".dart");
}
}
return resultImports;
Expand Down
Loading