OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generated API has no optional fields

Open
#14,042 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

Using the rest_framework_jwt module (i.e.) you get a serializer like this:
Screenshot 2022-11-16 at 18 27 54

This get's generated in codegen to this:
Screenshot 2022-11-16 at 18 28 40

The problem here is that token is still required on POST request, even though it shouldn't even be part of the POST request.

openapi-generator version

We are using "@openapitools/openapi-generator-cli": "^2.4.26", with "generator-cli": { "version": "5.2.0" } specified.

Generation Details

Code gets generated for Angular/ Typescript

Steps to reproduce

Use this yaml code to generate an API with the generator cli

"JSONWebToken": {
                "type": "object",
                "description": "Serializer class used to validate a username and password.\n\n'username' is identified by the custom UserModel.USERNAME_FIELD.\n\nReturns a JSON Web Token that can be used to authenticate later calls.",
                "properties": {
                    "password": {
                        "type": "string",
                        "writeOnly": true
                    },
                    "token": {
                        "type": "string",
                        "readOnly": true
                    },
                    "email": {
                        "type": "string",
                        "writeOnly": true
                    }
                },
                "required": [
                    "email",
                    "password",
                    "token"
                ]
            },
Related issues/PRs
Suggest a fix

To be honest, I am not even quite sure if this is a bug, so I can't suggest a fix. I think one solution could be to specify token as optional using the ? in TypeScript.

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 generator CLI and the supplied JSONWebToken YAML schema, then reproduce an Angular/TypeScript client and inspect how readOnly and required are represented in the generated model and POST request. Done means token is not required in the POST input while remaining available in responses, with generated output matching the schema.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.