Skip to content

Add an option to skip operation examples #8731

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 2 commits into from
Feb 20, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ public class Generate extends OpenApiGeneratorCommand {
description = CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DESC)
private Boolean removeOperationIdPrefix;

@Option(name = {"--skip-operation-example"}, title = "skip examples defined in the operation",
description = CodegenConstants.SKIP_OPERATION_EXAMPLE_DESC)
private Boolean skipOperationExample;

@Option(name = {"--skip-validate-spec"},
title = "skip spec validation",
description = "Skips the default behavior of validating an input specification.")
Expand Down Expand Up @@ -393,6 +397,10 @@ public void execute() {
configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix);
}

if (skipOperationExample != null) {
configurator.setSkipOperationExample(skipOperationExample);
}

if (enablePostProcessFile != null) {
configurator.setEnablePostProcessFile(enablePostProcessFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class WorkflowSettings {
public static final boolean DEFAULT_VERBOSE = false;
public static final boolean DEFAULT_SKIP_OVERWRITE = false;
public static final boolean DEFAULT_REMOVE_OPERATION_ID_PREFIX = false;
public static final boolean DEFAULT_SKIP_OPERATION_EXAMPLE = false;
public static final boolean DEFAULT_LOG_TO_STDERR = false;
public static final boolean DEFAULT_VALIDATE_SPEC = true;
public static final boolean DEFAULT_ENABLE_POST_PROCESS_FILE = false;
Expand All @@ -56,6 +57,7 @@ public class WorkflowSettings {
private boolean verbose = DEFAULT_VERBOSE;
private boolean skipOverwrite = DEFAULT_SKIP_OVERWRITE;
private boolean removeOperationIdPrefix = DEFAULT_REMOVE_OPERATION_ID_PREFIX;
private boolean skipOperationExample = DEFAULT_SKIP_OPERATION_EXAMPLE;
private boolean logToStderr = DEFAULT_LOG_TO_STDERR;
private boolean validateSpec = DEFAULT_VALIDATE_SPEC;
private boolean enablePostProcessFile = DEFAULT_ENABLE_POST_PROCESS_FILE;
Expand Down Expand Up @@ -104,6 +106,7 @@ public static Builder newBuilder(WorkflowSettings copy) {
builder.verbose = copy.isVerbose();
builder.skipOverwrite = copy.isSkipOverwrite();
builder.removeOperationIdPrefix = copy.isRemoveOperationIdPrefix();
builder.skipOperationExample = copy.isSkipOperationExample();
builder.logToStderr = copy.isLogToStderr();
builder.validateSpec = copy.isValidateSpec();
builder.enablePostProcessFile = copy.isEnablePostProcessFile();
Expand Down Expand Up @@ -169,6 +172,15 @@ public boolean isRemoveOperationIdPrefix() {
return removeOperationIdPrefix;
}

/**
* Indicates whether or not to skip examples defined in the operation.
*
* @return <code>true</code> if the examples defined in the operation should be skipped.
*/
public boolean isSkipOperationExample() {
return skipOperationExample;
}

/**
* Indicates whether or not the generator's executor will write all log messages (not just errors) to STDOUT. Useful for
* piping the JSON output of debug options (e.g. <code>-DdebugOperations</code>) to an external parser directly while testing a generator.
Expand Down Expand Up @@ -284,6 +296,7 @@ public static final class Builder {
private Boolean verbose = DEFAULT_VERBOSE;
private Boolean skipOverwrite = DEFAULT_SKIP_OVERWRITE;
private Boolean removeOperationIdPrefix = DEFAULT_REMOVE_OPERATION_ID_PREFIX;
private Boolean skipOperationExample = DEFAULT_SKIP_OPERATION_EXAMPLE;
private Boolean logToStderr = DEFAULT_LOG_TO_STDERR;
private Boolean validateSpec = DEFAULT_VALIDATE_SPEC;
private Boolean enablePostProcessFile = DEFAULT_ENABLE_POST_PROCESS_FILE;
Expand Down Expand Up @@ -362,6 +375,17 @@ public Builder withRemoveOperationIdPrefix(Boolean removeOperationIdPrefix) {
return this;
}

/**
* Sets the {@code skipOperationExample} and returns a reference to this Builder so that the methods can be chained together.
*
* @param skipOperationExample the {@code skipOperationExample} to set
* @return a reference to this Builder
*/
public Builder withSkipOperationExample(Boolean skipOperationExample) {
this.skipOperationExample = skipOperationExample != null ? skipOperationExample : Boolean.valueOf(DEFAULT_REMOVE_OPERATION_ID_PREFIX);
Copy link

Choose a reason for hiding this comment

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

This appears to be the wrong default that is assigned - should be DEFAULT_SKIP_OPERATION_EXAMPLE i suppose

return this;
}

/**
* Sets the {@code logToStderr} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -568,6 +592,7 @@ public boolean equals(Object o) {
return isVerbose() == that.isVerbose() &&
isSkipOverwrite() == that.isSkipOverwrite() &&
isRemoveOperationIdPrefix() == that.isRemoveOperationIdPrefix() &&
isSkipOperationExample() == that.isSkipOperationExample() &&
isLogToStderr() == that.isLogToStderr() &&
isValidateSpec() == that.isValidateSpec() &&
isEnablePostProcessFile() == that.isEnablePostProcessFile() &&
Expand All @@ -590,6 +615,7 @@ public int hashCode() {
isVerbose(),
isSkipOverwrite(),
isRemoveOperationIdPrefix(),
isSkipOperationExample(),
isLogToStderr(),
isValidateSpec(),
isGenerateAliasAsModel(),
Expand Down
5 changes: 5 additions & 0 deletions modules/openapi-generator-gradle-plugin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ apply plugin: 'org.openapi.generator'
|false
|Remove prefix of operationId, e.g. config_getId => getId.

|skipOperationExample
|Boolean
|false
|Skip examples defined in the operation

|apiFilesConstrainedTo
|List(String)
|None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
reservedWordsMappings.set(generate.reservedWordsMappings)
ignoreFileOverride.set(generate.ignoreFileOverride)
removeOperationIdPrefix.set(generate.removeOperationIdPrefix)
skipOperationExample.set(generate.skipOperationExample)
apiFilesConstrainedTo.set(generate.apiFilesConstrainedTo)
modelFilesConstrainedTo.set(generate.modelFilesConstrainedTo)
supportingFilesConstrainedTo.set(generate.supportingFilesConstrainedTo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val removeOperationIdPrefix = project.objects.property<Boolean?>()

/**
* Skip examples defined in the operation
*/
val skipOperationExample = project.objects.property<Boolean?>()

/**
* Defines which API-related files should be generated. This allows you to create a subset of generated files (or none at all).
*
Expand Down Expand Up @@ -333,4 +338,4 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
skipValidateSpec.set(false)
generateAliasAsModel.set(false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ open class GenerateTask : DefaultTask() {
@Input
val removeOperationIdPrefix = project.objects.property<Boolean?>()

/**
* Remove examples defined in the operation
*/
@Optional
@Input
val skipOperationExample = project.objects.property<Boolean?>()

/**
* Defines which API-related files should be generated. This allows you to create a subset of generated files (or none at all).
*
Expand Down Expand Up @@ -615,6 +622,10 @@ open class GenerateTask : DefaultTask() {
configurator.setRemoveOperationIdPrefix(value!!)
}

skipOperationExample.ifNotEmpty { value ->
configurator.setSkipOperationExample(value!!)
}

logToStderr.ifNotEmpty { value ->
configurator.setLogToStderr(value)
}
Expand Down
1 change: 1 addition & 0 deletions modules/openapi-generator-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mvn clean compile
| `ignoreFileOverride` | `openapi.generator.maven.plugin.ignoreFileOverride` | specifies the full path to a `.openapi-generator-ignore` used for pattern based overrides of generated outputs
| `httpUserAgent` | `openapi.generator.maven.plugin.httpUserAgent` | Sets custom User-Agent header value
| `removeOperationIdPrefix` | `openapi.generator.maven.plugin.removeOperationIdPrefix` | remove operationId prefix (e.g. user_getName => getName)
| `skipOperationExample` | `openapi.generator.maven.plugin.skipOperationExample` | skip examples defined in the operation
| `logToStderr` | `openapi.generator.maven.plugin.logToStderr` | write all log messages (not just errors) to STDOUT
| `enablePostProcessFile` | `openapi.generator.maven.plugin.` | enable file post-processing hook
| `skipValidateSpec` | `openapi.generator.maven.plugin.skipValidateSpec` | Whether or not to skip validating the input spec prior to generation. By default, invalid specifications will result in an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "removeOperationIdPrefix", property = "openapi.generator.maven.plugin.removeOperationIdPrefix")
private Boolean removeOperationIdPrefix;

/**
* To skip examples defined in the operation
*/
@Parameter(name = "skipOperationExample", property = "openapi.generator.maven.plugin.skipOperationExample")
private Boolean skipOperationExample;

/**
* To write all log messages (not just errors) to STDOUT
*/
Expand Down Expand Up @@ -485,6 +491,10 @@ public void execute() throws MojoExecutionException {
configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix);
}

if (skipOperationExample != null) {
configurator.setSkipOperationExample(skipOperationExample);
}

if (isNotEmpty(inputSpec)) {
configurator.setInputSpec(inputSpec);
}
Expand Down Expand Up @@ -888,4 +898,4 @@ private void adjustAdditionalProperties(final CodegenConfig config) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ public interface CodegenConfig {

void setRemoveOperationIdPrefix(boolean removeOperationIdPrefix);

boolean isSkipOperationExample();

void setSkipOperationExample(boolean skipOperationExample);

public boolean isHideGenerationTimestamp();

public void setHideGenerationTimestamp(boolean hideGenerationTimestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String REMOVE_OPERATION_ID_PREFIX = "removeOperationIdPrefix";
public static final String REMOVE_OPERATION_ID_PREFIX_DESC = "Remove prefix of operationId, e.g. config_getId => getId";

public static final String SKIP_OPERATION_EXAMPLE = "skipOperationExample";
public static final String SKIP_OPERATION_EXAMPLE_DESC = "Skip examples defined in operations to avoid out of memory errors.";

public static final String STRIP_PACKAGE_NAME = "stripPackageName";
public static final String STRIP_PACKAGE_NAME_DESC = "Whether to strip leading dot-separated packages from generated model classes";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
protected List<CliOption> cliOptions = new ArrayList<CliOption>();
protected boolean skipOverwrite;
protected boolean removeOperationIdPrefix;
protected boolean skipOperationExample;

/**
* True if the code generator supports multiple class inheritance.
Expand Down Expand Up @@ -318,6 +319,11 @@ public void processOpts() {
.get(CodegenConstants.REMOVE_OPERATION_ID_PREFIX).toString()));
}

if (additionalProperties.containsKey(CodegenConstants.SKIP_OPERATION_EXAMPLE)) {
this.setSkipOperationExample(Boolean.valueOf(additionalProperties
.get(CodegenConstants.SKIP_OPERATION_EXAMPLE).toString()));
}

if (additionalProperties.containsKey(CodegenConstants.DOCEXTENSION)) {
this.setDocExtension(String.valueOf(additionalProperties
.get(CodegenConstants.DOCEXTENSION).toString()));
Expand Down Expand Up @@ -3547,14 +3553,18 @@ protected void handleMethodResponse(Operation operation,
}
}

// generate examples
String exampleStatusCode = "200";
for (String key : operation.getResponses().keySet()) {
if (operation.getResponses().get(key) == methodResponse && !key.equals("default")) {
exampleStatusCode = key;
// check skipOperationExample, which can be set to true to avoid out of memory errors for large spec
if (!isSkipOperationExample()) {
// generate examples
String exampleStatusCode = "200";
for (String key : operation.getResponses().keySet()) {
if (operation.getResponses().get(key) == methodResponse && !key.equals("default")) {
exampleStatusCode = key;
}
}
op.examples = new ExampleGenerator(schemas, this.openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(this.openAPI, operation));
}
op.examples = new ExampleGenerator(schemas, this.openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(this.openAPI, operation));

op.defaultResponse = toDefaultValue(responseSchema);
op.returnType = cm.dataType;
op.returnFormat = cm.dataFormat;
Expand Down Expand Up @@ -4954,10 +4964,18 @@ public boolean isRemoveOperationIdPrefix() {
return removeOperationIdPrefix;
}

public boolean isSkipOperationExample() {
return skipOperationExample;
}

public void setRemoveOperationIdPrefix(boolean removeOperationIdPrefix) {
this.removeOperationIdPrefix = removeOperationIdPrefix;
}

public void setSkipOperationExample(boolean skipOperationExample) {
this.skipOperationExample = skipOperationExample;
}

public boolean isHideGenerationTimestamp() {
return hideGenerationTimestamp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ public CodegenConfigurator setRemoveOperationIdPrefix(boolean removeOperationIdP
return this;
}

public CodegenConfigurator setSkipOperationExample(boolean skipOperationExample) {
workflowSettingsBuilder.withSkipOperationExample(skipOperationExample);
return this;
}

public CodegenConfigurator setSkipOverwrite(boolean skipOverwrite) {
workflowSettingsBuilder.withSkipOverwrite(skipOverwrite);
return this;
Expand Down Expand Up @@ -583,6 +588,7 @@ public ClientOptInput toClientOptInput() {
config.setSkipOverwrite(workflowSettings.isSkipOverwrite());
config.setIgnoreFilePathOverride(workflowSettings.getIgnoreFileOverride());
config.setRemoveOperationIdPrefix(workflowSettings.isRemoveOperationIdPrefix());
config.setSkipOperationExample(workflowSettings.isSkipOperationExample());
config.setEnablePostProcessFile(workflowSettings.isEnablePostProcessFile());
config.setEnableMinimalUpdate(workflowSettings.isEnableMinimalUpdate());
config.setStrictSpecBehavior(workflowSettings.isStrictSpecBehavior());
Expand Down