Skip to content

Commit

Permalink
[all] Move feature set setter (OpenAPITools#5460)
Browse files Browse the repository at this point in the history
When I originally implemented the feature set code, I added the
getter/setter on DefaultCodegen and CodegenConfig as well as on
GeneratorMetadata. GeneratorMetadata also includes the library variation
features. When I went to add library-specific features, I realized the
discrepancy.

This removes the public setter from DefaultCodegen/CodegenConfig, and
adds a protected modifyFeatureSet which accepts a lambda and hides the
builder logic away in the method.

This will be a breaking change for anyone who's created a custom
generator in 4.2.3, so the impact is very limited.
  • Loading branch information
jimschubert authored and MikailBag committed Mar 23, 2020
1 parent b3a5c83 commit c40c05f
Show file tree
Hide file tree
Showing 115 changed files with 216 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private void generateMarkdownHelp(StringBuilder sb, CodegenConfig config) {
if (Boolean.TRUE.equals(featureSets)) {
sb.append(newline).append("## FEATURE SET").append(newline).append(newline);

List<FeatureSet.FeatureSetFlattened> flattened = config.getFeatureSet().flatten();
List<FeatureSet.FeatureSetFlattened> flattened = config.getGeneratorMetadata().getFeatureSet().flatten();
flattened.sort(Comparator.comparing(FeatureSet.FeatureSetFlattened::getFeatureCategory));

AtomicReference<String> lastCategory = new AtomicReference<>();
Expand Down Expand Up @@ -385,7 +385,7 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) {
if (Boolean.TRUE.equals(featureSets)) {
sb.append(newline).append("FEATURE SET").append(newline);

List<FeatureSet.FeatureSetFlattened> flattened = config.getFeatureSet().flatten();
List<FeatureSet.FeatureSetFlattened> flattened = config.getGeneratorMetadata().getFeatureSet().flatten();
flattened.sort(Comparator.comparing(FeatureSet.FeatureSetFlattened::getFeatureCategory));

AtomicReference<String> lastCategory = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ public interface CodegenConfig {

FeatureSet getFeatureSet();

void setFeatureSet(FeatureSet featureSet);

boolean isRemoveEnumValuePrefix();

void setRemoveEnumValuePrefix(boolean removeEnumValuePrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.io.File;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -124,7 +125,6 @@ public class DefaultCodegen implements CodegenConfig {
.build();
}

protected FeatureSet featureSet;
protected GeneratorMetadata generatorMetadata;
protected String inputSpec;
protected String outputFolder = "";
Expand Down Expand Up @@ -1289,10 +1289,9 @@ public DefaultCodegen() {
codegenType = CodegenType.OTHER;
}

featureSet = DefaultFeatureSet;

generatorMetadata = GeneratorMetadata.newBuilder()
.stability(Stability.STABLE)
.featureSet(DefaultFeatureSet)
.generationMessage(String.format(Locale.ROOT, "OpenAPI Generator: %s (%s)", getName(), codegenType.toValue()))
.build();

Expand Down Expand Up @@ -5672,12 +5671,7 @@ public void setStrictSpecBehavior(final boolean strictSpecBehavior) {

@Override
public FeatureSet getFeatureSet() {
return this.featureSet;
}

@Override
public void setFeatureSet(final FeatureSet featureSet) {
this.featureSet = featureSet == null ? DefaultFeatureSet : featureSet;
return this.generatorMetadata.getFeatureSet();
}

/**
Expand Down Expand Up @@ -5742,4 +5736,11 @@ public void addOneOfInterfaceModel(ComposedSchema cs, String type) {

public void addImportsToOneOfInterface(List<Map<String, String>> imports) {}
//// End of methods related to the "useOneOfInterfaces" feature

protected void modifyFeatureSet(Consumer<FeatureSet.Builder> processor) {
FeatureSet.Builder builder = getFeatureSet().modify();
processor.accept(builder);
this.generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.featureSet(builder.build()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
public AbstractJavaCodegen() {
super();

featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML))
.securityFeatures(EnumSet.noneOf(
Expand All @@ -119,7 +119,7 @@ public AbstractJavaCodegen() {
.includeClientModificationFeatures(
ClientModificationFeature.BasePath
)
.build();
);

supportsInheritance = true;
modelTemplateFiles.put("model.mustache", ".java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
public AbstractTypeScriptClientCodegen() {
super();

featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.wireFormatFeatures(EnumSet.of(WireFormatFeature.JSON, WireFormatFeature.XML))
.securityFeatures(EnumSet.noneOf(
Expand All @@ -100,7 +100,7 @@ public AbstractTypeScriptClientCodegen() {
.includeClientModificationFeatures(
ClientModificationFeature.BasePath
)
.build();
);

// clear import mapping (from default generator) as TS does not use it
// at the moment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void processOpts() {
super.processOpts();

// TODO: Ada maintainer review.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.excludeDocumentationFeatures(DocumentationFeature.Readme)
.excludeWireFormatFeatures(
WireFormatFeature.XML,
Expand Down Expand Up @@ -86,7 +86,7 @@ public void processOpts() {
ParameterFeature.Cookie
)
.includeClientModificationFeatures(ClientModificationFeature.BasePath)
.build();
);

if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
packageName = (String) additionalProperties.get(CodegenConstants.PACKAGE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AdaServerCodegen() {
super();

// TODO: Ada maintainer review.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.excludeWireFormatFeatures(
WireFormatFeature.XML,
Expand Down Expand Up @@ -64,7 +64,7 @@ public AdaServerCodegen() {
ParameterFeature.Cookie
)
.includeClientModificationFeatures(ClientModificationFeature.BasePath)
.build();
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public AndroidClientCodegen() {
super();

// TODO: Android client maintainer review.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.excludeWireFormatFeatures(
WireFormatFeature.PROTOBUF
Expand All @@ -87,7 +87,7 @@ public AndroidClientCodegen() {
ParameterFeature.Cookie
)
.includeClientModificationFeatures(ClientModificationFeature.BasePath)
.build();
);

outputFolder = "generated-code/android";
modelTemplateFiles.put("model.mustache", ".java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Apache2ConfigCodegen() {
super();

// TODO: Apache2 maintainer review.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.parameterFeatures(EnumSet.of(ParameterFeature.Path))
.securityFeatures(EnumSet.of(SecurityFeature.BasicAuth))
.dataTypeFeatures(EnumSet.noneOf(DataTypeFeature.class))
Expand All @@ -61,7 +61,7 @@ public Apache2ConfigCodegen() {
.globalFeatures(EnumSet.noneOf(GlobalFeature.class))
.schemaSupportFeatures(EnumSet.noneOf(SchemaSupportFeature.class))
.clientModificationFeatures(EnumSet.noneOf(ClientModificationFeature.class))
.build();
);

apiTemplateFiles.put("apache-config.mustache", ".conf");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ public AsciidocDocumentationCodegen() {
super();

// TODO: Asciidoc maintainer review.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.securityFeatures(EnumSet.noneOf(SecurityFeature.class))
.documentationFeatures(EnumSet.noneOf(DocumentationFeature.class))
.globalFeatures(EnumSet.noneOf(GlobalFeature.class))
.schemaSupportFeatures(EnumSet.noneOf(SchemaSupportFeature.class))
.clientModificationFeatures(EnumSet.noneOf(ClientModificationFeature.class))
.build();
);

LOGGER.trace("start asciidoc codegen");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public AspNetCoreServerCodegen() {
super();

// TODO: AspnetCore community review
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.excludeWireFormatFeatures(WireFormatFeature.PROTOBUF)
.includeSecurityFeatures(
Expand Down Expand Up @@ -117,7 +117,7 @@ public AspNetCoreServerCodegen() {
.includeParameterFeatures(
ParameterFeature.Cookie
)
.build();
);

outputFolder = "generated-code" + File.separator + getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AvroSchemaCodegen() {
.build();

// TODO: Avro maintainer review.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.parameterFeatures(EnumSet.noneOf(ParameterFeature.class))
.securityFeatures(EnumSet.noneOf(SecurityFeature.class))
.wireFormatFeatures(EnumSet.noneOf(WireFormatFeature.class))
Expand All @@ -55,7 +55,7 @@ public AvroSchemaCodegen() {
SchemaSupportFeature.Union
)
.clientModificationFeatures(EnumSet.noneOf(ClientModificationFeature.class))
.build();
);

outputFolder = "generated-code/avro-schema";
modelTemplateFiles.put("model.mustache", ".avsc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public BashClientCodegen() {
super();

// TODO: Bash maintainer review
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.documentationFeatures(EnumSet.of(
DocumentationFeature.Readme
))
Expand All @@ -125,7 +125,7 @@ public BashClientCodegen() {
SchemaSupportFeature.Polymorphism,
SchemaSupportFeature.Union
)
.build();
);

setReservedWordsLowerCase(
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public CLibcurlClientCodegen() {
// TODO: c maintainer review
// Assumes that C community considers api/model header files as documentation.
// Generator supports Basic, OAuth, and API key explicitly. Bearer is excluded although clients are able to set headers directly.
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(
DocumentationFeature.Readme
)
Expand All @@ -73,7 +73,7 @@ public CLibcurlClientCodegen() {
SchemaSupportFeature.Polymorphism,
SchemaSupportFeature.Union
)
.build();
);

modelPackage = "models";
apiPackage = "api";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
public CSharpClientCodegen() {
super();

featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.securityFeatures(EnumSet.of(
SecurityFeature.OAuth2_Implicit,
Expand All @@ -103,7 +103,7 @@ public CSharpClientCodegen() {
ClientModificationFeature.BasePath,
ClientModificationFeature.UserAgent
)
.build();
);

supportsInheritance = true;
modelTemplateFiles.put("model.mustache", ".cs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public class CSharpDotNet2ClientCodegen extends AbstractCSharpCodegen {
public CSharpDotNet2ClientCodegen() {
super();

featureSet = getFeatureSet().modify()
.includeDocumentationFeatures(DocumentationFeature.Readme)
.build();
modifyFeatureSet(features -> features.includeDocumentationFeatures(DocumentationFeature.Readme));

generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.DEPRECATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class CSharpNancyFXServerCodegen extends AbstractCSharpCodegen {
public CSharpNancyFXServerCodegen() {
super();

featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.excludeDocumentationFeatures(DocumentationFeature.Readme)
.securityFeatures(EnumSet.noneOf(SecurityFeature.class))
.excludeGlobalFeatures(
Expand All @@ -84,7 +84,7 @@ public CSharpNancyFXServerCodegen() {
.excludeParameterFeatures(
ParameterFeature.Cookie
)
.build();
);

outputFolder = "generated-code" + File.separator + getName();
apiTemplateFiles.put("api.mustache", ".cs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
public CSharpNetCoreClientCodegen() {
super();

featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.securityFeatures(EnumSet.of(
SecurityFeature.OAuth2_Implicit,
Expand All @@ -115,7 +115,7 @@ public CSharpNetCoreClientCodegen() {
ClientModificationFeature.BasePath,
ClientModificationFeature.UserAgent
)
.build();
);

// mapped non-nullable type without ?
typeMapping = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ClojureClientCodegen() {
super();

// TODO: Clojure maintainer review
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.excludeDocumentationFeatures(
DocumentationFeature.Readme
)
Expand All @@ -80,7 +80,7 @@ public ClojureClientCodegen() {
SchemaSupportFeature.Polymorphism,
SchemaSupportFeature.Union
)
.build();
);

outputFolder = "generated-code" + File.separator + "clojure";
modelTemplateFiles.put("spec.mustache", ".clj");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ConfluenceWikiCodegen() {
super();

// TODO: ConfluenceWiki maintainer review
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.documentationFeatures(EnumSet.noneOf(DocumentationFeature.class))
.securityFeatures(EnumSet.noneOf(SecurityFeature.class))
.excludeParameterFeatures(ParameterFeature.Cookie)
Expand All @@ -49,7 +49,7 @@ public ConfluenceWikiCodegen() {
SchemaSupportFeature.Polymorphism,
SchemaSupportFeature.Union
)
.build();
);

outputFolder = "docs";
embeddedTemplateDir = templateDir = "confluenceWikiDocs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public CppPistacheServerCodegen() {
super();

// TODO: cpp-pistache-server maintainer review
featureSet = getFeatureSet().modify()
modifyFeatureSet(features -> features
.includeDocumentationFeatures(DocumentationFeature.Readme)
.securityFeatures(EnumSet.noneOf(SecurityFeature.class))
.excludeGlobalFeatures(
Expand All @@ -85,7 +85,7 @@ public CppPistacheServerCodegen() {
.excludeParameterFeatures(
ParameterFeature.Cookie
)
.build();
);

if (StringUtils.isEmpty(modelNamePrefix)) {
modelNamePrefix = PREFIX;
Expand Down
Loading

0 comments on commit c40c05f

Please sign in to comment.