hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.39k stars 107 forks source link

Default values in the generated file make parameters optional by default #1219

Open adrienrobertsenvo opened 2 weeks ago

adrienrobertsenvo commented 2 weeks ago

Description

Is there any way to not have parameters become optional whenever they are defined with a default option? the same as here: https://github.com/openapi-ts/openapi-typescript/issues/1944

It would be great to have a configuration option like: defaultsRequired: boolean

mrlubos commented 2 weeks ago

Hi @adrienrobertsenvo, did you mean to post this issue here? Asking since the linked issue isn't for this repository

adrienrobertsenvo commented 2 weeks ago

Yes I did! Sorry for the confusion. Both tools are relatively similar and this feature is present there. I was wondering if that is a possibility here. But let me explain the example here again:

Here HuntingSkill has a default value so might not be required.


  /huntingSkill:
    post:
      operationId: createHuntingSkill
      summary: Create a hunting skill
      description: Submit a new hunting skill level
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/HuntingSkill'
      responses:
        '201':
          description: Successfully created hunting skill
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HuntingSkill'
        '400':
          description: Bad request
      security:
        - apiKeyAuth: []
components:
  securitySchemes:
    apiKeyAuth:
      type: apiKey
      in: header
      name: X-API-Key
  schemas:
    HuntingSkill:
      type: object
      properties:
        huntingSkill:
          type: string
          description: The measured skill for hunting
          default: lazy
          enum:
            - clueless
            - lazy
            - adventurous
            - aggressive
      required: []```

And openapi-ts creates:
`HuntingSkill: {
           huntingSkill?: IntApiHuntingSkillHuntingSkillEnum;
        };`

That is not required. 
This might not be a behavior that i want, I'd rather have it as required, even if it has a default value 
mrlubos commented 2 weeks ago

Got it! I sort of remember there being a discussion around this, I'll add a vote label on this issue nonetheless. My question is, why do you need a different behaviour if the server handles missing values?

adrienrobertsenvo commented 2 weeks ago
image

Here is probably a better example above.

I'm basing my typescript types on what my API will send me (the whole point of openapi right :p) and for this example: DataExport status should never be null but they do have a default value of "in-progress". It doesn't mean that it's not required in my type, DataExport should always have a value.

Outputed type looks like this:


export type DataExport = {
  tenant_id: number;
  export_type: DataExportType;
 ...
  id: number;
  created_at: string;
  available: boolean;
  blobname?: string | null;
  status?: DataExportStatus;
}; ```
mrlubos commented 2 weeks ago

@adrienrobertsenvo Aha! So this is about the response shape you'd receive, correct? And I'm assuming the reason the API uses default is because they reuse the same shape for the request?