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

typescript-fetch: Move parenthesis to correct place #18075

Merged
merged 1 commit into from
Mar 21, 2024

Conversation

shirin175
Copy link
Contributor

A misplaced parenthesis let to a syntax error when generating the code for a nullable set in the FromJSONTyped function. By moving the parenthesis to the correct place the syntax error was resolved in my case, I did not test for all combinations though.

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.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
    Pinging @kota65535 @wing328 because the change was introduced by [typescript-fetch] Use == to check property is null or undefined #17972

A misplaced parenthesis let to a syntax error when generating the code for a nullable set in the FromJSONTyped function. By moving the parenthesis to the correct place the syntax error was resolved.
@kota65535
Copy link
Contributor

@shirin175 Oh, sorry for inconvinience.
Could you provide an example of the field that reproduces the syntax error?

@shirin175
Copy link
Contributor Author

shirin175 commented Mar 12, 2024

Of course :)
In our Java Code we have the following Dto:

public record Identity(
    @NotBlank String username,
    @Nullable String name,
    @Nullable Instant expireAt,
    @Nullable String userMailAddress,
    Set<@NotNull RoleEnum> roles
) {}

which is translates to the following Code in Typescript:

export function IdentityFromJSONTyped(json: any, ignoreDiscriminator: boolean): Identity {
    if (json == null) {
        return json;
    }
    return {
        
        'username': json['username'],
        'name': json['name'] == null ? undefined : json['name'],
        'expireAt': json['expireAt'] == null ? undefined : (new Date(json['expireAt'])),
        'userMailAddress': json['userMailAddress'] == null ? undefined : json['userMailAddress'],
        'roles': json['roles'] == null ? undefined : new Set((json['roles'] as Array<any>).map(RoleEnumFromJSON))),
    };
}

In the IDE the error becomes clearer
image
The following is the generated OpenApi YAML:

    Identity:
      required:
      - username
      type: object
      properties:
        username:
          pattern: \S
          type: string
        name:
          type: string
        expireAt:
          type: string
          allOf:
          - $ref: '#/components/schemas/Instant'
        userMailAddress:
          type: string
        roles:
          uniqueItems: true
          type: array
          items:
            $ref: '#/components/schemas/RoleEnum'

If we take a Dto which has a non-nullable Set the code is generated without syntax errors:

@Builder(toBuilder = true)
public record RDProSgZuordnungDto(
    @NotNull @ValidGueltigAb Instant gueltigVon,
    @ValidGueltigBis Instant gueltigBis,
    String document,
    @NotNull @Valid @IdAttribute CodeCodierung codeCodierung,
    @NotNull Set<@NotNull @Valid CodeCodierung> srChildren
) implements CodeCodierungDto, TimesliceRecord {
    @JsonIgnore
    @Override
    public CodeCodierung getBusinessKey() {
        return codeCodierung();
    }
}

Which translates to:

export function RDProSgZuordnungDtoFromJSONTyped(json: any, ignoreDiscriminator: boolean): RDProSgZuordnungDto {
    if (json == null) {
        return json;
    }
    return {
        
        'gueltigVon': (new Date(json['gueltigVon'])),
        'gueltigBis': json['gueltigBis'] == null ? undefined : (new Date(json['gueltigBis'])),
        'document': json['document'] == null ? undefined : json['document'],
        'codeCodierung': CodeCodierungFromJSON(json['codeCodierung']),
        'srChildren': (new Set((json['srChildren'] as Array<any>).map(CodeCodierungFromJSON))),
    };
}

image

We have this problem since 7.4.0 has been released.

madpah added a commit to sonatype-nexus-community/nexus-iq-api-client that referenced this pull request Mar 12, 2024
@kota65535
Copy link
Contributor

I've confirmed that all combinations of required and nullable produce the correct code.
Thank you for fixing it!

@shirin175
Copy link
Contributor Author

Thank you for making sure all combinations work!

@wing328 wing328 merged commit 1bfe800 into OpenAPITools:master Mar 21, 2024
14 checks passed
@wing328 wing328 added this to the 7.5.0 milestone Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants