Swaggeratr icon indicating copy to clipboard operation
Swaggeratr copied to clipboard

Defaulting Request Data

Open harmonf opened this issue 10 years ago • 7 comments

Hello,

I'm using Swaggerator in conjunction with Swagger UI. Everything is working, but I'm wondering if it is possible to have default data. The idea would be for a 3rd party to be able to come in with some good sample data to make the method call with.

For example:

Right now, in Swagger UI when you can click on the Model Schema to set it as a parameter.

before

What would be nice is if we could either default this parameter to a set JSON string or something so it would default to something like this.

after

Not sure if there is an attribute that I missed that could do this at the DataMember level or something. I'd love if something like this was already possible.

Thanks

harmonf avatar Jul 24 '14 19:07 harmonf

Unfortunately no, but it is a good idea!

makotogitdev avatar Jul 24 '14 21:07 makotogitdev

Hello,

I've implemented this and would like to share my changes with the group. I figure you can review them and decide if you want to commit them back to the project yourselves. It works for our purposes but you may want to possibly extend it some.

Basically, I implemented a new swagger attribute type called "DocumentationSample".

image

I found that swagger-ui already supports defaulting a parameter via "defaultValue". So, at this point, it was just about creating our JSON string.

In Mapper.cs while looping through all the parameters, I first create a new instance of the parameter type using Activator.CreateInstance. I then recursively loop through all it's properties and child properties to see if they have "DocumentationSample" attributes. If they do, I set the value on that property. Finally, I serialize the object into a JSON string and set it on the "defaultValue" parameter attribute.

Let me know how you'd like me to share my changes with you.

Fred

harmonf avatar Jul 30 '14 15:07 harmonf

Sure, thanks! Are you able to commit a branch? I'm in the middle of some refactoring and cleanup in the code base, but I will look at folding it in.

On Jul 30, 2014, at 8:20 AM, harmonf [email protected] wrote:

Hello,

I've implemented this and would like to share my changes with the group. I figure you can review them and decide if you want to commit them back to the project yourselves. It works for our purposes but you may want to possibly extend it some.

Basically, I implemented a new swagger attribute type called "DocumentationSample".

I found that swagger-ui already supports defaulting a parameter via "defaultValue". So, at this point, it was just about creating our JSON string.

In Mapper.cs while looping through all the parameters, I first create a new instance of the parameter type using Activator.CreateInstance. I then recursively loop through all it's properties and child properties to see if they have "DocumentationSample" attributes. If they do, I set the value on that property. Finally, I serialize the object into a JSON string and set it on the "defaultValue" parameter attribute.

Let me know how you'd like me to share my changes with you.

Fred

— Reply to this email directly or view it on GitHub.

superstator avatar Jul 30 '14 16:07 superstator

I’ve broken up the solution into several projects so it might be easier if I just zip up what I have and tell you where my changes are. We needed to have SwaggerAttributes in a separate project so we could set its type to .Net 4.0 for other compatibility issues.

Can I email it to you somewhere?

harmonf avatar Jul 30 '14 16:07 harmonf

Yeah, [email protected]

On Jul 30, 2014, at 9:34 AM, harmonf [email protected] wrote:

I’ve broken up the solution into several projects so it might be easier if I just zip up what I have and tell you where my changes are. We needed to have SwaggerAttributes in a separate project so we could set its type to .Net 4.0 for other compatibility issues.

Can I email it to you somewhere?

— Reply to this email directly or view it on GitHub.

superstator avatar Jul 30 '14 17:07 superstator

I don't think my changes ever made it in as I probably just emailed them to you. Would you still be interested in having me commit a branch for this?

harmonf avatar Sep 14 '16 19:09 harmonf

Sorry, it's been ages since I looked at this; we moved over to WebAPI and haven't needed to touch WCF since. Feel free to commit a branch and I'll try to take a look at it, or you may want to just fork from here and do your own thing without waiting on me...

superstator avatar Sep 16 '16 17:09 superstator