Can I add specification extensions to my schema?

The spec allows x- prefixed extensions to the schema objects - sometimes called vendor extensions. I have been using the ApiDeclarationFilter to modify the JSON as needed but I’ve started looking at using ReDoc and noticed that since the .net objects are simple classes, I can’t add properties on the fly. Do you have have recommendations on how I can accomplish this?

Thanks!

That’s a question for ReDoc, do they support it?

Sorry, I probably wasn’t specific enough/very clear in what I was asking. ReDoc has a set of vendor extensions to the open api 2.0 spec. For example they have a property in the info JSON which is x-logo. Is there a way I can add this to the JSON returned from the ~/openapi endpoint?

For example:

{
  "info": {
    "version": "1.0.0",
    "title": "Swagger Petstore",
    "x-logo": {
      "url": "https://rebilly.github.io/ReDoc/petstore-logo.png",
      "backgroundColor": "#FFFFFF"
    }
  }
}

I can’t just add the property in the ApiDeclarationFilter because I can’t add the property to the OpenApiInfo class without creating a custom build. There are other extensions listed here if that helps clarify: https://github.com/Rebilly/ReDoc/blob/master/docs/redoc-vendor-extensions.md

Edit - I’m not asking for added support - I’m looking for whether I can manipulate the openapi response outside of the rigid class structure used in the plug-in, more something like a Json Object before it’s returned to the caller.

No, the typed DTOs are what’s serialized to generate the Open API JSON specification. You would need to capture the generated output and manually modify the JSON out-of-band to add the vendor extensions.

In case this is helpful to anyone else - I came up with a work around by exposing the extended schema for ReDoc as a separate endpoint and calling the open api service within that endpoint. I.e.

        public HttpResult Get(ReDocReq req)
        {
            using (var service = base.ResolveService<OpenApiService>())
            {
                var resp = service.Get(new OpenApiSpecification());
                var res = resp as HttpResult;
                var oApiObj = res?.Response as OpenApiDeclaration;

                var json = JsonObject.Parse(oApiObj.ToJson());

                ... add vendor extensions  ...
                
                return new HttpResult(json, "application/json");
            }
            
        }

Thanks!

2 Likes