public JObject GetSwagger() { IEdmModel model = Request.GetModel(); model.SetEdmxVersion(_defaultEdmxVersion); ODataSwaggerConverter converter = new ODataSwaggerConverter(model); return(converter.GetSwaggerModel()); }
public JObject GetSwagger() { IEdmModel model = Request.ODataProperties().Model; model.SetEdmxVersion(_defaultEdmxVersion); ODataSwaggerConverter converter = new ODataSwaggerConverter(model); return(converter.ConvertToSwaggerModel()); }
private IEdmModel GetEdmModel() { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); // Register entities which has GeneratedController attribute MethodInfo registerMethod = builder.GetType().GetMethod("EntitySet", new Type[] { typeof(String) }); var currentAssembly = typeof(Startup).Assembly; var candidates = currentAssembly.GetExportedTypes().Where(x => x.GetCustomAttributes <Helpers.GeneratedControllerAttribute>().Any()); foreach (var candidate in candidates) { MethodInfo genericMethod = registerMethod.MakeGenericMethod(candidate); genericMethod.Invoke(builder, new object[] { candidate.Name }); } var model = builder.GetEdmModel(); var converter = new ODataSwaggerConverter(model); var swaggerModel = converter.GetSwaggerModel(); var swaggerPaths = (JObject)swaggerModel.GetValue("paths"); List <JProperty> childrenToRemove = new List <JProperty>(); foreach (var property in swaggerPaths.Properties()) { var path = property.Value <JToken>(); var obj = (JObject)path.FirstOrDefault(); foreach (var child in obj.Children <JProperty>()) { if (child.Name == "patch") { childrenToRemove.Add(child); } } } childrenToRemove.ForEach((child) => { child.Remove(); }); var swaggerPath = Path.Combine(JsonPath, "swagger.json"); File.WriteAllText(swaggerPath, converter.GetSwaggerModel().ToString(), Encoding.UTF8); return(model); }
public void ODataSwaggerConverterTest_Works() { // Arrange string expect = @"{ ""swagger"": ""2.0"", ""info"": { ""title"": ""OData Service"", ""description"": ""The OData Service at http://localhost/"", ""version"": ""0.1.0"", ""x-odata-version"": ""4.0"" }, ""host"": ""default"", ""schemes"": [ ""http"" ], ""basePath"": ""/odata"", ""consumes"": [ ""application/json"" ], ""produces"": [ ""application/json"" ], ""paths"": { ""/Customers"": { ""get"": { ""summary"": ""Get EntitySet Customers"", ""operationId"": ""Customers_Get"", ""description"": ""Returns the EntitySet Customers"", ""tags"": [ ""Customers"" ], ""parameters"": [ { ""name"": ""$expand"", ""in"": ""query"", ""description"": ""Expand navigation property"", ""type"": ""string"" }, { ""name"": ""$select"", ""in"": ""query"", ""description"": ""select structural property"", ""type"": ""string"" }, { ""name"": ""$orderby"", ""in"": ""query"", ""description"": ""order by some property"", ""type"": ""string"" }, { ""name"": ""$top"", ""in"": ""query"", ""description"": ""top elements"", ""type"": ""integer"" }, { ""name"": ""$skip"", ""in"": ""query"", ""description"": ""skip elements"", ""type"": ""integer"" }, { ""name"": ""$count"", ""in"": ""query"", ""description"": ""include count in response"", ""type"": ""boolean"" } ], ""responses"": { ""200"": { ""description"": ""EntitySet Customers"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Customer"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } }, ""post"": { ""summary"": ""Post a new entity to EntitySet Customers"", ""operationId"": ""Customers_Post"", ""description"": ""Post a new entity to EntitySet Customers"", ""tags"": [ ""Customers"" ], ""parameters"": [ { ""name"": ""Customer"", ""in"": ""body"", ""description"": ""The entity to post"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Customer"" } } ], ""responses"": { ""200"": { ""description"": ""EntitySet Customers"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Customer"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } }, ""/Customers({CustomerId})"": { ""get"": { ""summary"": ""Get entity from Customers by key."", ""operationId"": ""Customers_GetById"", ""description"": ""Returns the entity with the key from Customers"", ""tags"": [ ""Customers"" ], ""parameters"": [ { ""name"": ""CustomerId"", ""in"": ""path"", ""description"": ""key: CustomerId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""$select"", ""in"": ""query"", ""description"": ""description"", ""type"": ""string"" } ], ""responses"": { ""200"": { ""description"": ""EntitySet Customers"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Customer"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } }, ""patch"": { ""summary"": ""Update entity in EntitySet Customers"", ""operationId"": ""Customers_PatchById"", ""description"": ""Update entity in EntitySet Customers"", ""tags"": [ ""Customers"" ], ""parameters"": [ { ""name"": ""CustomerId"", ""in"": ""path"", ""description"": ""key: CustomerId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""Customer"", ""in"": ""body"", ""description"": ""The entity to patch"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Customer"" } } ], ""responses"": { ""204"": { ""description"": ""Empty response"" }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } }, ""delete"": { ""summary"": ""Delete entity in EntitySet Customers"", ""operationId"": ""Customers_DeleteById"", ""description"": ""Delete entity in EntitySet Customers"", ""tags"": [ ""Customers"" ], ""parameters"": [ { ""name"": ""CustomerId"", ""in"": ""path"", ""description"": ""key: CustomerId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""If-Match"", ""in"": ""header"", ""description"": ""If-Match header"", ""type"": ""string"" } ], ""responses"": { ""204"": { ""description"": ""Empty response"" }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } }, ""/Orders"": { ""get"": { ""summary"": ""Get EntitySet Orders"", ""operationId"": ""Orders_Get"", ""description"": ""Returns the EntitySet Orders"", ""tags"": [ ""Orders"" ], ""parameters"": [ { ""name"": ""$expand"", ""in"": ""query"", ""description"": ""Expand navigation property"", ""type"": ""string"" }, { ""name"": ""$select"", ""in"": ""query"", ""description"": ""select structural property"", ""type"": ""string"" }, { ""name"": ""$orderby"", ""in"": ""query"", ""description"": ""order by some property"", ""type"": ""string"" }, { ""name"": ""$top"", ""in"": ""query"", ""description"": ""top elements"", ""type"": ""integer"" }, { ""name"": ""$skip"", ""in"": ""query"", ""description"": ""skip elements"", ""type"": ""integer"" }, { ""name"": ""$count"", ""in"": ""query"", ""description"": ""include count in response"", ""type"": ""boolean"" } ], ""responses"": { ""200"": { ""description"": ""EntitySet Orders"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Order"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } }, ""post"": { ""summary"": ""Post a new entity to EntitySet Orders"", ""operationId"": ""Orders_Post"", ""description"": ""Post a new entity to EntitySet Orders"", ""tags"": [ ""Orders"" ], ""parameters"": [ { ""name"": ""Order"", ""in"": ""body"", ""description"": ""The entity to post"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Order"" } } ], ""responses"": { ""200"": { ""description"": ""EntitySet Orders"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Order"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } }, ""/Orders({OrderId})"": { ""get"": { ""summary"": ""Get entity from Orders by key."", ""operationId"": ""Orders_GetById"", ""description"": ""Returns the entity with the key from Orders"", ""tags"": [ ""Orders"" ], ""parameters"": [ { ""name"": ""OrderId"", ""in"": ""path"", ""description"": ""key: OrderId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""$select"", ""in"": ""query"", ""description"": ""description"", ""type"": ""string"" } ], ""responses"": { ""200"": { ""description"": ""EntitySet Orders"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Order"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } }, ""patch"": { ""summary"": ""Update entity in EntitySet Orders"", ""operationId"": ""Orders_PatchById"", ""description"": ""Update entity in EntitySet Orders"", ""tags"": [ ""Orders"" ], ""parameters"": [ { ""name"": ""OrderId"", ""in"": ""path"", ""description"": ""key: OrderId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""Order"", ""in"": ""body"", ""description"": ""The entity to patch"", ""schema"": { ""$ref"": ""#/definitions/Microsoft.Test.AspNet.OData.Order"" } } ], ""responses"": { ""204"": { ""description"": ""Empty response"" }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } }, ""delete"": { ""summary"": ""Delete entity in EntitySet Orders"", ""operationId"": ""Orders_DeleteById"", ""description"": ""Delete entity in EntitySet Orders"", ""tags"": [ ""Orders"" ], ""parameters"": [ { ""name"": ""OrderId"", ""in"": ""path"", ""description"": ""key: OrderId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""If-Match"", ""in"": ""header"", ""description"": ""If-Match header"", ""type"": ""string"" } ], ""responses"": { ""204"": { ""description"": ""Empty response"" }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } }, ""/UnboundFunction(param={param})"": { ""get"": { ""summary"": ""Call operation import UnboundFunction"", ""operationId"": ""UnboundFunction_FunctionImportGet"", ""description"": ""Call operation import UnboundFunction"", ""tags"": [ ""Function Import"" ], ""parameters"": [ { ""name"": ""param"", ""in"": ""path"", ""description"": ""parameter: param"", ""type"": ""integer"", ""format"": ""int32"" } ], ""responses"": { ""200"": { ""description"": ""Response from UnboundFunction"", ""schema"": { ""type"": ""string"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } }, ""/UnboundAction()"": { ""post"": { ""summary"": ""Call operation import UnboundAction"", ""operationId"": ""UnboundAction_ActionImportPost"", ""description"": ""Call operation import UnboundAction"", ""tags"": [ ""Action Import"" ], ""parameters"": [ { ""name"": ""param"", ""in"": ""body"", ""description"": ""parameter: param"", ""schema"": { ""type"": ""number"", ""format"": ""double"" } } ], ""responses"": { ""204"": { ""description"": ""Empty response"" }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } }, ""/Customers({CustomerId})/Default.BoundFunction(name='{name}')"": { ""get"": { ""summary"": ""Call operation BoundFunction"", ""operationId"": ""BoundFunction_FunctionGetById"", ""description"": ""Call operation BoundFunction"", ""tags"": [ ""Customers"", ""Function"" ], ""parameters"": [ { ""name"": ""CustomerId"", ""in"": ""path"", ""description"": ""key: CustomerId"", ""type"": ""integer"", ""format"": ""int32"" }, { ""name"": ""name"", ""in"": ""path"", ""description"": ""parameter: name"", ""type"": ""string"" } ], ""responses"": { ""200"": { ""description"": ""Response from BoundFunction"", ""schema"": { ""type"": ""number"", ""format"": ""double"" } }, ""default"": { ""description"": ""Unexpected error"", ""schema"": { ""$ref"": ""#/definitions/_Error"" } } } } } }, ""definitions"": { ""Microsoft.Test.AspNet.OData.Customer"": { ""properties"": { ""CustomerId"": { ""description"": ""CustomerId"", ""type"": ""integer"", ""format"": ""int32"" } } }, ""Microsoft.Test.AspNet.OData.Order"": { ""properties"": { ""OrderId"": { ""description"": ""OrderId"", ""type"": ""integer"", ""format"": ""int32"" } } }, ""_Error"": { ""properties"": { ""error"": { ""$ref"": ""#/definitions/_InError"" } } }, ""_InError"": { ""properties"": { ""code"": { ""type"": ""string"" }, ""message"": { ""type"": ""string"" } } } } }"; ODataSwaggerConverter converter = new ODataSwaggerConverter(_model); // Act JObject obj = converter.GetSwaggerModel(); // Assert Assert.NotNull(obj); Assert.Equal(expect, obj.ToString()); }