OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.82k stars 6.58k forks source link

[BUG] [Javascript] Setting a property as default doesn't work #19604

Closed Poolmann closed 1 month ago

Poolmann commented 1 month ago

Bug Report Checklist

Description
openapi-generator version

7.8.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Title
  description: Title
  version: 1.0.0
paths:
  '/user':
    get:
      responses:
        200:
          description: "success"
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RequiredProperties'

components:
  schemas:
    RequiredProperties:
      type: object
      required:
        - requiredWithDefault
        - requiredWithoutDefault
      properties:
        requiredWithDefault:
          type: string
          default: "4.5.0"
        requiredWithoutDefault:
          type: string
        notRequiredWithDefault:
          type: string
          default: "4.5.0"
        notRequiredWithoutDefault:
          type: string
Generation Details

java -jar openapi-generator-cli.jar generate -i required.yaml -g javascript -o req_test

Steps to reproduce

Generate client, run:

var Title = require("title");

var test = new Title.RequiredProperties();
console.log(test)

Output:

RequiredProperties {
  requiredWithDefault: '4.5.0',
  requiredWithoutDefault: undefined
}

I would expect properties with a default to be initialized as well (with the DEFAULT). Especially since this is the case with all the other generators that I'm using (Java, Python, C#, PHP).

wing328 commented 1 month ago

I would expect properties with a default to be initialized as well (with the DEFAULT).

thanks for reporting the issue.

May I know if you've time to contribute a PR to add the default value support in the JS client? We can work with you on that.

Poolmann commented 1 month ago

May I know if you've time to contribute a PR to add the default value support in the JS client?

Not right now, but I will probably have to look into the issue in the near future.

Poolmann commented 1 month ago

I actually got sooner to it than I thought. It would be nice if someone looked into it. The changes are minimal.