swagger-api / swagger-api/swagger-codegen

[typescript-node] Not generating optional model members correctly.

Open
#6,309 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

typescript-node is not producing models with optional fields where they ought to be. All fields are created as required.

Swagger-codegen version

2.2.3

Swagger declaration file content or url
  Project:
    type: object
    properties:
      created:
        $ref: "#/definitions/UserTouchTime"
      id:
        type: string
        readOnly: true
      lastModified:
        $ref: "#/definitions/UserTouchTime"
      name:
        type: string
    required:
      - name

This is the model that was generated:

export class Project {
    'created': UserTouchTime;
    'id': string;
    'lastModified': UserTouchTime;
    'name': string;
}

However, everything except name should be optional (have a ? after the property name).

Command line used for generation

java -jar swagger-codegen-cli-2.2.3.jar generate -l typescript-node -i c:/Users/abrick/src/vt.meta-swagger/swagger.yaml -o foo

Steps to reproduce

Run the above command.

Related issues/PRs

Can't find one.

Suggest a fix/enhancement

I don't see where in the code this would be set, but I notice that in the Mustache template, it just uses the name property. It probably also needs to have a required property that determines if a ? should be appended.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the typescript-node Mustache model template and reproduce the issue using the Swagger declaration and generation command in the report. Update the generated model so only fields listed under required lack a question mark, then compare the output with the expected Project example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.