OpenAPITools / OpenAPITools/openapi-generator

[BUG] openapi-generator typescript-axios generated model properties optional

Open
#15,152 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ X] Have you provided a full/minimal spec to reproduce the issue?
  • [ X ] Have you validated the input using an OpenAPI validator (example)?
  • [ X ] Have you tested with the latest master to confirm the issue still exists?
  • [ X ] Have you searched for related issues/PRs?
  • [ X ] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When I generate a typescript-axios client from my APIs written in GoLang all of the models properties are optional. I would expect all the fields to be non optional unless specified.

openapi-generator version

Library: @openapitools/openapi-generator-cli: 2.5.2

OpenAPI declaration file content or url

GoLang Model

// Account model info
// @Description User account information
// @Description with user id, username, nickname, avatar, roles, guild avatar, and rank
type Account struct {
	ID          string   `json:"id"`
	Username    string   `json:"username"`
	Nick        string   `json:"nick"`
	Avatar      string   `json:"avatar"`
	Roles       []string `json:"roles"`
	GuildAvatar string   `json:"guildAvatar"`
	Rank        string   `json:"rank"`
}

Generated swag model from yaml file:

definitions:
  models.Account:
    description: User account information with user id, username, nickname, avatar,
      roles, guild avatar, and rank
    properties:
      avatar:
        type: string
      guildAvatar:
        type: string
      id:
        type: string
        x-nullable: "false"
      nick:
        type: string
      rank:
        type: string
      roles:
        items:
          type: string
        type: array
      username:
        type: string
    type: object

typescript-axios generated model from yaml file definition:

/**
 * User account information with user id, username, nickname, avatar, roles, guild avatar, and rank
 * @export
 * @interface ModelsAccount
 */
export interface ModelsAccount {
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'avatar'?: string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'guildAvatar'?: string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'id'?: string | null;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'nick'?: string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'rank'?: string;
    /**
     * 
     * @type {Array<string>}
     * @memberof ModelsAccount
     */
    'roles'?: Array<string>;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'username'?: string;
}
Generation Details

Building a typescript axios client using the command:
openapi-generator-cli generate -i specFile -g typescript-axios -o outputDir -p packageName

results in all my produced models from my GoLang swag generated definition to be optional. I would expect them to not be optional unless specified.

Steps to reproduce
  1. Build API Spec with user defined Golang struct.
  2. Build an typescript axios client via: openapi-generator-cli generate -i specFile -g typescript-axios -o outputDir -p packageName
  3. Check produced models to verify all the properties are all optioanl (? next to the property name)
Suggest a fix

I may be describing my models incorrectly, so this could just be user error. If this is a feature ask I would think that being able to set a golang tag on properties for if they are nullable or not to avoid having all option properties in the generated typescript-axios client models.

For example, you have this GoLang struct, which produces that yaml and typescript model to have all fields being non optional, but the current behavior is to have all the fields as optional. I would expect the generated model to match my last example where the fields are not optional.

// Account model info
// @Description User account information
// @Description with user id, username, nickname, avatar, roles, guild avatar, and rank
type Account struct {
	ID          string   `json:"id"`
	Username    string   `json:"username"`
	Nick        string   `json:"nick"`
	Avatar      string   `json:"avatar"`
	Roles       []string `json:"roles"`
	GuildAvatar string   `json:"guildAvatar"`
	Rank        string   `json:"rank"`
}
definitions:
  models.Account:
    description: User account information with user id, username, nickname, avatar,
      roles, guild avatar, and rank
    properties:
      avatar:
        type: string
      guildAvatar:
        type: string
      id:
        type: string
        x-nullable: "false"
      nick:
        type: string
      rank:
        type: string
      roles:
        items:
          type: string
        type: array
      username:
        type: string
    type: object

typescript-axios generated model from yaml file definition:

/**
 * User account information with user id, username, nickname, avatar, roles, guild avatar, and rank
 * @export
 * @interface ModelsAccount
 */
export interface ModelsAccount {
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'avatar'?: string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'guildAvatar'?: string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'id'?: string | null;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'nick'?: string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'rank'?: string;
    /**
     * 
     * @type {Array<string>}
     * @memberof ModelsAccount
     */
    'roles'?: Array<string>;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'username'?: string;
}

This is my expected typescript-axios generated model from yaml file definition (All the properties are not optional):

/**
 * User account information with user id, username, nickname, avatar, roles, guild avatar, and rank
 * @export
 * @interface ModelsAccount
 */
export interface ModelsAccount {
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'avatar': string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'guildAvatar': string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'id': string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'nick': string;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'rank': string;
    /**
     * 
     * @type {Array<string>}
     * @memberof ModelsAccount
     */
    'roles': Array<string>;
    /**
     * 
     * @type {string}
     * @memberof ModelsAccount
     */
    'username': string;
}

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 supplied OpenAPI definition and the typescript-axios generation command, then trace how the schema’s properties and required declarations determine optional TypeScript fields. Reproduce the output and compare it with OpenAPI required-field semantics; done means confirming whether the generator is incorrect or the specification is missing required declarations, with a focused regression case if a generator defect is confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.