Skip to content
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

Add support in the models for rendering examples in operations etc. #853

Closed
dilipkrish opened this issue Jul 17, 2015 · 59 comments
Closed

Comments

@dilipkrish
Copy link
Member

No description provided.

@dilipkrish dilipkrish added this to the Someday milestone Jul 17, 2015
@Khoulaiz
Copy link

I just stumbled about this issue. For me, it looks more than a bug than a feature, because springfox is supporting example values from the ApiModel annotation. Why is it ignoring the ApiParam example attribute? Is there a complexity that I don't understand? If not and it is just not prioritized I would give it a shot and provide a small pull request.

@dilipkrish
Copy link
Member Author

that would be great! 🤘 Let me know if you have any questions

@Lucas-C
Copy link

Lucas-C commented Jul 4, 2016

+1
Was there some progress on this issue by anyone so far ?
We'd love to have this feature too.

@dilipkrish
Copy link
Member Author

No bandwidth to do this currently. Would you perhaps like to give a shot at a PR? I'd be more than happy to help answer any questions.

@Lucas-C
Copy link

Lucas-C commented Jul 8, 2016

Sadly I won't have time to take a look at this for now

@bvanloocke
Copy link

+1

@dilipkrish
Copy link
Member Author

Instructions on how to do this in case someone wants to help

Hey have a look at these instructions to support a custom annotation for a parameter. Essentially we need to do a similar thing ... with a little bit more work.

  1. We need to add an example property to the Parameter object
  2. Add corresponding property to ParameterBuilder
  3. Add logic to map the internal Parameter model to the swagger Parameter model

We need to do the same thing for Response model as well. Update the builder and the mapper

Once we do the above we can create a ParameterBuilderPlugin for the parameters and OperationBuilderPlugin plugin for response examples.

@andrew-garland
Copy link

+1

1 similar comment
@mathieuales
Copy link
Contributor

+1

@andrew-garland
Copy link

@dilipkrish I have a question about a similar use case.

I have a re-usable core JSON class (just id/label pairs) called BasicJSON

The JSON object used for POST/PUT/GET (call it FooJSON) has multiple fields of type BasicJSON

I don't need/want to annotate the @ApiParameter or @requestbody of various controller methods, I simply want to provide different examples of BasicJSON for different fields on FooJSON.

The example attribute of @ApiModelProperty only allows strings. Is there a way to format the "string" value so that it gets converted into proper json by the swagger ui? I tried something like '{"id": 7, value: "Hello"}' with various versions of quoting but the UI treated them like just strings not as json objects. It seems to me that the @ExampleProperty could be useful somehow in this context as well if there is a hard-constraint on example attribute being a String.

@dilipkrish
Copy link
Member Author

Yeah you're right about ExampleProperty. However with all of that changing in the near future with 3.0 I don't really know if it would be worth adding support in the core product just yet only to change it pretty soon.

@andrew-garland
Copy link

Thanks for the response. Is the timeline and feature set of 3.0 documented somewhere?

@dilipkrish
Copy link
Member Author

Its all in the issue queue... prioritized by demand :) Mostly stories tagged someday aren't really worth (my time) supporting in the core library, because there are extensibility mechanisms that already support it.

@andrew-garland
Copy link

Sorry to be slow, are you referring to Springfox 3.0 or Swagger 3? For springfox, I only see 2.7.0 upcoming. In either case, will there be an avenue for me to provide more specific examples on fields via @ApiModelProperty or similar?

@dilipkrish
Copy link
Member Author

It can be confusing :)

  1. OpenAPI is the spec (aka swagger) that has version => 2.0 (current). 3.0 has a lot of the new stuff and changes in the structure of the document.
  2. Swagger-Core is the tool chain that is built on top of Open API 2.0 and JAX-RS, Jersey and a slew of other libraries and frameworks for the JVM. Currently in version => 1.5.x. For all practical purposes we don't care about this library.
  3. SpringFox is the library that can generate OpenAPI documents (supports 1.2 and 2.0) for the spring ecosystem . Currently in version => 2.7.0-SNAPSHOT

So what I was implying was that since (1) is changing in the near future, its not good use of time in building something for v2.0 of (1) in Springfox (3) since it needs to be re-worked pretty soon.

Was that helpful?

@andrew-garland
Copy link

Thanks for the clarity 👍

One quibble I have is that Swagger/OpenAPI version 3 is just now a release candidate so that IMO it may be a while before it has the widespread tooling support and acceptance by the community that Swagger 2 has now. Furthermore, since springfox still supports Swagger 1, I can't imagine springfox will be dropping support for swagger 2 very soon :) So the functionality I am asking about will have value even if the implementation details will vary for a swagger 2 docket vs. a swagger 3 docket.

OTOH, I realize that efforts to support OpenAPI 3.0.0 in springfox would naturally come before this kind of feature request. Much thanks for all you do with this project!

@dilipkrish
Copy link
Member Author

NOTE: Here Im using swagger to loosely mean OpenAPI

