Skip to content

Bug: undefined not represented in union type within array type in C++ #1202

@chrispader

Description

@chrispader

What happened?

Problem

When using undefined within a union type in an array, the undefined is not represented with std::optional:

The spec below creates the following following C++ header:

namespace margelo::nitro::nitroreproducer {

  using namespace margelo::nitro;

  class HybridDatabaseSpec: public virtual HybridObject {
    public:
      ...

    public:
      // Methods
      virtual void write(const std::optional<std::vector<std::variant<nitro::NullType, bool, std::shared_ptr<ArrayBuffer>, std::string, double>>>& rows) = 0;

    ...
  };

} // namespace margelo::nitro::nitroreproducer

Whereas you would expect the method signature to include std::optional since we have undefined in the union type from the TypeScript spec above:

virtual void write(const std::optional<std::vector<std::optional<std::variant<nitro::NullType, bool, std::shared_ptr<ArrayBuffer>, std::string, double>>>>& rows) = 0;

Repro PR: #1201

Nitrogen CLI logs (with --logLevel="debug")

🔧  Loading nitro.json config...
🚀  Nitrogen 0.33.5 runs at ~/Projects/Margelo/nitro-reproducer
    🔍  Nitrogen found 1 spec in ./src/specs
  Parsing Example.nitro.ts...
    ⚙️   Generating specs for HybridObject "Database"...
        shared: Generating C++ code...
          C++: Creating HybridDatabaseSpec.hpp...
          C++: Creating HybridDatabaseSpec.cpp...
⛓️   Setting up build configs for autolinking...
    Creating autolinking build setup for ios...
          Ruby: Creating NitroReproducer+autolinking.rb...
          C++: Creating NitroReproducer-Swift-Cxx-Bridge.hpp...
          C++: Creating NitroReproducer-Swift-Cxx-Bridge.cpp...
          C++: Creating NitroReproducer-Swift-Cxx-Umbrella.hpp...
    Creating autolinking build setup for android...
          Cmake: Creating NitroReproducer+autolinking.cmake...
          Gradle: Creating NitroReproducer+autolinking.gradle...
          C++: Creating NitroReproducerOnLoad.hpp...
          C++: Creating NitroReproducerOnLoad.cpp...
          Kotlin: Creating NitroReproducerOnLoad.kt...
🎉  Generated 1/1 HybridObject in 0.5s!
💡  Your code is in ./nitrogen/generated

Nitro Specs (.nitro.ts)

import type { HybridObject } from 'react-native-nitro-modules'

export type Column = boolean | number | string | ArrayBuffer | null | undefined

export type Rows = Column[]

export interface Database extends HybridObject<{ ios: 'c++'; android: 'c++' }> {
  write(rows?: Rows): void
}

Nitro Modules Version

0.33.5

Nitrogen Version

0.33.5

Operating system

MacOS

Additional information

Metadata

Metadata

Assignees

No one assigned

    Labels

    nitrogenIssue is related to the code-generator "Nitrogen"

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions