Skip to content

Commit

Permalink
fix: more safe work with mapped refs to use oneOf baseNames
Browse files Browse the repository at this point in the history
  • Loading branch information
DDtKey committed Feb 21, 2024
1 parent 401bedb commit 678c671
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,13 @@ public CodegenModel fromModel(String name, Schema model) {
throw new RuntimeException("oneOf size does not match the model");
}

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

Expand All @@ -234,8 +236,8 @@ public CodegenModel fromModel(String name, Schema model) {
if (aliasName != null) {
oneOf.setName(toModelName(aliasName));
}
if (oneOf.getRef() != null) {
oneOf.setBaseName(mappedNameByRef.get(oneOf.getRef()));
if (oneOf.getRef() != null && !mappedNamesByRef.isEmpty()) {
oneOf.setBaseName(mappedNamesByRef.get(oneOf.getRef()).removeFirst());
}

}
Expand Down

0 comments on commit 678c671

Please sign in to comment.