Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12127
Closed
Open
Issue created Apr 13, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][DART] Fields named json get shadowed by ToJson and become unserializable due to cyclic pointer reference

Created by: 0xNF

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

Here's a repo for the issue: https://github.com/0xNF/openapi_gen_dart_json_shadow

The Dart SDK generator will incorrectly shadow field variables named json in the generated toJson() method, resulting in a cyclic pointer loop when running the toJson() method.

openapi-generator version

jar name: openapi-generator-cli-6.0.0-20220412.074015-127.jar jar version: 6.0.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: "1.0"
  title: Dart Shadow Json Demo
servers:
  - url: 'localhost'
    variables:
      host:
        default: localhost
components:
  schemas:
    ItemWithFieldNamedJson:
      type: object
      properties:
        json:
          type: object
          additionalProperties: {}
paths:
  /:
    get:
      operationId: shadow
      responses:
        '200':
          description: produces a shadowed json field when generated in dart
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ItemWithFieldNamedJson'
Generation Details

See: https://github.com/0xNF/openapi_gen_dart_json_shadow/blob/master/shadowed/lib/model/item_with_field_named_json.dart

class ItemWithFieldNamedJson {
  ItemWithFieldNamedJson({
    this.json = const {},
  });

  Map<String, Object> json;

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};
      json[r'json'] = json;
    return json;
  }
Steps to reproduce
  1. Run the generator command
  • java -jar openapi-generator-cli-6.0.0-20220412.074015-127.jar generate -i .\shadowspec.yaml -g dart -o shadowed
  1. Examine the shadowed/lib/model/item_with_field_named_json.dart
  2. See the shadowed variable on line 34, and the infinite reference on line 35
Related issues/PRs

Use another parameter name to stop variable shadowing. related to https://github.com/OpenAPITools/openapi-generator/pull/10263

Suggest a fix

The hard fix:

  • For any variables that are generated by the Generator, use static analysis to determine if there are any conflicting variable names in the namespace in the generated functions, and then modify the desired generated variable name appropriately, repeating as many times as necessary to ensure truly unique variable names within the functions scope.

The easier fix:

  • The Dart generator already knows about reserved keywords. For fields that are named int, generated fields are named int_ with an underscore. Extend this idea to json. This is a half measure though, as if I name my field variable json_, then we're back to being shadowed.
Assignee
Assign to
Time tracking