winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.06k stars 198 forks source link

Invalid console view when reusing handlers in API #7203

Open ekeren opened 3 weeks ago

ekeren commented 3 weeks ago

I tried this:

bring cloud;

let api = new cloud.Api();
let handler = inflight () => {};

api.get("/shared-handler" , handler);
api.post("/shared-handler" , handler);
api.get("/private-handler" , inflight () => {}); 
api.post("/private-handler" , inflight () => {}); 

This happened:

image

notice missing POST on shared-handler

I expected this:

image

Is there a workaround?

No response

Anything else?

No response

Wing Version

0.85.22

Node.js Version

No response

Platform(s)

No response

Community Notes

eladb commented 3 weeks ago

How does this behave at runtime? Is this just a console issue?

ekeren commented 3 weeks ago
bring cloud;
bring http;
bring expect;

let api = new cloud.Api();
let handler = inflight () => {return { status: 200};};

api.get("/shared-handler" , handler);
api.post("/shared-handler" , handler);
api.get("/private-handler" , inflight () => {return { status: 200};}); 
api.post("/private-handler" , inflight () => {return { status: 200};}); 

test "A" {
  expect.equal(
    http.post("{api.url}/shared-handler").status,
    http.post("{api.url}/private-handler").status
  ); 

  expect.equal(
    http.get("{api.url}/shared-handler").status,
    http.get("{api.url}/private-handler").status
  ); 
}

The test pass, so it is just a console issue

eladb commented 3 weeks ago

@skyrpex can you take a quick look?

skyrpex commented 2 weeks ago

I think it's on the previous parts of the toolchain. I modified the example a bit:

bring cloud;

let api = new cloud.Api();
let handler = inflight () => {};

api.get("/my-shared-handler1" , handler);
api.post("/my-shared-handler2" , handler);
api.get("/my-private-handler3" , inflight () => {}); 
api.post("/my-private-handler4" , inflight () => {}); 

and then run wing compile demo/main.w -t sim, the tree.json file is missing the second shared handler (look for my-shared-handler2):

{
  "version": "tree-0.1",
  "tree": {
    "id": "root",
    "path": "root",
    "children": {
      "Default": {
        "id": "Default",
        "path": "root/Default",
        "children": {
          "Api": {
            "id": "Api",
            "path": "root/Default/Api",
            "children": {
              "Endpoint": {
                "id": "Endpoint",
                "path": "root/Default/Api/Endpoint",
                "constructInfo": {
                  "fqn": "@winglang/sdk.cloud.Endpoint",
                  "version": "0.0.0"
                },
                "display": {
                  "title": "Endpoint",
                  "description": "Api root/Default/Api",
                  "hidden": true
                }
              },
              "Policy": {
                "id": "Policy",
                "path": "root/Default/Api/Policy",
                "constructInfo": {
                  "fqn": "@winglang/sdk.sim.Policy",
                  "version": "0.0.0"
                },
                "display": {
                  "title": "Policy",
                  "description": "A simulated resource policy",
                  "hidden": true
                }
              },
              "OnRequestHandler0": {
                "id": "OnRequestHandler0",
                "path": "root/Default/Api/OnRequestHandler0",
                "constructInfo": {
                  "fqn": "@winglang/sdk.cloud.Function",
                  "version": "0.0.0"
                },
                "display": {
                  "title": "GET /my-shared-handler1",
                  "description": "A cloud function (FaaS)",
                  "sourceModule": "@winglang/sdk"
                }
              },
              "ApiEventMapping0": {
                "id": "ApiEventMapping0",
                "path": "root/Default/Api/ApiEventMapping0",
                "constructInfo": {
                  "fqn": "@winglang/sdk.std.Resource",
                  "version": "0.0.0"
                },
                "display": {
                  "hidden": true
                }
              },
              "OnRequestHandler1": {
                "id": "OnRequestHandler1",
                "path": "root/Default/Api/OnRequestHandler1",
                "constructInfo": {
                  "fqn": "@winglang/sdk.cloud.Function",
                  "version": "0.0.0"
                },
                "display": {
                  "title": "GET /my-private-handler3",
                  "description": "A cloud function (FaaS)",
                  "sourceModule": "@winglang/sdk"
                }
              },
              "ApiEventMapping1": {
                "id": "ApiEventMapping1",
                "path": "root/Default/Api/ApiEventMapping1",
                "constructInfo": {
                  "fqn": "@winglang/sdk.std.Resource",
                  "version": "0.0.0"
                },
                "display": {
                  "hidden": true
                }
              },
              "OnRequestHandler2": {
                "id": "OnRequestHandler2",
                "path": "root/Default/Api/OnRequestHandler2",
                "constructInfo": {
                  "fqn": "@winglang/sdk.cloud.Function",
                  "version": "0.0.0"
                },
                "display": {
                  "title": "POST /my-private-handler4",
                  "description": "A cloud function (FaaS)",
                  "sourceModule": "@winglang/sdk"
                }
              },
              "ApiEventMapping2": {
                "id": "ApiEventMapping2",
                "path": "root/Default/Api/ApiEventMapping2",
                "constructInfo": {
                  "fqn": "@winglang/sdk.std.Resource",
                  "version": "0.0.0"
                },
                "display": {
                  "hidden": true
                }
              }
            },
            "constructInfo": {
              "fqn": "@winglang/sdk.cloud.Api",
              "version": "0.0.0"
            },
            "display": {
              "title": "Api",
              "description": "A REST API endpoint"
            }
          },
          "$Closure1_0": {
            "id": "$Closure1_0",
            "path": "root/Default/$Closure1_0",
            "constructInfo": {
              "fqn": "@winglang/sdk.std.AutoIdResource",
              "version": "0.0.0"
            },
            "display": {
              "hidden": true
            }
          },
          "$Closure2_0": {
            "id": "$Closure2_0",
            "path": "root/Default/$Closure2_0",
            "constructInfo": {
              "fqn": "@winglang/sdk.std.AutoIdResource",
              "version": "0.0.0"
            },
            "display": {
              "hidden": true
            }
          },
          "$Closure3_0": {
            "id": "$Closure3_0",
            "path": "root/Default/$Closure3_0",
            "constructInfo": {
              "fqn": "@winglang/sdk.std.AutoIdResource",
              "version": "0.0.0"
            },
            "display": {
              "hidden": true
            }
          }
        },
        "constructInfo": {
          "fqn": "@winglang/sdk.std.Resource",
          "version": "0.0.0"
        },
        "display": {}
      },
      "ParameterRegistrar": {
        "id": "ParameterRegistrar",
        "path": "root/ParameterRegistrar",
        "constructInfo": {
          "fqn": "@winglang/sdk.platform.ParameterRegistrar",
          "version": "0.0.0"
        },
        "display": {
          "hidden": true
        }
      }
    },
    "constructInfo": {
      "fqn": "@winglang/sdk.core.App",
      "version": "0.0.0"
    },
    "display": {}
  }
}