3

Is it possible to customize the generate Swagger documentation for ASP.NET CORE (C#)? Specifically, it seems to be changing the order properties are displayed in my model (i.e. it puts derived class properties first).

class BaseObj
{
   string Username {get;set;}
   string Password {get;set;}
}

class Obj2 : BaseObj
{
    string SomeotherProp {get;set;}
}

Swagger generates:

SomeotherProp
Username
Password

I want SomeotherProp to be at the bottom. I've tried using the Display(Order=1) attribute, but Swagger ignores that. I didn't see any hook in the configuration that I can custom sort.

6
  • Don't force tags into question title stackoverflow.com/help/tagging
    – Tseng
    Aug 26, 2016 at 6:13
  • the json file produced by swagger is called swagger schema. The swagger documentation consists of two parts. the schema file, generated by the server and a couple of static html/css/json files that display it. The swagger schema you can control via c# code on the server side, the ui is a bit more difficult since it's embedded in Swashbuckle dll
    – Tseng
    Aug 26, 2016 at 14:18
  • @Tseng, well that was a much more constructive response, don't you think? So is there a way to fix / control the issue I have indicated? Aug 26, 2016 at 14:55
  • Well, the above being said, first check if the properties are being sorted this way in the json file that the web app produces or if the sortings happend on the swagger ui. Actions for example are sorted on swagger-ui part (in the static java script files). Can only offer a hint, no complete answer/solution for reordering the swagger schema, there are two interfaces: IOperationFilter and IDocumentFilter. For actions/controller "display" you'd use IDocument, to change stuff within the actions you use the IOperationFilter
    – Tseng
    Aug 26, 2016 at 15:57
  • Here is a bit dated example on how to use IOperationFilter to change name of the property github.com/domaindrivendev/Swashbuckle/issues/… but should give you a very rough idea on how to approach it
    – Tseng
    Aug 26, 2016 at 15:58

1 Answer 1

1

After a lot of research and trial and error, I stumbled across the answer. Swagger doesn't actually reflect upon your types directly, rather it uses Json.Net to get the type schema. Json.Net respects the JsonProperty attribute. So, on my base type, I can set the JsonProperty=-2 on the properties to get them to show first. Note that you have to use -2 and not -1 since that is reserved. By using -2, you don't have to set JsonProperty on all the derived types.

This will work in my case, but I found another post where the guy defined a custom contract resolver and sorted the properties there... that'll be more generic and cleaner then JsonProperty. Need to figure out how to hook that into Asp.net core though.

But anyways, the point is, the funky ordering is coming from Json.net and not Swagger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.