Skip to content

Fixes #357: Allow exclusion of endpoints #895

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ The `executions` block is used to specify the phase of the build lifecycle you w
| `operationIdFormat` | Format of `operationId` used in Swagger spec. For historical reasons default is Java method name. Since 3.1.8, for new APIs suggested format is: `{{className}}_{{methodName}}_{{httpMethod}}`. `{{packageName}}` token is also supported. |
| `externalDocs` | URL Reference to external documentation |
| `responseMessageOverrides` | Default response message overrides of type ```@ApiResponse```. Example: `<responseMessageOverrides><responseMessageOverride><code>401</code><message>Unauthenticated - could not authenticate the user.</message></responseMessageOverride></responseMessageOverrides>` |
| `includeHidden` | Specifies whether hidden resources, operations and parameters should be included in the generated output. Default: `false`. |
# <a id="templatefile">Template File</a>

If you'd like to generate a template-driven static document, such as markdown or HTML documentation, you'll need to specify a [handlebars](https://github.com/jknack/handlebars.java) template file in ```templatePath```.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ protected ClassSwaggerReader resolveApiReader() throws GenerateException {
reader.setTypesToSkip(this.typesToSkip);
reader.setOperationIdFormat(this.apiSource.getOperationIdFormat());
reader.setResponseMessageOverrides(this.apiSource.getResponseMessageOverrides());
reader.setIncludeHidden(apiSource.isIncludeHidden());
return reader;
} else {
ClassSwaggerReader customApiReader = getCustomApiReader(customReaderClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public class ApiSource {
@Parameter
private List<ResponseMessageOverride> responseMessageOverrides;

@Parameter
private boolean includeHidden = false;

public Set<Class<?>> getValidClasses(Class<? extends Annotation> clazz) {
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();

Expand Down Expand Up @@ -500,5 +503,13 @@ public Boolean getRemoveBasePathFromEndpoints() {
public void setRemoveBasePathFromEndpoints(Boolean removeBasePathFromEndpoints) {
this.removeBasePathFromEndpoints = removeBasePathFromEndpoints;
}

public boolean isIncludeHidden() {
return includeHidden;
}

public void setIncludeHidden(boolean includeHidden) {
this.includeHidden = includeHidden;
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.kongchen.swagger.docgen.reader;

import com.github.kongchen.swagger.docgen.ResponseMessageOverride;
import com.github.kongchen.swagger.docgen.mavenplugin.ApiSource;
import com.github.kongchen.swagger.docgen.util.TypeExtracter;
import com.github.kongchen.swagger.docgen.util.TypeWithAnnotations;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -40,6 +41,7 @@ public abstract class AbstractReader {
protected Swagger swagger;
private Set<Type> typesToSkip = new HashSet<Type>();
protected List<ResponseMessageOverride> responseMessageOverrides;
protected boolean includeHidden = false;

protected String operationIdFormat;

Expand Down Expand Up @@ -292,6 +294,9 @@ protected void updateOperation(String[] apiConsumes, String[] apiProduces, Map<S
}

private boolean isApiParamHidden(List<Annotation> parameterAnnotations) {
if (includeHidden) {
return false;
}
for (Annotation parameterAnnotation : parameterAnnotations) {
if (parameterAnnotation instanceof ApiParam) {
return ((ApiParam) parameterAnnotation).hidden();
Expand All @@ -301,6 +306,14 @@ private boolean isApiParamHidden(List<Annotation> parameterAnnotations) {
return false;
}

protected boolean isApiOperationHidden(ApiOperation apiOperation) {
if (includeHidden) {
return false;
} else {
return apiOperation != null && apiOperation.hidden();
}
}

private boolean hasValidAnnotations(List<Annotation> parameterAnnotations) {
// Because method parameters can contain parameters that are valid, but
// not part of the API contract, first check to make sure the parameter
Expand Down Expand Up @@ -577,5 +590,9 @@ public String getOperationIdFormat() {
public void setOperationIdFormat(String operationIdFormat) {
this.operationIdFormat = operationIdFormat;
}

public void setIncludeHidden(boolean includeHidden) {
this.includeHidden = includeHidden;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Swagger getSwagger() {
}

public Swagger read(Class<?> cls) {
return read(cls, "", null, false, new String[0], new String[0], new HashMap<String, Tag>(), new ArrayList<Parameter>());
return read(cls, "", null, includeHidden, new String[0], new String[0], new HashMap<String, Tag>(), new ArrayList<Parameter>());
}

protected Swagger read(Class<?> cls, String parentPath, String parentMethod, boolean readHidden, String[] parentConsumes,
Expand Down Expand Up @@ -125,7 +125,7 @@ protected Swagger read(Class<?> cls, String parentPath, String parentMethod, boo
List<Method> filteredMethods = getFilteredMethods(cls);
for (Method method : filteredMethods) {
ApiOperation apiOperation = AnnotationUtils.findAnnotation(method, ApiOperation.class);
if (apiOperation != null && apiOperation.hidden()) {
if (isApiOperationHidden(apiOperation)) {
continue;
}
Path methodPath = AnnotationUtils.findAnnotation(method, Path.class);
Expand Down Expand Up @@ -282,6 +282,7 @@ private void handleSubResource(String[] apiConsumes, String httpMethod, String[]
responseClass = apiOperation.response();
}
LOGGER.debug("handling sub-resource method " + method.toString() + " -> " + responseClass);
// TODO: Should we use includeHidden instead of true?
read(responseClass, operationPath, httpMethod, true, apiConsumes, apiProduces, tags, operation.getParameters());
}
}
Expand Down Expand Up @@ -344,7 +345,7 @@ public Operation parseMethod(String httpMethod, Method method) {
Map<String, Property> defaultResponseHeaders = null;

if (apiOperation != null) {
if (apiOperation.hidden()) {
if (isApiOperationHidden(apiOperation)) {
return null;
}
if (!apiOperation.nickname().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public Swagger read(SpringResource resource) {

if (controller.isAnnotationPresent(Api.class)) {
Api api = findMergedAnnotation(controller, Api.class);
if (!canReadApi(false, api)) {
if (!canReadApi(includeHidden, api)) {
return swagger;
}
tags = updateTagsForApi(null, api);
Expand All @@ -106,7 +106,7 @@ public Swagger read(SpringResource resource) {
continue;
}
ApiOperation apiOperation = findMergedAnnotation(method, ApiOperation.class);
if (apiOperation != null && apiOperation.hidden()) {
if (isApiOperationHidden(apiOperation)) {
continue;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ private Operation parseMethod(Method method, RequestMethod requestMethod) {
ApiOperation apiOperation = findMergedAnnotation(method, ApiOperation.class);

if(apiOperation != null) {
if (apiOperation.hidden()) {
if (isApiOperationHidden(apiOperation)) {
return null;
}
if (!apiOperation.nickname().isEmpty()) {
Expand Down