OpenAPITools / OpenAPITools/openapi-generator

[BUG] Description

Open
#7,930 0 comments 0 reactions 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
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

axios-typescript generator does not honor required properties in a derived model schema.

Start with a base schema and derive some schemas from it -- use the same property names/types,
but in derived schemas, mark some properties as required. See components/schemas/base,
components/schemas/thing2, components/schemas/thing3, in the sample OpenAPI input

In the generated api.json, the generated Typescript classes for Thing2 and Thing3 merely extend Base,

/**
 * Base type.
 * @export
 * @interface Base
 */
export interface Base {
    /**
     * A
     * @type {string}
     * @memberof Base
     */
    a?: string;
    /**
     * B
     * @type {string}
     * @memberof Base
     */
    b?: string;
    /**
     * map of string -> msg
     * @type {{ [key: string]: Msg; }}
     * @memberof Base
     */
    msgs1?: { [key: string]: Msg; };
    /**
     * messages object, a map of string -> msg
     * @type {{ [key: string]: Msg; }}
     * @memberof Base
     */
    msgs2?: { [key: string]: Msg; };
}

/**
 * Thing2
 * @export
 * @interface Thing2
 */
export interface Thing2 extends Base {
}
/**
 * Thing3
 * @export
 * @interface Thing3
 */
export interface Thing3 extends Thing1 {
}

which leaves the types of the properties a and b as still optional

Those types should change the types of a and b from a?: string; b?:string
to a: string; b:string in Thing2 and Thing3 to reflect the JSON schema.
This will allow better diagnostics in client code that uses the generated types, i.e. one can use

const thing3 : Thing3 = ....;
const t3a :string = thing3.a;
const t3b :string = thing3.b;

and allow passing t3a and t3b to other code that uses the type string without
having to deal with undefined.

openapi-generator version

4.3.0-SNAPSHOT

OpenAPI declaration file content or url

openapi.yaml (Gist)

Generation Details

openapi-generator generate -g typescript-axios -o gen/__typescript-axios -i openapi.yaml

Steps to reproduce

run the command

Related issues/PRs
Suggest a fix

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 linked openapi.yaml and run the shown typescript-axios generation command. Compare the generated Thing2 and Thing3 declarations with the required properties in the derived schemas. Done means inherited required properties are emitted as non-optional TypeScript properties, while unrelated optional properties remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.