Skip to content

Commit

Permalink
fix swagger serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaofeiCao committed Sep 20, 2024
1 parent 4e7cab1 commit e8454cf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.typespec.http.client.generator.core.mapper;

import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.EnumType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

Expand Down Expand Up @@ -47,6 +48,6 @@ public IType map(ChoiceSchema enumType) {
}

private IType createChoiceType(ChoiceSchema enumType) {
return MapperUtils.createEnumType(enumType, true, true);
return MapperUtils.createEnumType(enumType, true, true, "getValue", null, ClassType.STRING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class MapperUtils {
* @return enum client type
*/
public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember) {
return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null);
return createEnumType(enumType, expandable, useCodeModelNameForEnumMember, null, null, null);
}

/**
Expand All @@ -48,9 +48,10 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo
* @param useCodeModelNameForEnumMember whether to use code model enum member name for client enum member name
* @param serializationMethodName method name for serialization
* @param deserializationMethodName method name for deserialization
* @param wireType wire type for serialization, if null, will default to element type of the enum
* @return enum client type
*/
public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName) {
public static IType createEnumType(ChoiceSchema enumType, boolean expandable, boolean useCodeModelNameForEnumMember, String serializationMethodName, String deserializationMethodName, IType wireType) {
JavaSettings settings = JavaSettings.getInstance();
String enumTypeName = enumType.getLanguage().getJava().getName();

Expand Down Expand Up @@ -109,6 +110,7 @@ public static IType createEnumType(ChoiceSchema enumType, boolean expandable, bo
.crossLanguageDefinitionId(enumType.getCrossLanguageDefinitionId())
.fromMethodName(deserializationMethodName)
.toMethodName(serializationMethodName)
.wireType(wireType)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@ public class EnumType implements IType {
private String crossLanguageDefinitionId;
private final String fromMethodName;
private final String toMethodName;
private final IType wireType;

/**
* Create a new Enum with the provided properties.
*
* @param name The name of the new Enum.
* @param description The description of the Enum.
* @param expandable Whether this will be an ExpandableStringEnum type.
* @param values The values of the Enum.
* @param fromMethodName The method name used to convert JSON to the enum type.
* @param toMethodName The method name used to convert the enum type to JSON.
* @param name The name of the new Enum.
* @param description The description of the Enum.
* @param expandable Whether this will be an ExpandableStringEnum type.
* @param values The values of the Enum.
* @param fromMethodName The method name used to convert JSON to the enum type.
* @param toMethodName The method name used to convert the enum type to JSON.
* @param wireType The actual wire type in JSON form.
*/
private EnumType(String packageKeyword, String name, String description,
boolean expandable, List<ClientEnumValue> values,
IType elementType,
ImplementationDetails implementationDetails,
String crossLanguageDefinitionId, String fromMethodName, String toMethodName) {
String crossLanguageDefinitionId, String fromMethodName, String toMethodName, IType wireType) {
this.name = name;
this.packageName = packageKeyword;
this.description = description;
Expand All @@ -64,6 +66,7 @@ private EnumType(String packageKeyword, String name, String description,
this.crossLanguageDefinitionId = crossLanguageDefinitionId;
this.fromMethodName = fromMethodName;
this.toMethodName = toMethodName;
this.wireType = wireType;
}

public String getCrossLanguageDefinitionId() {
Expand Down Expand Up @@ -202,7 +205,7 @@ public String jsonSerializationMethodCall(String jsonWriterName, String fieldNam
? valueGetter + "." + getToMethodName() + "()"
: valueGetter + " == null ? null : " + valueGetter + "." + getToMethodName() + "()";

return elementType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter,
return wireType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter,
jsonMergePatch);
}

Expand Down Expand Up @@ -245,6 +248,7 @@ public static class Builder {
private String crossLanguageDefinitionId;
private String fromMethodName;
private String toMethodName;
private IType wireType;

/**
* Sets the name of the Enum.
Expand Down Expand Up @@ -333,10 +337,19 @@ public Builder toMethodName(String toMethodName) {
return this;
}

public Builder wireType(IType wireType) {
this.wireType = wireType;
return this;
}

/**
* @return an immutable EnumType instance with the configurations on this builder.
*/
public EnumType build() {
IType wireType = this.wireType;
if (wireType == null) {
wireType = elementType;
}
return new EnumType(
packageName,
name,
Expand All @@ -346,7 +359,9 @@ public EnumType build() {
elementType,
implementationDetails,
crossLanguageDefinitionId,
fromMethodName, toMethodName);
fromMethodName,
toMethodName,
wireType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.ChoiceSchema;
import com.microsoft.typespec.http.client.generator.core.mapper.ChoiceMapper;
import com.microsoft.typespec.http.client.generator.core.mapper.MapperUtils;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

public class FluentChoiceMapper extends ChoiceMapper {
Expand All @@ -18,6 +19,6 @@ public static FluentChoiceMapper getInstance() {

@Override
public IType map(ChoiceSchema enumType) {
return MapperUtils.createEnumType(enumType, true, false, "getValue", null);
return MapperUtils.createEnumType(enumType, true, false, "getValue", null, ClassType.STRING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public IType map(ChoiceSchema enumType) {
if (isStringEnum) {
return MapperUtils.createEnumType(enumType, true, true);
} else {
return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue");
return MapperUtils.createEnumType(enumType, true, true, "getValue", "fromValue", null);
}
}
}

0 comments on commit e8454cf

Please sign in to comment.