Skip to content

Commit 393ec96

Browse files
fix(java): handle enum input when args is not enum (#3585)
1 parent c589717 commit 393ec96

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

packages/plugins/java/java/src/visitor.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -117,34 +117,36 @@ public static ${enumName} valueOfLabel(String label) {
117117
.withBlock(enumBlock).string;
118118
}
119119

120-
protected resolveInputFieldType(typeNode: TypeNode): { baseType: string; typeName: string; isScalar: boolean; isArray: boolean } {
120+
protected resolveInputFieldType(typeNode: TypeNode): { baseType: string; typeName: string; isScalar: boolean; isArray: boolean; isEnum: boolean } {
121121
const innerType = getBaseTypeNode(typeNode);
122122
const schemaType = this._schema.getType(innerType.name.value);
123123
const isArray = typeNode.kind === Kind.LIST_TYPE || (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE);
124-
let result: { baseType: string; typeName: string; isScalar: boolean; isArray: boolean } = null;
124+
let result: { baseType: string; typeName: string; isScalar: boolean; isArray: boolean; isEnum: boolean } = null;
125125

126126
if (isScalarType(schemaType)) {
127127
if (this.scalars[schemaType.name]) {
128128
result = {
129129
baseType: this.scalars[schemaType.name],
130130
typeName: this.scalars[schemaType.name],
131131
isScalar: true,
132+
isEnum: false,
132133
isArray,
133134
};
134135
} else {
135-
result = { isArray, baseType: 'Object', typeName: 'Object', isScalar: true };
136+
result = { isArray, baseType: 'Object', typeName: 'Object', isScalar: true, isEnum: false };
136137
}
137138
} else if (isInputObjectType(schemaType)) {
138139
result = {
139140
baseType: `${this.convertName(schemaType.name)}Input`,
140141
typeName: `${this.convertName(schemaType.name)}Input`,
141142
isScalar: false,
143+
isEnum: false,
142144
isArray,
143145
};
144146
} else if (isEnumType(schemaType)) {
145-
result = { isArray, baseType: this.convertName(schemaType.name), typeName: this.convertName(schemaType.name), isScalar: true };
147+
result = { isArray, baseType: this.convertName(schemaType.name), typeName: this.convertName(schemaType.name), isScalar: false, isEnum: true };
146148
} else {
147-
result = { isArray, baseType: 'Object', typeName: 'Object', isScalar: true };
149+
result = { isArray, baseType: 'Object', typeName: 'Object', isScalar: true, isEnum: false };
148150
}
149151

150152
if (result) {
@@ -173,6 +175,15 @@ public static ${enumName} valueOfLabel(String label) {
173175
return indent(`this._${arg.name.value} = ((List<Map<String, Object>>) args.get("${arg.name.value}")).stream().map(${typeToUse.baseType}::new).collect(Collectors.toList());`, 3);
174176
} else if (typeToUse.isScalar) {
175177
return indent(`this._${arg.name.value} = (${typeToUse.typeName}) args.get("${arg.name.value}");`, 3);
178+
} else if (typeToUse.isEnum) {
179+
return indentMultiline(
180+
`if (args.get("${arg.name.value}") instanceof ${typeToUse.typeName}) {
181+
this._${arg.name.value} = (${typeToUse.typeName}) args.get("${arg.name.value}");
182+
} else {
183+
this._${arg.name.value} = ${typeToUse.typeName}.valueOfLabel(args.get("${arg.name.value}"));
184+
}`,
185+
3
186+
);
176187
} else {
177188
return indent(`this._${arg.name.value} = new ${typeToUse.typeName}((Map<String, Object>) args.get("${arg.name.value}"));`, 3);
178189
}

packages/plugins/java/java/tests/java.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ describe('Java', () => {
204204
}`);
205205
});
206206

207+
it('Should generate check type for enum', async () => {
208+
const result = await plugin(schema, [], {}, { outputFile: OUTPUT_FILE });
209+
expect(result).toBeSimilarStringTo(`if (args.get("sort") instanceof ResultSort) {
210+
this._sort = (ResultSort) args.get("sort");
211+
} else {
212+
this._sort = ResultSort.valueOfLabel(args.get("sort"));
213+
}`);
214+
});
215+
207216
it('Should generate input class per each input, also with nested input types', async () => {
208217
const result = await plugin(schema, [], {}, { outputFile: OUTPUT_FILE });
209218

@@ -231,7 +240,11 @@ describe('Java', () => {
231240
this._username = (String) args.get("username");
232241
this._email = (String) args.get("email");
233242
this._name = (String) args.get("name");
234-
this._sort = (ResultSort) args.get("sort");
243+
if (args.get("sort") instanceof ResultSort) {
244+
this._sort = (ResultSort) args.get("sort");
245+
} else {
246+
this._sort = ResultSort.valueOfLabel(args.get("sort"));
247+
}
235248
this._metadata = new MetadataSearchInput((Map<String, Object>) args.get("metadata"));
236249
}
237250
}

0 commit comments

Comments
 (0)