-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/create new header schema when modified (#941)
* test: reproduce GH-938 (wip) * feat(core): create new header schema when properties from multiple schemas are merged GH-938 Co-authored-by: Timon Back <[email protected]> --------- Co-authored-by: David Müller <[email protected]>
- Loading branch information
Showing
10 changed files
with
240 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../io/github/springwolf/core/asyncapi/scanners/common/headers/HeaderSchemaObjectMerger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.core.asyncapi.scanners.common.headers; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject; | ||
import io.github.springwolf.asyncapi.v3.model.schema.SchemaType; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HeaderSchemaObjectMerger { | ||
|
||
public static SchemaObject merge(SchemaObject initial, SchemaObject... schemas) { | ||
|
||
int additionalProperties = Arrays.stream(schemas) | ||
.filter(schemaObject -> schemaObject != null && schemaObject.getProperties() != null) | ||
.mapToInt(schema -> schema.getProperties().size()) | ||
.sum(); | ||
if (additionalProperties == 0) { | ||
return initial; | ||
} | ||
|
||
SchemaObject.SchemaObjectBuilder headerSchemaBuilder = | ||
SchemaObject.builder().type(SchemaType.OBJECT); | ||
|
||
String title = initial.getTitle(); | ||
String description = initial.getDescription(); | ||
Map<String, Object> headerProperties = new HashMap<>(initial.getProperties()); | ||
|
||
for (SchemaObject schema : schemas) { | ||
if (schema == null) { | ||
continue; | ||
} | ||
|
||
if (StringUtils.isBlank(description)) { | ||
description = schema.getDescription(); | ||
} | ||
|
||
schema.getProperties().forEach(headerProperties::putIfAbsent); | ||
} | ||
|
||
return headerSchemaBuilder | ||
.title(generateTitle(initial, headerProperties)) | ||
.description(description) | ||
.properties(headerProperties) | ||
.build(); | ||
} | ||
|
||
public static String generateHeaderSchemaName(Object object) { | ||
return generateHeaderSchemaName("Headers", object); | ||
} | ||
|
||
private static String generateHeaderSchemaName(String prefix, Object object) { | ||
return prefix + "-" + Math.abs(object.hashCode()); | ||
} | ||
|
||
private static String generateTitle(SchemaObject initial, Map<String, Object> headerProperties) { | ||
if (StringUtils.isBlank(initial.getTitle())) { | ||
return generateHeaderSchemaName(headerProperties); | ||
} else { | ||
return generateHeaderSchemaName(initial.getTitle(), headerProperties.hashCode()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
...olf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SchemaObjectMerger.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...github/springwolf/core/asyncapi/scanners/common/headers/HeaderSchemaObjectMergerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.core.asyncapi.scanners.common.headers; | ||
|
||
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class HeaderSchemaObjectMergerTest { | ||
|
||
@Test | ||
void merge() { | ||
SchemaObject initial = | ||
SchemaObject.builder().properties(new HashMap<>()).build(); | ||
initial.getProperties().put("key1", "value1"); | ||
|
||
// when | ||
SchemaObject merged = HeaderSchemaObjectMerger.merge(initial); | ||
|
||
// then | ||
assertEquals( | ||
merged, | ||
SchemaObject.builder().properties(Map.of("key1", "value1")).build()); | ||
} | ||
|
||
@Test | ||
void mergeAndIgnoreNullValues() { | ||
SchemaObject initial = | ||
SchemaObject.builder().properties(new HashMap<>()).build(); | ||
initial.getProperties().put("key1", "value1"); | ||
|
||
// when | ||
SchemaObject merged = HeaderSchemaObjectMerger.merge( | ||
initial, | ||
null, | ||
SchemaObject.builder().build(), | ||
SchemaObject.builder().properties(Collections.emptyMap()).build()); | ||
|
||
// then | ||
assertEquals( | ||
merged, | ||
SchemaObject.builder().properties(Map.of("key1", "value1")).build()); | ||
} | ||
|
||
@Test | ||
void mergeWhileNotOverwritingInitialValues() { | ||
SchemaObject initial = SchemaObject.builder() | ||
.title("initial-title") | ||
.description("this-is-initial") | ||
.properties(new HashMap<>()) | ||
.build(); | ||
initial.getProperties().put("key1", "value1"); | ||
|
||
SchemaObject schema1 = SchemaObject.builder() | ||
.description("this-is-schema1") | ||
.properties(new HashMap<>()) | ||
.build(); | ||
schema1.getProperties().put("key1", "value2"); | ||
schema1.getProperties().put("key2", "value2"); | ||
|
||
SchemaObject schema2 = SchemaObject.builder() | ||
.description("this-is-schema2") | ||
.properties(new HashMap<>()) | ||
.build(); | ||
schema2.getProperties().put("key2", "value3"); | ||
schema2.getProperties().put("key3", "value3"); | ||
|
||
// when | ||
SchemaObject merged = HeaderSchemaObjectMerger.merge(initial, schema1, schema2); | ||
|
||
// then | ||
assertEquals(merged.getTitle(), "initial-title-1820791802"); | ||
assertEquals(merged.getDescription(), "this-is-initial"); | ||
assertEquals(merged.getProperties().size(), 3); | ||
assertEquals(merged.getProperties().get("key1"), "value1"); | ||
assertEquals(merged.getProperties().get("key2"), "value2"); | ||
assertEquals(merged.getProperties().get("key3"), "value3"); | ||
} | ||
|
||
@Test | ||
void mergeAndTakeFirstNonNull() { | ||
SchemaObject initial = | ||
SchemaObject.builder().properties(new HashMap<>()).build(); | ||
|
||
SchemaObject schema1 = SchemaObject.builder() | ||
.description("this-is-schema1") | ||
.properties(new HashMap<>()) | ||
.build(); | ||
schema1.getProperties().put("key2", "value2"); | ||
|
||
SchemaObject schema2 = SchemaObject.builder() | ||
.description("this-is-schema2") | ||
.properties(new HashMap<>()) | ||
.build(); | ||
schema2.getProperties().put("key2", "value3"); | ||
|
||
// when | ||
SchemaObject merged = HeaderSchemaObjectMerger.merge(initial, schema1, schema2); | ||
|
||
// then | ||
assertEquals(merged.getTitle(), "Headers-824725166"); | ||
assertEquals(merged.getDescription(), "this-is-schema1"); | ||
assertEquals(merged.getProperties().size(), 1); | ||
assertEquals(merged.getProperties().get("key2"), "value2"); | ||
} | ||
} |
Oops, something went wrong.