Skip to content

[C#][Nancy] Customize interface prefix #4557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion bin/nancyfx-petstore-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/nancyfx -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l nancyfx -o samples/server/petstore/nancyfx"
ags="generate $@ -t modules/swagger-codegen/src/main/resources/nancyfx -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l nancyfx -o samples/server/petstore/nancyfx"

java $JAVA_OPTS -jar $executable $ags
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public class CodegenConstants {
public static final String USE_COLLECTION = "useCollection";
public static final String USE_COLLECTION_DESC = "Deserialize array types to Collection<T> instead of List<T>.";

public static final String INTERFACE_PREFIX = "interfacePrefix";
public static final String INTERFACE_PREFIX_DESC = "Prefix interfaces with a community standard or widely accepted prefix.";

public static final String RETURN_ICOLLECTION = "returnICollection";
public static final String RETURN_ICOLLECTION_DESC = "Return ICollection<T> instead of the concrete type.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
protected String packageCompany = "Swagger";
protected String packageCopyright = "No Copyright";

protected String interfacePrefix = "I";

protected String sourceFolder = "src";

// TODO: Add option for test folder output location. Nice to allow e.g. ./test instead of ./src.
Expand Down Expand Up @@ -254,6 +256,18 @@ public void processOpts() {
if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES)) {
setOptionalEmitDefaultValue(Boolean.valueOf(additionalProperties.get(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES).toString()));
}

if (additionalProperties.containsKey(CodegenConstants.INTERFACE_PREFIX)) {
String useInterfacePrefix = additionalProperties.get(CodegenConstants.INTERFACE_PREFIX).toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: what about using Boolean.valueOf to store CodegenConstants.INTERFACE_PREFIX as boolean instead of string so as to avoid case sensitivity issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 Boolean.valueOf would return true if true, and false otherwise. This would remove support for custom prefixes, as in the example in the PR description which prefixes with ZZ.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimschubert you're right. I missed that.

if("false".equals(useInterfacePrefix)) {
setInterfacePrefix("");
} else if(!"true".equals(useInterfacePrefix)) {
// NOTE: if user passes "true" explicitly, we use the default I- prefix. The other supported case here is a custom prefix.
setInterfacePrefix(sanitizeName(useInterfacePrefix));
}

additionalProperties.put(CodegenConstants.INTERFACE_PREFIX, interfacePrefix);
}
}

@Override
Expand Down Expand Up @@ -616,6 +630,14 @@ public void setSourceFolder(String sourceFolder) {
this.sourceFolder = sourceFolder;
}

public String getInterfacePrefix() {
return interfacePrefix;
}

public void setInterfacePrefix(final String interfacePrefix) {
this.interfacePrefix = interfacePrefix;
}