Actually,it is a lot easier to move from version to version of the spec, the hard work of understanding springs model is already done. The swagger1/swagger2 are just a project of the internal models that we create. i.e. We have our own model => swagger1 mapping layer == produces ==> swagger1
and same for swagger2.

Now our internal models are a little bit skewed because they used to mirror swagger 1 and we've added on to it more and more to expose newer concepts like examples.

The structure of swagger 3 is bit different from its predecessors so its Im not sure of the effort it will take to change the internal models. There are a lot of new concepts in the works like links/hypermedia, more rich schema support like allOf and anyOf, more rich example support. So I don't want to put a lot of effort into supporting/improving the models to handle examples etc. till I understand the impact of it. Really once its out there it is going to have to be supported :)

@dheerajuppalapati
Copy link

+1

Need support for example in Springfox
{ @ApiImplicitParam(example = "...") } does not work using Sprinfox.

@ninja-inc
Copy link

+1

1 similar comment
@RobynLiu
Copy link

RobynLiu commented Sep 8, 2017

+1

@kanashkin-fv
Copy link

+1

8 similar comments
@Spentas
Copy link

Spentas commented Jan 18, 2018

+1

@carl87gt
Copy link

+1

@schmidtlucila
Copy link

+1

@murugaraja
Copy link

+1

@VMarisevs
Copy link

+1

@YadvigaSpb
Copy link

+1

@abhilash-dev
Copy link

+1

@vsethi13
Copy link

vsethi13 commented Apr 6, 2018

+1

@dilipkrish dilipkrish added the next label Apr 8, 2018
@ghost ghost removed the next label Apr 9, 2018
@walterMPDL
Copy link

+1

@dilipkrish dilipkrish modified the milestones: Someday, 2.9.0 Apr 23, 2018
@JensSchliesser
Copy link

+1

@dilipkrish
Copy link
Member Author

@JensSchliesser +1 for what? Its already implemented.

@andrew-garland
Copy link

+1 in thanks for implementing

@darrellme
Copy link

Is there a working example of this, as I couldn't get the example stuff working and displaying in the swagger ui, and I've tried using 2.9.0

@wuhyapc
Copy link

wuhyapc commented May 29, 2018

+1 really need it

@dilipkrish
Copy link
Member Author

@darrellme There is an example in the docs and for scalar properties do this.

@Stormzcy
Copy link

+1

1 similar comment
@AndrejLukasevic
Copy link

+1

@andrey-nakin
Copy link

@dilipkrish Your example produces 'x-examples' field with object example. But 'x-examples' is not supported by SwaggerUI 3.x. Is there a working example with complex object example? Thanks

@dilipkrish
Copy link
Member Author

I'm not sure it is supported in the ui

@vaibhavn
Copy link

vaibhavn commented Nov 13, 2018

@darrellme There is an example in the docs and for scalar properties do this.

@dilipkrish Would it work for RequestBody too? I do not see any example for same.
Tried using SpringFox 2.9.2, following code for reference:
@ApiParam(name = "body",value = "Request body in JSON format...",required=true, examples = @io.swagger.annotations.Example(value= @ExampleProperty( mediaType = MediaType.APPLICATION_JSON_VALUE, value = "{foo: whatever, bar: whatever2}")) ) @RequestBody String body,

I would like to display one large JSON as example value for request body and clicking on 'Try it out' application should show same in edit mode. Please refer to following URL for my expectation behavior []https://petstore.swagger.io/#/store/placeOrder
Also note that I am not passing any objects in parameters to method, it is simple 'String' type request body as I have mentioned above in my code snippet.
Please suggest.

@Superman-IT
Copy link

No bandwidth to do this currently. Would you perhaps like to give a shot at a PR? I'd be more than happy to help answer any questions.

Instructions on how to do this in case someone wants to help

Hey have a look at these instructions to support a custom annotation for a parameter. Essentially we need to do a similar thing ... with a little bit more work.

  1. We need to add an example property to the Parameter object
  2. Add corresponding property to ParameterBuilder
  3. Add logic to map the internal Parameter model to the swagger Parameter model

We need to do the same thing for Response model as well. Update the builder and the mapper

Once we do the above we can create a ParameterBuilderPlugin for the parameters and OperationBuilderPlugin plugin for response examples.
Hi, I have a problem using Swagger.My response body in controller methods has a map.
So how can I describe each field in my response body so that others can understand.
For now ,the response example is nothing, could you give a demo to achieve it?

@Superman-IT
Copy link

Instructions on how to do this in case someone wants to help

Hey have a look at these instructions to support a custom annotation for a parameter. Essentially we need to do a similar thing ... with a little bit more work.

  1. We need to add an example property to the Parameter object
  2. Add corresponding property to ParameterBuilder
  3. Add logic to map the internal Parameter model to the swagger Parameter model

We need to do the same thing for Response model as well. Update the builder and the mapper

Once we do the above we can create a ParameterBuilderPlugin for the parameters and OperationBuilderPlugin plugin for response examples.
@dilipkrish
Hi, I have a problem using Swagger.My response body in controller methods has a map.
So how can I describe each field in my response body so that others can understand.
For now ,the response example is nothing, could you give a demo to achieve it?

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

No branches or pull requests