Hi
For a simple test project i created a REST-api with only some endpoints. But when adding this REST-api into AppBuilder the endpoints "Employees" are visible but not selectable - they are grey and looks disabled:
swagger-definition:
{ openapi: "3.0.1", info: { title: "FullStack.API", version: "1.0" }, paths: { /api/Employees: { get: { tags: [ "Employees" ], responses: { 200: { description: "Success" } } }, post: { tags: [ "Employees" ], requestBody: { content: { application/json: { schema: { $ref: "#/components/schemas/Employee" } }, text/json: { schema: { $ref: "#/components/schemas/Employee" } }, application/*+json: { schema: { $ref: "#/components/schemas/Employee" } } } }, responses: { 200: { description: "Success" } } } }, /api/Employees/{id}: { get: { tags: [ "Employees" ], parameters: [ { name: "id", in: "path", required: true, schema: { type: "string", format: "uuid" } } ], responses: { 200: { description: "Success" } } }, put: { tags: [ "Employees" ], parameters: [ { name: "id", in: "path", required: true, schema: { type: "string", format: "uuid" } } ], requestBody: { content: { application/json: { schema: { $ref: "#/components/schemas/Employee" } }, text/json: { schema: { $ref: "#/components/schemas/Employee" } }, application/*+json: { schema: { $ref: "#/components/schemas/Employee" } } } }, responses: { 200: { description: "Success" } } }, delete: { tags: [ "Employees" ], parameters: [ { name: "id", in: "path", required: true, schema: { type: "string", format: "uuid" } } ], responses: { 200: { description: "Success" } } } }, /api/TzGemeinden: { get: { tags: [ "TzGemeinden" ], responses: { 200: { description: "Success" } } } }, /WeatherForecast: { get: { tags: [ "WeatherForecast" ], operationId: "GetWeatherForecast", responses: { 200: { description: "Success", content: { text/plain: { schema: { type: "array", items: { $ref: "#/components/schemas/WeatherForecast" } } }, application/json: { schema: { type: "array", items: { $ref: "#/components/schemas/WeatherForecast" } } }, text/json: { schema: { type: "array", items: { $ref: "#/components/schemas/WeatherForecast" } } } } } } } } }, components: { schemas: { Employee: { type: "object", properties: { id: { type: "string", format: "uuid" }, name: { type: "string", nullable: true }, email: { type: "string", nullable: true }, phone: { type: "integer", format: "int64" }, salary: { type: "integer", format: "int64" }, department: { type: "string", nullable: true } }, additionalProperties: false }, WeatherForecast: { type: "object", properties: { date: { type: "string", format: "date-time" }, temperatureC: { type: "integer", format: "int32" }, temperatureF: { type: "integer", format: "int32", readOnly: true }, summary: { type: "string", nullable: true } }, additionalProperties: false } } } }
Any idea whats going wrong here?
Thanks for any support.
br, Robert
Hey Robert,
Currently, AppBuilder only support endpoints returning array or object. If the greyed out endpoints are ones that return primitive types, this is the reason for them to appear greyed out, let me verify that and provide more info. Meanwhile, is it possible that you reurn the "original" OpenAPi definiton file? It will contain quotes around namings, which is essential when parsing the content as JSON.
Hey Robert,I did not heard from you, but I would not like you to wait for answer.I once again investigated the definition. It actually is lacking a description of the response for the Employees endpoint (possibly for other endpoints too):
The yellow part is a good description inlcluding the content object with description and a reference to the schema of the expected response, which would be smth in the lines of:I see there is an "Employee" schema in your definiton, so maybe this is what describes the Employees endpoint. Try to edit your definition and replace lines 65-70 with the following:
"responses": { "200": { "description": "Success", "content": { "application/json": { "schema": { "type": "array", "items": { "$ref": "#/components/schemas/Employee" } } } } } }
Hello sorry to jump in here but I have the same issue. I think I understand what you are saying Hristo, (I'm an old programmer learning new tricks here!!) and I noticed that even in Jason Beres video https://www.youtube.com/watch?v=d9X0Q5iY8OE&t=2039s here at 34:07 you can see that several of the endpoints are greyed out - those greyed related to the changes he did at 25:13 when instead of returning a simple list (as I understand it) he is now returning a Http Results. Have I undersood that correctly?
If so, then my code is as below. Where my endpoint calls a function Data.PersonGetAll which itself calls a stored procedure via Dapper. But I think the issue is returning an IResult?
// this line part of Configure endpoint function
// then below
How can I change that to return something that AppBuilder can work with?
Thanks, And I appreciate this may be beyond the bounds of this support forum.
Thanks, Anthony
Hi Anthony,Sorry that we missed your last reply.
Anthony Marler said:But I think the issue is returning an IResult?
Exactly. Results.Ok() is an old method and loses all the type information which swagger (OpenAPI) needs to produce the correct metadata and App Builder to be able to "see" the return types.
In .Net 7+ you need to either:
Any of those will be able to generate the correct OpenAPI Schema, and App Builder to consume it.
You can read more about typed results in .Net documentation TypedResults vs Results.
Regards,Pablo
Hi thanks for the reply Pablo. I will just point out that my current code using Results.OK does work fine with Swagger,and with other projects that call the REST endpoints. I get back proper return codes 200, 404 etc. and Json data and the schema shows in Swagger too.
The documentation link you referred me to states. "Minimal endpoints support the following types of return values:"
string
Task<string>
ValueTask<string>
T
Task<T>
ValueTask<T>
IResult
Task<IResult>
ValueTask<IResult>
So I would have expected AppBuilder should support Task<IResult> as I think that is where the issue is. Even if I change the return Results.OK(...) to return TypedResults.OK(...) app builder stays greyed out I suspect because the PersonGetAll() function returns a Task<IResult>.
My C# knowledge is far from complete as mentioned above. Any ideas how I can refactor those calls as all my many endpoints work in this manner. Thanks.
Anthony,
It's not about App Builder supporting IResult, it's about feeding Swagger with information to properly describe the response type.
e.g.
When using IResult with no additional information this area will be empty.
Anthony Marler said:Even if I change the return Results.OK(...) to return TypedResults.OK(...) app builder stays greyed out I suspect because the PersonGetAll() function returns a Task<IResult>
If you only return OK results, then change it to Task<Ok<YourResponseType>> like in the docs:
public static async Task<Ok<Todo[]>> GetAllTodos(TodoGroupDbContext database) { var todos = await database.Todos.ToArrayAsync(); return TypedResults.Ok(todos); }
If you need to keep IResult, then you need to add a .Produces<>() statement:
Consider the following endpoint, for which a 400 BadRequest status code is returned when the orderId is greater than 999. Otherwise, it produces a 200 OK with the expected content. C# app.MapGet("/orders/{orderId}", IResult (int orderId) => orderId > 999 ? TypedResults.BadRequest() : TypedResults.Ok(new Order(orderId))) .Produces(400) .Produces<Order>();
I hope this helps.
Thanks for continuing to help me. Getting there now. Here is the code and I am now returning a PersonModel using Produces statement.
This still works with Swagger
And in AppBuilder almost but...
Then I can't bind to the grid as now options sow against the source. "This resource contains a single object" indicates my Produces isn't right I guess?
Anthony
PS. It's early hours of the morning here so I'll pick this up tomorrow. Thanks again.
Haha. Glad to have helped you.
Cheers
Bingo!! It works!! Pablo if we should ever meet I owe you an obscene amount of alcohol!! In the meantime, please accept my sincere thanks at spending the time to walk me through this with my, still limited, knowledge of C#. Having this problem, and then your advice has actually taught me a lot. Many thanks.
Anthony Marler said:This resource contains a single object" indicates my Produces isn't right I guess?
Yes, since you're trying to return a collection, your type should describe some type of collection or enumerable.
e.g. .Produces<IEnumerable<PersonModel>>()