@Override
public String toEnumVarName(String name, String datatype) {
if (name.length() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public CSharpClientCodegen() {
CodegenConstants.OPTIONAL_PROJECT_GUID_DESC,
null);

addOption(CodegenConstants.INTERFACE_PREFIX,
CodegenConstants.INTERFACE_PREFIX_DESC,
interfacePrefix);

CliOption framework = new CliOption(
CodegenConstants.DOTNET_FRAMEWORK,
CodegenConstants.DOTNET_FRAMEWORK_DESC
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package io.swagger.codegen.languages;

import static com.google.common.base.Strings.isNullOrEmpty;
import static io.swagger.codegen.CodegenConstants.OPTIONAL_PROJECT_FILE;
import static io.swagger.codegen.CodegenConstants.OPTIONAL_PROJECT_FILE_DESC;
import static io.swagger.codegen.CodegenConstants.PACKAGE_NAME;
import static io.swagger.codegen.CodegenConstants.PACKAGE_VERSION;
import static io.swagger.codegen.CodegenConstants.RETURN_ICOLLECTION;
import static io.swagger.codegen.CodegenConstants.RETURN_ICOLLECTION_DESC;
import static io.swagger.codegen.CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG;
import static io.swagger.codegen.CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC;
import static io.swagger.codegen.CodegenConstants.SOURCE_FOLDER;
import static io.swagger.codegen.CodegenConstants.SOURCE_FOLDER_DESC;
import static io.swagger.codegen.CodegenConstants.USE_COLLECTION;
import static io.swagger.codegen.CodegenConstants.USE_COLLECTION_DESC;
import static io.swagger.codegen.CodegenConstants.USE_DATETIME_OFFSET;
import static io.swagger.codegen.CodegenConstants.USE_DATETIME_OFFSET_DESC;
import static io.swagger.codegen.CodegenConstants.*;
import static io.swagger.codegen.CodegenType.SERVER;
import static java.util.Arrays.asList;
import static java.util.UUID.randomUUID;
Expand Down Expand Up @@ -71,6 +58,9 @@ public NancyFXServerCodegen() {
outputFolder = "generated-code" + File.separator + getName();
apiTemplateFiles.put("api.mustache", ".cs");

// Early versions use no prefix for interfaces. Defaulting to I- common practice would break existing users.
setInterfacePrefix("");

// contextually reserved words
setReservedWordsLowerCase(
asList("var", "async", "await", "dynamic", "yield")
Expand All @@ -82,6 +72,7 @@ public NancyFXServerCodegen() {
addOption(PACKAGE_NAME, "C# package name (convention: Title.Case).", packageName);
addOption(PACKAGE_VERSION, "C# package version.", packageVersion);
addOption(SOURCE_FOLDER, SOURCE_FOLDER_DESC, sourceFolder);
addOption(INTERFACE_PREFIX, INTERFACE_PREFIX_DESC, interfacePrefix);

// CLI Switches
addSwitch(SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_BY_REQUIRED_FLAG_DESC, sortParamsByRequiredFlag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace {{packageName}}.{{apiPackage}}
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public interface I{{classname}} : IApiAccessor
public interface {{interfacePrefix}}{{classname}} : IApiAccessor
{
#region Synchronous Operations
{{#operation}}
Expand Down Expand Up @@ -73,7 +73,7 @@ namespace {{packageName}}.{{apiPackage}}
/// <summary>
/// Represents a collection of functions to interact with the API endpoints
/// </summary>
public partial class {{classname}} : I{{classname}}
public partial class {{classname}} : {{interfacePrefix}}{{classname}}
{
private {{packageName}}.Client.ExceptionFactory _exceptionFactory = (name, response) => null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace {{packageName}}.{{packageContext}}.Modules
/// <summary>
/// Service handling {{classname}} requests.
/// </summary>
public interface {{classname}}Service
public interface {{interfacePrefix}}{{classname}}Service
{
{{#operation}}/// <summary>
/// {{notes}}
Expand All @@ -56,7 +56,7 @@ namespace {{packageName}}.{{packageContext}}.Modules
/// <summary>
/// Abstraction of {{classname}}Service.
/// </summary>
public abstract class Abstract{{classname}}Service: {{classname}}Service
public abstract class Abstract{{classname}}Service: {{interfacePrefix}}{{classname}}Service
{
{{#operation}}public virtual {{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}(NancyContext context{{#allParams.0}}, {{/allParams.0}}{{>paramsList}})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protected void setExpectations() {
times = 1;
clientCodegen.setGeneratePropertyChanged(true);
times = 1;
clientCodegen.setInterfacePrefix("X");
times = 1;
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.OPTIONAL_EMIT_DEFAULT_VALUES, "true")
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
.put(CodegenConstants.GENERATE_PROPERTY_CHANGED, "true")
.put(CodegenConstants.INTERFACE_PREFIX, "X")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package io.swagger.codegen.options;

import static io.swagger.codegen.CodegenConstants.PACKAGE_NAME;
import static io.swagger.codegen.CodegenConstants.PACKAGE_VERSION;
import static io.swagger.codegen.CodegenConstants.RETURN_ICOLLECTION;
import static io.swagger.codegen.CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG;
import static io.swagger.codegen.CodegenConstants.SOURCE_FOLDER;
import static io.swagger.codegen.CodegenConstants.USE_COLLECTION;
import static io.swagger.codegen.CodegenConstants.USE_DATETIME_OFFSET;

import java.util.Map;

import com.google.common.collect.ImmutableMap;

import static io.swagger.codegen.CodegenConstants.*;

public class NancyFXServerOptionsProvider implements OptionsProvider {
public static final String PACKAGE_NAME_VALUE = "swagger_server_nancyfx";
public static final String PACKAGE_VERSION_VALUE = "1.0.0-SNAPSHOT";
Expand All @@ -32,6 +26,7 @@ public Map<String, String> createOptions() {
.put(USE_DATETIME_OFFSET, "true")
.put(USE_COLLECTION, "false")
.put(RETURN_ICOLLECTION, "false")
.put(INTERFACE_PREFIX, "X")
.build();
}

Expand Down