Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): nullable array types in 3.1.x specs #19687

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

joscha
Copy link
Contributor

@joscha joscha commented Sep 26, 2024

Currently, in 3.1 specs, the follwing:

      properties:
        arrayDataOrNull:
          items:
            $ref: "#/components/schemas/SomeObject"
          type:
            - array
            - 'null'

yields:

'arrayDataOrNull': Array<SomeObject>

where it should yield:

'arrayDataOrNull': Array<SomeObject> | null;

It seems either nullability is not determined correctly in core and/or the typescript generator doesn't interpret it correctly.

Real-life example: planet-a-ventures/affinity-node#49
Discussion: https://openapi-generator.slack.com/archives/CLSB0U0R5/p1727342905017529

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language I think this is a core issue: @wing328 (2015/07) ❤️
    @jimschubert (2016/05) ❤️
    @cbornet (2016/05)
    @jmini (2018/04) ❤️
    @etherealjoy (2019/06)

- stringDataOrNull
- oneofOrNull
properties:
arrayDataOrNull:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the broken part

import { HttpFile } from '../http/http';

export class WithNullableType {
'arrayDataOrNull': Array<SomeObject>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

Suggested change
'arrayDataOrNull': Array<SomeObject>;
'arrayDataOrNull': Array<SomeObject> | null;

import { HttpFile } from '../http/http';

export class WithNullableType {
'arrayDataOrNull': Array<SomeObject> | null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the fix - it now has the | null

@@ -0,0 +1 @@
builds/**/* linguist-generated=true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sure that all the generated code collapses in the github diff.

@@ -1268,6 +1268,7 @@ private Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchemas)
as.setMaxItems(schema.getMaxItems());
as.setExtensions(schema.getExtensions());
as.setXml(schema.getXml());
as.setNullable(schema.getNullable());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further up, we fix up the nullable flag when the types contain the new 3.1 null type:

if (schema.getTypes().contains("null")) {
schema.setNullable(true);
schema.getTypes().remove("null");
}

but then, if there's a single type left AND it is an array schema, we forget to copy this property to the cloned ArraySchema. This line makes sure that the nullable flag is transferred onto the schema clone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is likely to happen again in the future when/if a new property is added that is in 3.1+ but not 3.0.x - we could possibly introduce a clone or transfer function somewhere which would make this a bit more obvious, but it's not a guarantee either.

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@joscha joscha changed the title fix: nullable array types in 3.1.x fix: nullable array types in 3.1.x specs Sep 26, 2024
@joscha joscha changed the title fix: nullable array types in 3.1.x specs fix(core): nullable array types in 3.1.x specs Sep 26, 2024
…penapi-generator into joscha/nullable-array-types-3_1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants