Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated API wrapper returns undefined instead Date if the timestamp is 0 #3043

Open
davidyuk opened this issue Feb 2, 2025 · 1 comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@davidyuk
Copy link

davidyuk commented Feb 2, 2025

In OpenApi I have a type defined as

"value": {
 "type": "integer",
  "format": "unixtime"
}

In the generated api client it has a non-optional Date type. But in the application, I get undefined if the processed timestamp is 0.

I expect it to be new Date(0) or the generated type to be Date | undefined (not preferable).

Reproduction

OpenAPI schema
{
  "openapi": "3.0.0",
  "info": {
    "title": "Node",
    "version": "1.0.0"
  },
  "paths": {
    "/foo/{value}": {
      "get": {
        "operationId": "foo",
        "parameters": [
          {
            "in": "path",
            "name": "value",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "value": {
                      "type": "integer",
                      "format": "unixtime"
                    }
                  },
                  "required": ["value"]
                }
              }
            }
          }
        }
      }
    }
  }
}

autorest.yaml

version: ^3.7.1
use-extension:
  '@autorest/typescript': ^6.0.23
  '@autorest/modelerfour': ^4.27.0
input-file: ./api.json
output-folder: ./reproduction-api
source-code-folder-path: .
generator: typescript
generate-metadata: false
add-credentials: false

npm dependencies

  "dependencies": {
    "@azure/core-client": "^1.9.2",
    "@types/express": "^5.0.0",
    "autorest": "^3.7.1",
    "express": "^4.21.2",
    "tsx": "^4.19.2"
  }

reproduction.ts

import express, { Request, Response } from "express";

const app = express();

app.get("/foo/:value", (req: Request, res: Response) => {
  res.json({ value: +req.params.value });
});

app.listen(3000, () => {
  console.log("listening on port 3000");
});

import { Node } from "./reproduction-api/index";

const node = new Node("http://localhost:3000", {
  allowInsecureConnection: true,
});

(async () => {
  console.log("value", (await node.foo(42)).value);
  console.log("value", (await node.foo(0)).value);
})();
$ npx autorest ./autorest.yaml
$ npx tsx reproduction        
listening on port 3000
value 1970-01-01T00:00:42.000Z
value undefined

I expect it to print 1970-01-01T00:00:00.000Z instead undefined.

@microsoft-github-policy-service microsoft-github-policy-service bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 2, 2025
@xirzec
Copy link
Member

xirzec commented Feb 3, 2025

Looks like this is happening because of this piece of code: https://github.com/Azure/azure-sdk-for-js/blob/e2a07dd899034ed223d085bae81e3aa49c0a998c/sdk/core/core-client/src/serializer.ts#L401

I'm not sure if any other services rely on the current 0 => undefined behavior, but really this should have been a check to see if the value was a number rather than using !n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

2 participants