发布于 2025-02-08 03:39:40 字数 2615 浏览 2 评论 0原文

我正在使用Swagger来生成我的React应用程序的端点。为此,我需要为我的端点和具有[EnableQuery]注释的端点之一指定响应对象,这意味着是ODATATACONTROLLER中放置的ODATA端点,没有返回正确的格式。我现在返回的是,

{
  "ODataContext": "string",
  "ODataCount": 0,
  "ODataNextLink": "string",
  "Value": List<Project>
}

而不是

{
  "@odata.context": "string",
  "@odata.count": 0,
  "@odata.nextLink": "string",
  "value": List<Project>
}

我遵循此问题我使用的代码是,

[EnableQuery(PageSize = ITEMS_PER_PAGE)]
[ProducesResponseType(typeof(ODataValueWithCount<IEnumerable<Project>>), 200)]
public IQueryable<Project> GetAsync()
{
    return _projectRepository.GetProjects();
}
internal class ODataValueWithCount<T> : ODataValue
{
    [JsonProperty("@odata.context")]
    public string ODataContext { get; set; }
    [JsonProperty("@odata.count")]
    public int ODataCount { get; set; }
    [JsonProperty("@odata.nextLink")]
    public string ODataNextLink { get; set; }
    [JsonProperty("value")]
    public List<Project> Value { get; set; }
}
private static IEdmModel GetEdmModel()
{
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Project>("Projects");
    return builder.GetEdmModel();
}

但是这仍然使我返回了我在这里指出的第一个对象,即使该问题似乎是在此处使用的解决方案。您知道我在做什么错,我该如何解决? 更多代码来自startup.cs

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1");
});

services.AddSwaggerGen(swagger =>
{
    swagger.OperationFilter<ODataOperationFilter>();
    swagger.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "0.0.1" });
    swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Description = "Please insert JWT with Bearer into field",
        Name = "Authorization",
        Type = SecuritySchemeType.ApiKey
    });
    swagger.AddSecurityRequirement(new OpenApiSecurityRequirement 
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                    {
                        Type = ReferenceType.SecurityScheme,
                        Id = "Bearer"
                    }
            },
        new string[] { }
        }
    });
    swagger.ResolveConflictingActions(apiDescription => apiDescription.First());
});

I'm using swagger in order to generate the endpoints for my React app. In order to do that, I need to specify the response objects for my endpoints and one of my endpoints that has [EnableQuery] annotation, which means is an OData endpoint placed inside an ODataController, doesn't return the right format. What I return now is

{
  "ODataContext": "string",
  "ODataCount": 0,
  "ODataNextLink": "string",
  "Value": List<Project>
}

instead of

{
  "@odata.context": "string",
  "@odata.count": 0,
  "@odata.nextLink": "string",
  "value": List<Project>
}

I followed this issue from GitHub and the code I used is

[EnableQuery(PageSize = ITEMS_PER_PAGE)]
[ProducesResponseType(typeof(ODataValueWithCount<IEnumerable<Project>>), 200)]
public IQueryable<Project> GetAsync()
{
    return _projectRepository.GetProjects();
}
internal class ODataValueWithCount<T> : ODataValue
{
    [JsonProperty("@odata.context")]
    public string ODataContext { get; set; }
    [JsonProperty("@odata.count")]
    public int ODataCount { get; set; }
    [JsonProperty("@odata.nextLink")]
    public string ODataNextLink { get; set; }
    [JsonProperty("value")]
    public List<Project> Value { get; set; }
}
private static IEdmModel GetEdmModel()
{
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Project>("Projects");
    return builder.GetEdmModel();
}

but this still somehow returns me the first object that I noted here even though the solution from the issue seems as it's working there. Do you know what am I doing wrong and how can I fix this?
More code from Startup.cs:

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("v1/swagger.json", "MyAPI V1");
});

services.AddSwaggerGen(swagger =>
{
    swagger.OperationFilter<ODataOperationFilter>();
    swagger.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "0.0.1" });
    swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Description = "Please insert JWT with Bearer into field",
        Name = "Authorization",
        Type = SecuritySchemeType.ApiKey
    });
    swagger.AddSecurityRequirement(new OpenApiSecurityRequirement 
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                    {
                        Type = ReferenceType.SecurityScheme,
                        Id = "Bearer"
                    }
            },
        new string[] { }
        }
    });
    swagger.ResolveConflictingActions(apiDescription => apiDescription.First());
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文