Skip to content

Commit 193d53b

Browse files
authored
fix(core): do no set header example value when none is specified (#1468)
1 parent 019deba commit 193d53b

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AsyncAnnotationUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ public static SchemaObject getAsyncHeaders(AsyncOperation op, StringValueResolve
6969
property.setTitle(propertyName);
7070
property.setDescription(getDescription(headersValues, stringValueResolver));
7171
List<String> values = getHeaderValues(headersValues, stringValueResolver);
72-
property.setExamples(new ArrayList<>(values));
73-
property.setEnumValues(values);
72+
if (!values.isEmpty()) {
73+
property.setExamples(new ArrayList<>(values));
74+
property.setEnumValues(values);
75+
}
7476
headerSchema.getProperties().put(propertyName, property);
7577
});
7678

springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/scanners/common/annotation/AsyncAnnotationUtilTest.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
3131
import static org.junit.jupiter.api.Assertions.assertEquals;
3232
import static org.junit.jupiter.api.Assertions.assertNotEquals;
33+
import static org.junit.jupiter.api.Assertions.assertNull;
3334
import static org.junit.jupiter.api.Assertions.assertTrue;
3435
import static org.mockito.ArgumentMatchers.any;
3536
import static org.mockito.Mockito.mock;
@@ -68,8 +69,8 @@ void getAsyncHeaders(Class<?> classWithOperationBindingProcessor) throws NoSuchM
6869
SchemaObject headerWithoutValueResolved =
6970
(SchemaObject) headers.getProperties().get("headerWithoutValueResolved");
7071
assertEquals("string", headerWithoutValueResolved.getType());
71-
assertTrue(headerWithoutValueResolved.getExamples().isEmpty());
72-
assertTrue(headerWithoutValueResolved.getEnumValues().isEmpty());
72+
assertNull(headerWithoutValueResolved.getExamples());
73+
assertNull(headerWithoutValueResolved.getEnumValues());
7374
assertEquals("descriptionResolved", headerWithoutValueResolved.getDescription());
7475
}
7576

@@ -120,6 +121,36 @@ void getAsyncHeadersWithoutSchemaName() throws NoSuchMethodException {
120121
.build());
121122
}
122123

124+
@Test
125+
void getAsyncHeadersWithoutValue() throws NoSuchMethodException {
126+
// given
127+
Method m = ClassWithHeaders.class.getDeclaredMethod("withoutValue", String.class);
128+
AsyncOperation operation = m.getAnnotation(AsyncListener.class).operation();
129+
130+
StringValueResolver stringValueResolver = mock(StringValueResolver.class);
131+
when(stringValueResolver.resolveStringValue(any()))
132+
.thenAnswer(invocation -> invocation.getArgument(0).toString() + "Resolved");
133+
134+
// when
135+
SchemaObject headers = AsyncAnnotationUtil.getAsyncHeaders(operation, stringValueResolver);
136+
137+
// then
138+
assertThat(headers)
139+
.isEqualTo(SchemaObject.builder()
140+
.type(SchemaType.OBJECT)
141+
.title("Headers-472917891")
142+
.properties(Map.of(
143+
"headerResolved",
144+
SchemaObject.builder()
145+
.type(SchemaType.STRING)
146+
.title("headerResolved")
147+
.description("descriptionResolved")
148+
.enumValues(null)
149+
.examples(null)
150+
.build()))
151+
.build());
152+
}
153+
123154
@Test
124155
void generatedHeaderSchemaNameShouldBeUnique() throws NoSuchMethodException {
125156
// given
@@ -424,6 +455,20 @@ private void emptyHeaders(String payload) {}
424455
@TestOperationBindingProcessor.TestOperationBinding()
425456
private void withoutSchemaName(String payload) {}
426457

458+
@AsyncListener(
459+
operation =
460+
@AsyncOperation(
461+
channelName = "${test.property.test-channel}",
462+
headers =
463+
@AsyncOperation.Headers(
464+
values = {
465+
@AsyncOperation.Headers.Header(
466+
name = "header",
467+
description = "description")
468+
})))
469+
@TestOperationBindingProcessor.TestOperationBinding()
470+
private void withoutValue(String payload) {}
471+
427472
@AsyncListener(
428473
operation =
429474
@AsyncOperation(

0 commit comments

Comments
 (0)