Created
September 25, 2018 18:09
-
-
Save rehevkor5/e77d1e5f2f17ac784e3da3be56e9c7ca to your computer and use it in GitHub Desktop.
Swagger plugin for dynamic allowed values, implementing ExpandedParameterBuilderPlugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mydomain.swagger; | |
import mydomain.repo.AttributeMetadataRepo; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.google.common.base.Optional; | |
import com.google.common.base.Strings; | |
import io.swagger.annotations.ApiModelProperty; | |
import io.swagger.annotations.ApiParam; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.core.annotation.Order; | |
import org.springframework.stereotype.Component; | |
import springfox.documentation.service.AllowableValues; | |
import springfox.documentation.spi.DocumentationType; | |
import springfox.documentation.spi.service.ExpandedParameterBuilderPlugin; | |
import springfox.documentation.spi.service.contexts.ParameterExpansionContext; | |
import springfox.documentation.spring.web.DescriptionResolver; | |
import springfox.documentation.swagger.annotations.Annotations; | |
import springfox.documentation.swagger.common.SwaggerPluginSupport; | |
import springfox.documentation.swagger.schema.ApiModelProperties; | |
import java.lang.reflect.Field; | |
@Component | |
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1) | |
public class DynamicExpandedParameterBuilder implements ExpandedParameterBuilderPlugin { | |
private final DescriptionResolver descriptions; | |
private final AttributeMetadataRepo attributeMetadataRepo; | |
private final ObjectMapper mapper; | |
@Autowired | |
public DynamicExpandedParameterBuilder(DescriptionResolver descriptions, | |
AttributeMetadataRepo attributeMetadataRepo, ObjectMapper mapper) { | |
this.descriptions = descriptions; | |
this.attributeMetadataRepo = attributeMetadataRepo; | |
this.mapper = mapper; | |
} | |
public void apply(ParameterExpansionContext context) { | |
Optional<ApiParam> apiParamOptional = | |
Annotations.findApiParamAnnotation(context.getField().getRawMember()); | |
if (apiParamOptional.isPresent()) { | |
this.fromApiParam(context, apiParamOptional.get()); | |
} | |
Optional<ApiModelProperty> apiModelPropertyOptional = | |
ApiModelProperties.findApiModePropertyAnnotation(context.getField().getRawMember()); | |
if (apiModelPropertyOptional.isPresent()) { | |
this.fromApiModelProperty(context, apiModelPropertyOptional.get()); | |
} | |
} | |
public boolean supports(DocumentationType delimiter) { | |
return SwaggerPluginSupport.pluginDoesApply(delimiter); | |
} | |
private void fromApiParam(ParameterExpansionContext context, ApiParam apiParam) { | |
String allowableProperty = Strings.emptyToNull(apiParam.allowableValues()); | |
if ("dynamic[whatever]".equals(allowableProperty)) { | |
AllowableValues allowable = this.allowableValues(Optional.fromNullable(allowableProperty), | |
context.getField().getRawMember()); | |
context.getParameterBuilder().description(this.descriptions.resolve(apiParam.value())) | |
.defaultValue(apiParam.defaultValue()).required(apiParam.required()) | |
.allowMultiple(apiParam.allowMultiple()).allowableValues(allowable) | |
.parameterAccess(apiParam.access()).hidden(apiParam.hidden()).build(); | |
} | |
} | |
private void fromApiModelProperty(ParameterExpansionContext context, ApiModelProperty apiModelProperty) { | |
String allowableProperty = Strings.emptyToNull(apiModelProperty.allowableValues()); | |
if ("dynamic[whatever]".equals(allowableProperty)) { | |
AllowableValues allowable = this.allowableValues(Optional.fromNullable(allowableProperty), | |
context.getField().getRawMember()); | |
context.getParameterBuilder().description(this.descriptions.resolve(apiModelProperty.value())) | |
.required(apiModelProperty.required()).allowableValues(allowable) | |
.parameterAccess(apiModelProperty.access()).hidden(apiModelProperty.hidden()).build(); | |
} | |
} | |
private AllowableValues allowableValues(Optional<String> optionalAllowable, Field field) { | |
DynamicAllowableValues allowableValues = | |
new DynamicAllowableValues(attributeMetadataRepo); | |
return allowableValues; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment