Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,20 @@ public void addFromInterfaceModel(CodegenModel cm, List<Map<String, String>> mod
*/
@SuppressWarnings("unchecked")
public void addToImplementor(CodegenConfig cc, CodegenModel implcm, List<Map<String, String>> implImports, boolean addInterfaceImports) {
implcm.getVendorExtensions().putIfAbsent(X_IMPLEMENTS, new ArrayList<String>());
// The model may already declare x-implements in the spec, in which case the parsed value can be
// a scalar string or an immutable list. Normalize it to a fresh mutable list (preserving any
// existing entries) so the oneOf interfaces below can be appended without failing.
Object existing = implcm.getVendorExtensions().get(X_IMPLEMENTS);
List<String> impl = new ArrayList<>();
if (existing instanceof Collection) {
impl.addAll((Collection<String>) existing);
} else if (existing instanceof String && !((String) existing).isEmpty()) {
impl.add((String) existing);
}
implcm.getVendorExtensions().put(X_IMPLEMENTS, impl);

// Add implemented interfaces
for (String intf : additionalInterfaces) {
List<String> impl = (List<String>) implcm.getVendorExtensions().get(X_IMPLEMENTS);
impl.add(intf);
if (addInterfaceImports) {
// Add imports for interfaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7995,6 +7995,19 @@ void oneOf_issue_23577() throws IOException {
.fileContains("String type;");
}

@Test
void oneOf_issue_23577_userDefinedXImplements() throws IOException {
// Default oneOf-interface generation (without REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING):
// a member schema that already declares its own x-implements must still be able to
// receive the oneOf interface, i.e. the user-supplied x-implements value must remain mutable.
Map<String, File> files = generateFromContract("src/test/resources/3_0/oneOf_issue_23577.yaml", SPRING_BOOT,
Map.of(GENERATE_MODEL_DOCS, false, GENERATE_APIS, false, INTERFACE_ONLY, true));
JavaFileAssert.assertThat(files.get("CreatedEvent.java"))
.implementsInterfaces("com.example.Notification", "Event");
JavaFileAssert.assertThat(files.get("UpdatedEvent.java"))
.implementsInterfaces("Event");
}

@Test
void oneof_polymorphism_and_inheritance() throws IOException {
Map<String, File> files = generateFromContract("src/test/resources/3_0/oneof_polymorphism_and_inheritance.yaml", SPRING_BOOT,
Expand Down
Loading