Skip to content

Commit 678c671

Browse files
committed
fix: more safe work with mapped refs to use oneOf baseNames
1 parent 401bedb commit 678c671

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,13 @@ public CodegenModel fromModel(String name, Schema model) {
219219
throw new RuntimeException("oneOf size does not match the model");
220220
}
221221

222-
Map<String, String> mappedNameByRef = Optional.ofNullable(model.getDiscriminator())
222+
// Reverse mapped references to use as baseName for oneOF, but different keys may point to the same $ref.
223+
// Thus, we group them by the value
224+
Map<String, List<String>> mappedNamesByRef = Optional.ofNullable(model.getDiscriminator())
223225
.map(Discriminator::getMapping)
224-
.map(mapping -> mapping.entrySet()
225-
.stream()
226-
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))
226+
.map(mapping -> mapping.entrySet().stream()
227+
.collect(Collectors.groupingBy(Map.Entry::getValue,
228+
Collectors.mapping(Map.Entry::getKey, Collectors.toList())))
227229
)
228230
.orElse(Collections.emptyMap());
229231

@@ -234,8 +236,8 @@ public CodegenModel fromModel(String name, Schema model) {
234236
if (aliasName != null) {
235237
oneOf.setName(toModelName(aliasName));
236238
}
237-
if (oneOf.getRef() != null) {
238-
oneOf.setBaseName(mappedNameByRef.get(oneOf.getRef()));
239+
if (oneOf.getRef() != null && !mappedNamesByRef.isEmpty()) {
240+
oneOf.setBaseName(mappedNamesByRef.get(oneOf.getRef()).removeFirst());
239241
}
240242

241243
}

0 commit comments

Comments
 (0)