Skip to content

Fix SWIG syntax error on maps#589

Open
emiliocuestaf wants to merge 1 commit into
masterfrom
basic/hotfix/swig-syntax-error-on-maps
Open

Fix SWIG syntax error on maps#589
emiliocuestaf wants to merge 1 commit into
masterfrom
basic/hotfix/swig-syntax-error-on-maps

Conversation

@emiliocuestaf

@emiliocuestaf emiliocuestaf commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Description

Description

When generating Python (SWIG) bindings for an IDL struct member of type map<K, V>, fastddsgen builds the %template(...) instantiation name by concatenating the key type's fully-qualified C++ typename with the value type's unqualified name. If the key type's C++ name contains a scope-resolution operator (::) — which string always does, since it maps to std::string — the generated identifier is invalid SWIG syntax and SWIG fails to parse the file.

Root cause

src/main/java/com/eprosima/fastcdr/idl/templates/TypesSwigInterface.stg,
rule member_getters, map branch:

$elseif(member.typecode.isMapType)$
%ignore $struct_name$::$member.name$() const;
%template($member.typecode.keyTypeCode.cppTypename$_$member.typecode.valueTypeCode.noScopedCppTypename$_map) std::map<...>;

The value type correctly calls .noScopedCppTypename (namespace
stripped). The key type uses plain .cppTypename (fully qualified) —
that's the bug. Every other collection/composite branch in this same file
(sequences, arrays, optionals, fixed strings, the RPC template family)
consistently uses a sanitized name (noScopedCppTypename or
formatedCppTypename); this is the only branch that doesn't.

Reproduction

Given an IDL like:

struct SomeipServiceEntry {
  SomeipServiceAvailability availability;
  uint32 last_changed;
};

typedef map<string, SomeipServiceEntry> SomeipServiceEntryByName;

struct SomeipServiceStatus {
  SomeipServiceEntryByName services;
};

fastddsgen -python generates, in the corresponding .i file:

%template(std::string_SomeipServiceEntry_map) std::map<std::string,SomeipServiceEntry>;

Compiling this with SWIG fails:

SomeipServiceStatus.i:185: Error: Syntax error in input(1).

SWIG parses :: as C++ scope resolution, so std::string_SomeipServiceEntry_map
is not a valid template-instantiation identifier.

Fix

Using

%template($member.typecode.keyTypeCode.noScopedCppTypename$_$member.typecode.valueTypeCode.noScopedCppTypename$_map) std::map<...>;

instead of

%template($member.typecode.keyTypeCode.cppTypename$_$member.typecode.valueTypeCode.noScopedCppTypename$_map) std::map<...>;

produces a clean identifier:

%template(string_SomeipServiceEntry_map) std::map<std::string,SomeipServiceEntry>;

which SWIG parses without error.

Why this only affects map, not sequence/array

Sequences and arrays only ever need a name for their single content type, and both branches already use noScopedCppTypename for it. map is the only collection type with a second, independent type parameter (the key), and it was the one spot where the unqualified variant wasn't used. Any map whose key type has a qualified C++ name — string (→ std::string), or an enum key — triggers this; maps keyed by a plain scalar (int32, etc.) don't hit it, since those types have no :: in their C++ name to begin with.

Contributor Checklist

  • Commit messages follow the project guidelines.
  • Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been added; the added tests pass locally
  • New feature has been documented/Current behavior is correctly described in the documentation.
  • Applicable backports have been included in the description.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • If this is a bug fix that needs to be backported to Fast DDS Gen basic, the corresponding label has been added.
  • Check CI results: changes do not issue any warning.
  • Check CI results: failing tests are unrelated with the changes.

Contributor Checklist

  • Commit messages follow the project guidelines.
  • Tests that thoroughly check the new feature have been added/Regression tests checking the bug and its fix have been added; the added tests pass locally
  • New feature has been documented/Current behavior is correctly described in the documentation.
  • Applicable backports have been included in the description.

Reviewer Checklist

  • The PR has a milestone assigned.
  • The title and description correctly express the PR's purpose.
  • Check contributor checklist is correct.
  • Check CI results: changes do not issue any warning.
  • Check CI results: failing tests are unrelated with the changes.

Signed-off-by: Emilio Cuesta <emiliocuesta@eprosima.com>
@emiliocuestaf emiliocuestaf added this to the v4.3.1 milestone Jul 10, 2026
@github-actions github-actions Bot added the ci-pending PR which CI is running label Jul 10, 2026

@richiware richiware left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, add a regression test in dds-types-test repository

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-pending PR which CI is running

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants