-1

Please help me out in implementing default request data to the model schema on the Swagger UI with Swagger 6.0.0 with C#.

U rao
  • 49
  • 8

1 Answers1

0

Redoing the answer to this to make it more clear.

1) you'll need to create a RequestExampleAttribute class that just keeps the example class type similar to the link you sent.

2) decorate the action method with the RequestExampleAttribute:

[RequestExample(typeof(MyRequestExample))]

3) MyRequestExample is a simple class derived from your model, except it has all the properties populated with example values in the constructor

4) Create the operation filter as in your link

5) In the operation filter, find the attribute (should only be one or none):

RequestExampleAttribute exampleAttr = context.ApiDescription.GetActionAttributes().Where(x => x is RequestExampleAttribute).Cast<RequestExampleAttribute>().FirstOrDefault();

6) if exampleAttr is null, return, otherwise, new up an instance of the type with Activator.CreateInstance()

7) set context.SchemaRegistry.Definitions[0].Example to instance created.

object oExample = Activator.CreateInstance(exampleAttr.ExampleType);

context.SchemaRegistry.Definitions[0].Example = oExample;

8) check if it works... if not, continue to step 9

9) if its not reading from ...[0].Example, it is reading from the property schema, so you'll need to modify all the properties:

context.SchemaRegistry.Definitions[0].Properties[x].Value.Example

This is just a dictionary key/value pair with the property name in the key. What I did in my code was I loop through the dictionary keys and then use regex to pull the value from the serialized instance created in step #6. I.e.

{
  "prop1" : "value1"
}

so when you loop through the dictionary, the first key will be "prop1" for example, so you can use regex to pull "prop1": [string] and then copy that string to the Value.Example for the prop.

Hopefully you won't need to do that since the successful return model reads from the "easy" place to modify, so hopefully the request will also pull from the "easy" place.

Make sense now?

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
  • @Urao Where are you getting an int and string from? For step #7, context.SchemaRegistry.Definitions[0].Example = oExample;, both sides are object. This part didn't work? You might not need to do 8/9. context.SchemaRegistry.Definitions[0].Properties[x].Value.Ex‌​ample is also an object. – SledgeHammer Sep 20 '16 at 23:12
  • Oh, sorry... I was looking in the debugger, you can do context.SchemaRegistry.Definitions.First().... (with using System.Linq)... same for step #9 if you need to go that far. – SledgeHammer Sep 21 '16 at 00:20
  • @Urao in my original response for the response examples, the main example was read from place #1, but the others were read from place #2 because you can have multples but on the request you can only have one so I'm guessing it will read from place #1. Stop at 7 and try it. If it doesn't read it from there then go on to the 8/9. – SledgeHammer Sep 21 '16 at 00:52
  • @Urao, works for me. Are you sure you put the attribute on the method? If you have multiple controllers/methods where you *haven't* put the attribute, it will be null. – SledgeHammer Sep 21 '16 at 14:15
  • @Urao what is in context.ApiDescription.GetActionAttributes()? This all works for me. Turn on first chance exceptions to see what the exception is. – SledgeHammer Sep 21 '16 at 15:43
  • the declaring method in ResponseType under example attr has this error : '((System.RuntimeType)exampleAttr.ResponseType).DeclaringMethod' threw an exception of type 'System.InvalidOperationException' – U rao Sep 21 '16 at 15:47
  • @Urao not sure what you are doing lol... please update your question and post your operation filter, attribute and the stripped down controller. – SledgeHammer Sep 21 '16 at 16:50
  • @Urao, if you just put var v = context.ApiDescription.GetActionAttributes(); and break point on there, what do you see inside v? I have an array of System.Attribute. Where is your HttpPost / HttpGet attribute on the method? Why is your custom attribute specifying the type twice? – SledgeHammer Sep 21 '16 at 17:13
  • @Urao what is in context.ApiDescription.GetActionAttributes()? do you see the array of attributes? – SledgeHammer Sep 21 '16 at 17:46
  • Ok so it's working. Can you debug why the where clause isn't picking up the reqmodelexample attribute? Break it up into pieces to see which part isn't working. – SledgeHammer Sep 21 '16 at 18:06
  • @Urao do you see any output in the debugger output? or through first chance exceptions? If you are getting a 500, it should throw a first chance exception. – SledgeHammer Sep 21 '16 at 18:26
  • @Urao, ok, got sick of going back and forth, so I tested it myself. I just did context.SchemaRegistry.Definitions.First().Value.Example = new OrderRequest() { PostbackUri = new Uri("http://localhost") }; and it showed up as an example just fine. I would start with something similiar, just hardcode it and get it working and then move to the dynamic way. – SledgeHammer Sep 21 '16 at 19:31