GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Compiler error when mapping an object named "Override" · Issue #3905 · mapstruct/mapstruct

Compiler error when mapping an object named "Override" #3905

New issue
Copy link
New issue
Copy link
Closed
Bug
#3922
Closed
Compiler error when mapping an object named "Override"#3905
Bug
#3922
Copy link
Milestone
1.7.0.Beta1

Description

@thunderhook
thunderhook
opened on Jul 26, 2025
Issue body actions

The following mapper:

import org.mapstruct.Mapper;

class Override {}

class OverrideDto {}

class Car {
    public Override override;
}
class CarDto {
    public OverrideDto override;
}

@Mapper
interface Issue3904Mapper {
    CarDto map(Car car);
    OverrideDto map(Override override);
}

generates:

import javax.annotation.processing.Generated;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2025-07-26T10:41:47+0200",
    comments = "version: 1.6.0, compiler: javac, environment: Java 21.0.1 (Oracle Corporation)"
)
class Issue3904MapperImpl implements Issue3904Mapper {

    @Override
    public CarDto map(Car car) {
        if ( car == null ) {
            return null;
        }

        CarDto carDto = new CarDto();

        carDto.override = map( car.override );

        return carDto;
    }

    @Override
    public OverrideDto map(Override override) { // Incompatible types
        if ( override == null ) {
            return null;
        }

        OverrideDto overrideDto = new OverrideDto();

        return overrideDto;
    }
}

Override needs to get a fully qualified name, which it currently does not.
Interesting note: I also tried this with Generated (which could also clash) but this got a qully qualified name.

Another option would be to write @java.lang.Override.

Discussed in #3904

Originally posted by trico-tillster July 23, 2025
I've 1 object to be mapped to another, if one of them contains a object called Override compiler fails.
This happens because generated code adds the @Override annotation to the implementation method, and compiler gets confused with the import class.
I've tried to search for a config to manipulate the generated annotation, something like updating it to @java.lang.Override, but I was not able.
I'm also not able to change the model Override class name to other, because this is a generated code from an external json schema.
Thought it would be helpful to mention this here.
Any clues how to solve this?

Reacted by Luís Tiago Barreto Rico

Activity

  1. thunderhook
    added the
    Bug
    issue type on Jul 26, 2025
  2. thunderhook commented on Jul 26, 2025

    @thunderhook
    thunderhook
    on Jul 26, 2025
    ContributorAuthor
    More actions

    I just had a quick look. Even when we change it to a fully qualified name, there is an error:

        @Override // <-- Annotation type expected
        public OverrideDto map(example.package.Override override) {
            if ( override == null ) {
                return null;
            }
    
            OverrideDto overrideDto = new OverrideDto();
    
            return overrideDto;
        }

    Even if it is not imported via import it shows Annotation type expected.

    So to fix this, we would need to declare @java.lang.Override instead of @Override. Adding this by default might cause some issues with some code quality tools, but I'm not 100% sure.

  3. filiphr
    added this to the 1.7.0 milestone on Jul 27, 2025
  4. filiphr commented on Jul 27, 2025

    @filiphr
    filiphr
    on Jul 27, 2025
    Member
    More actions

    I am not sure that I am following @thunderhook. Can you provide the entire class?

    If the example.package.Override is not in the import statements, then the code you shared should compile. I do not see a reason why it wouldn't compile?

  5. thunderhook commented on Jul 28, 2025

    @thunderhook
    thunderhook
    on Jul 28, 2025
    ContributorAuthor
    More actions

    I'm not sure myself why Java behaves like that. I stumbled accross this while trying to fix it in a unit test. Just copy those classes to one of your projects (with the given packages) and have a look:

    package org.example._3904.inner;
    
    public class Override {
        public String name;
    }
    package org.example._3904.inner;
    
    public class OverrideDto {
        public String name;
    }
    package org.example._3904;
    
    import org.example._3904.inner.Override;
    import org.example._3904.inner.OverrideDto;
    import org.mapstruct.Mapper;
    
    @Mapper
    interface Issue3904Mapper {
        OverrideDto map(Override override);
    }
    package org.example._3904;
    
    import javax.annotation.processing.Generated;
    import org.example._3904.inner.Override;
    import org.example._3904.inner.OverrideDto;
    
    @Generated(
        value = "org.mapstruct.ap.MappingProcessor",
        date = "2025-07-28T07:37:57+0200",
        comments = "version: 1.6.3, compiler: javac, environment: Java 21.0.1 (Oracle Corporation)"
    )
    class Issue3904MapperImpl implements Issue3904Mapper {
    
        @Override // <- Annotation type expected
        public OverrideDto map(Override override) {
            if ( override == null ) {
                return null;
            }
    
            OverrideDto overrideDto = new OverrideDto();
    
            overrideDto.name = override.name;
    
            return overrideDto;
        }
    }

    In the generated class you can play around with the imports and fully qualified names. The only way I found was to give the Override a FQN. Maybe you have a better idea.

  6. filiphr commented on Jul 31, 2025

    @filiphr
    filiphr
    on Jul 31, 2025
    Member
    More actions

    The reason why you need an FQN in your example is because import org.example._3904.inner.Override is imported. If instead if importing that, we ise an FQQ in the map method then it works properly.

    e.g.

    public OverrideDto map(org.example._3904.inner.Override override) {
    Reacted by Luís Tiago Barreto Rico
  7. filiphr
    added 2 commits that reference this issue on Aug 29, 2025

    mapstruct#3905: Using custom class Override should compile

    22e9be0

    #3905: Using custom class Override should compile

    4e1720c
  8. filiphr
    closed this as completedin #3922on Aug 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Bug

    Projects

    No projects

      Milestone

      • 1.7.0.Beta1

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.