8000 Add default property support on model properties by matrosovs · Pull Request #1974 · springfox/springfox · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add default property support on model properties #1974

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 5 commits into from
Sep 17, 2017

Conversation

matrosovs
Copy link
Contributor

What's this PR do/fix?

Adding an option to set default property

Are there unit tests? If not how should this be manually tested?

There are unit tests for changes to ModelProperty and ModelPropertyBuilder.

Any background context you want to provide?

If you want to make use of this new feature, you need the following:

  1. Write custom annotation, which contains defaultValue:
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
@Inherited
@Documented
public @interface ApiModelPropertyExtended {
    String defaultValue() default "";
}
  1. Write custom plugin to make use of that new value:
import <...>.annotation.ApiModelPropertyExtended;   // the annotation we specified earlier
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.google.common.base.Optional;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1000)
public class ApiModelPropertyExtendedPlugin implements ModelPropertyBuilderPlugin {

    @Override
    public void apply(ModelPropertyContext context) {

        Optional<BeanPropertyDefinition> optionalDefinition = context.getBeanPropertyDefinition();

        if (optionalDefinition.isPresent()) {

            BeanPropertyDefinition definition = optionalDefinition.get();
            ApiModelPropertyExtended annotation = null;

            if (definition.hasGetter()) {
                annotation = definition.getGetter().getAnnotation(ApiModelPropertyExtended.class);
            }

            if (annotation == null) {
                annotation = definition.getField().getAnnotation(ApiModelPropertyExtended.class);
            }

            if (annotation != null) {
                context.getBuilder()
                       .defaultValue(annotation.defaultValue())
                       .build();
            }
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
  1. Use recently created annotation in your code:
import <...>.annotation.ApiModelPropertyExtended;
import io.swagger.annotations.ApiModelProperty;
<...>

public interface AccountAPI {

    @ApiModelProperty(
            name = "Account's first name",
            required = true
    )
    @ApiModelPropertyExtended(
            defaultValue = "John Doe"
    )
    String getFirstName();
<...>

@codecov
Copy link
codecov bot commented Aug 8, 2017

Codecov Report

Merging #1974 into master will decrease coverage by 0.02%.
The diff coverage is 71.42%.

@@             Coverage Diff              @@
##             master    #1974      +/-   ##
============================================
- Coverage     95.85%   95.82%   -0.03%     
- Complexity     2667     2669       +2     
============================================
  Files           313      313              
  Lines          5880     5886       +6     
  Branches        483      484       +1     
============================================
+ Hits           5636     5640       +4     
- Misses          142      143       +1     
- Partials        102      103       +1

@dilipkrish dilipkrish added this to the 2.8.0 milestone Sep 17, 2017
@dilipkrish dilipkrish changed the title Add default property Add default property support on model properties Sep 17, 2017
@dilipkrish dilipkrish merged commit 27459ba into springfox:master Sep 17, 2017
@dilipkrish
Copy link
Member

Thanks @matrosovs 🙇

@matrosovs
Copy link
Contributor Author

Thank you @dilipkrish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0