question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extensions in ApiModelProperty

See original GitHub issue

Hi,

I am using springfox v2.9.2 and I have been able to use extensions on @ApiOperation annotations, but I can’t use them on @ApiModelProperty annotations. Is there something I am missing in this as I can see that the Extensions items are in the Swagger code for ApiModelProperty but they don’t get carried across to the Swagger documentation like they do when they are in the ApiOperation annotation. Below is what I am adding as the extensions.

@ApiModelProperty(name = "preference",
 value = "Specifies the preference.\n" +
     "Default: fastest.", extensions = {
       @Extension(name = "test", properties = {
         @ExtensionProperty(name="test-prop", value="val")
 })
})

Thanks

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
drzaiusx11commented, Apr 12, 2019

I got this working by making a ModelPropertyBuilderPlugin that looks at the annotations:

public void apply(final ModelPropertyContext context) {
	final ModelPropertyBuilder builder = context.getBuilder();

	final Optional<BeanPropertyDefinition> beanPropDef = context.getBeanPropertyDefinition();

	if (!beanPropDef.isPresent()) {
		return;
	}

	final BeanPropertyDefinition beanDef = beanPropDef.get();
	final AnnotatedMethod method = beanDef.getGetter();
	if (method == null) {
		return;
	}

	final ApiModelProperty apiModelProperty = method.getAnnotation(ApiModelProperty.class);
	if (apiModelProperty == null) {
		return;
	}

	List<VendorExtension> vendorExtensions = new ArrayList<>();

	if (apiModelProperty.extensions().length > 0) {
		Extension[] extensionAnnotations = apiModelProperty.extensions();
		for (Extension extensionAnnotation : extensionAnnotations) {
			for (ExtensionProperty prop : extensionAnnotation.properties()) {
				vendorExtensions.add(new StringVendorExtension("x-"+prop.name(), prop.value()));
			}
		}
	}

	builder.extensions(vendorExtensions);
}
3reactions
rabidllamacommented, Nov 7, 2018

It’s basically so that we can add extra information to the documentation, such as saying that this property is dependant on another, for example

@ApiModelProperty(name = "preference",
 value = "Specifies the preference.\n" +
     "Default: fastest.", extensions = {
       @Extension(name = "dependencies", properties = {
         @ExtensionProperty(name="vehicle", value="car")
 })
})

which could then be used by a secondary swagger parser to highlight in documentation that providing a preference property is dependent on the vehicle property being set to car, as then the swagger output would be something like:

vehicle:
    type: "string",
    default: "car"
preference:
    type: "string"
    x-dependencies: 
       vehicle: "car"
    

I believe that it was implemented into Swagger here: https://github.com/swagger-api/swagger-core/pull/2424

Read more comments on GitHub >

github_iconTop Results From Across the Web

ApiModelProperty (swagger-annotations 1.5.19 API) - javadoc.io
A sample value for the property. Extension[], extensions. boolean, hidden. Allows a model property to be hidden in ...
Read more >
Extension (swagger-annotations 1.5.0 API)
The extension properties. Returns: the actual extension properties; See Also: ExtensionProperty. name. public abstract String name.
Read more >
io.swagger.annotations.Extension Java Examples
This page shows Java code examples of io.swagger.annotations.Extension.
Read more >
swagger @ApiModelProperty example value for List<String ...
I managed to get this to work, generating a List of Strings. Within the ApiModelProperty with springfox 2, write your example as follows:...
Read more >
@io.swagger.annotations.ApiModelProperty(value = Whether or not ...
java code examples for @io.swagger.annotations.ApiModelProperty(value = Whether or not the extension has additional detail documentation).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found