Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
support oneof
  • Loading branch information
DanielLiu1123 committed Nov 10, 2025
commit a6ad574d5e07f997ec0d1a24f662307eb379d702
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public class Everything {
private DayOfWeek dayOfWeek;
private Month month;

// oneof
private int oneofInt32;
private String oneofString;
private Integer oneofEnum;
private Message oneofMessage;

public Integer getInt32() {
return this.int32;
}
Expand Down Expand Up @@ -390,6 +396,38 @@ public void setMonth(Month month) {
this.month = month;
}

public int getOneofInt32() {
return oneofInt32;
}

public void setOneofInt32(int oneofInt32) {
this.oneofInt32 = oneofInt32;
}

public String getOneofString() {
return oneofString;
}

public void setOneofString(String oneofString) {
this.oneofString = oneofString;
}

public Integer getOneofEnum() {
return oneofEnum;
}

public void setOneofEnum(Integer oneofEnum) {
this.oneofEnum = oneofEnum;
}

public Message getOneofMessage() {
return oneofMessage;
}

public void setOneofMessage(Message oneofMessage) {
this.oneofMessage = oneofMessage;
}

public static class Message {
private long id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
*/
package org.mapstruct.itest.protobuf;

import static org.mapstruct.NullValueCheckStrategy.ALWAYS;
import static org.mapstruct.ReportingPolicy.ERROR;

import org.mapstruct.itest.protobuf.EverythingModel;
import org.mapstruct.itest.protobuf.Everything;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import org.mapstruct.itest.protobuf.Everything;
import org.mapstruct.itest.protobuf.EverythingModel;

import static org.mapstruct.NullValueCheckStrategy.ALWAYS;
import static org.mapstruct.ReportingPolicy.ERROR;

@Mapper(nullValueCheckStrategy = ALWAYS, unmappedTargetPolicy = ERROR)
@Mapper(nullValueCheckStrategy = ALWAYS, unmappedTargetPolicy = ERROR, unmappedSourcePolicy = ERROR)
public interface EverythingMapper {

EverythingMapper INSTANCE = Mappers.getMapper(EverythingMapper.class);
EverythingMapper INSTANCE = Mappers.getMapper( EverythingMapper.class );

@Mapping(target = "float_", source = "float")
@Mapping(target = "double_", source = "double")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ message EverythingModel {
google.type.DayOfWeek day_of_week = 73;
google.type.Month month = 74;

// oneof field
oneof oneof {
int32 oneof_int32 = 80;
string oneof_string = 81;
Enum oneof_enum = 82;
Message oneof_message = 83;
}

message Message {
int64 id = 1;
string name = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.mapstruct.ap.spi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -188,7 +189,7 @@ private Map<String, Set<MethodSignature>> getInternalMethods() {
MESSAGE_LITE_BUILDER,
};

Map<String, Set<MethodSignature>> methodsMap = new java.util.HashMap<>();
Map<String, Set<MethodSignature>> methodsMap = new HashMap<>();
for ( String className : internalClasses ) {
TypeElement typeElement = elementUtils.getTypeElement( className );
if ( typeElement != null ) {
Expand Down Expand Up @@ -333,6 +334,14 @@ && isMapType( m.getReturnType() )
}
) );

// oneof field generates extra 'getXxxCase()'
rules.add( new SpecialMethodRule(
method -> getMethodName( method ).endsWith( "Case" )
&& isEnumType( method.getReturnType() )
&& !isProtobufEnumType( method.getReturnType() ),
(method, methods) -> true
) );

return rules;
}

Expand Down Expand Up @@ -485,6 +494,14 @@ private boolean isProtobufEnumType(TypeMirror t) {
return typeUtils.isSubtype( t, protobufEnumType );
}

private boolean isEnumType(TypeMirror t) {
if ( t.getKind() != TypeKind.DECLARED ) {
return false;
}
Element element = ( (DeclaredType) t ).asElement();
return element.getKind() == ElementKind.ENUM;
}

private boolean isGetList(ExecutableElement element) {
// repeated fields getter: getXxxList()
return hasPrefixWithUpperCaseNext( element, "get" )
Expand Down