Skip to content

Commit 246a0e6

Browse files
committed
Fixed a bug that javadoc of record class parameters was not recognized. Fixes springdoc#2131
1 parent 9a6c1b9 commit 246a0e6

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/providers/JavadocProvider.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* *
44
* * *
5-
* * * * Copyright 2019-2022 the original author or authors.
5+
* * * * Copyright 2019-2023 the original author or authors.
66
* * * *
77
* * * * Licensed under the Apache License, Version 2.0 (the "License");
88
* * * * you may not use this file except in compliance with the License.
@@ -41,11 +41,19 @@ public interface JavadocProvider {
4141
String getClassJavadoc(Class<?> cl);
4242

4343
/**
44-
* Gets method description.
44+
* Gets param descripton of record class.
4545
*
46-
* @param method the method
47-
* @return the method description
46+
* @param cl the class
47+
* @return map of field and param descriptions
4848
*/
49+
Map<String, String> getRecordClassParamJavadoc(Class<?> cl);
50+
51+
/**
52+
* Gets method description.
53+
*
54+
* @param method the method
55+
* @return the method description
56+
*/
4957
String getMethodJavadocDescription(Method method);
5058

5159
/**
@@ -88,4 +96,3 @@ public interface JavadocProvider {
8896
*/
8997
String getFirstSentence(String text);
9098
}
91-

springdoc-openapi-javadoc/src/main/java/org/springdoc/openapi/javadoc/JavadocPropertyCustomizer.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* *
44
* * *
5-
* * * * Copyright 2019-2022 the original author or authors.
5+
* * * * Copyright 2019-2023 the original author or authors.
66
* * * *
77
* * * * Licensed under the Apache License, Version 2.0 (the "License");
88
* * * * you may not use this file except in compliance with the License.
@@ -109,13 +109,23 @@ else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedS
109109
* @param fields the fields
110110
* @param existingSchema the existing schema
111111
*/
112-
private void setJavadocDescription(Class<?> cls, List<Field> fields, Schema existingSchema) {
112+
void setJavadocDescription(Class<?> cls, List<Field> fields, Schema existingSchema) {
113113
if (existingSchema != null) {
114114
if (StringUtils.isBlank(existingSchema.getDescription())) {
115115
existingSchema.setDescription(javadocProvider.getClassJavadoc(cls));
116116
}
117117
Map<String, Schema> properties = existingSchema.getProperties();
118-
if (!CollectionUtils.isEmpty(properties))
118+
if (!CollectionUtils.isEmpty(properties)) {
119+
if (cls.getSuperclass() != null && "java.lang.Record".equals(cls.getSuperclass().getName())) {
120+
Map<String, String> recordParamMap = javadocProvider.getRecordClassParamJavadoc(cls);
121+
properties.entrySet().stream()
122+
.filter(stringSchemaEntry -> StringUtils.isBlank(stringSchemaEntry.getValue().getDescription()))
123+
.forEach(stringSchemaEntry -> {
124+
if (recordParamMap.containsKey(stringSchemaEntry.getKey()))
125+
stringSchemaEntry.getValue().setDescription(recordParamMap.get(stringSchemaEntry.getKey()));
126+
});
127+
}
128+
119129
properties.entrySet().stream()
120130
.filter(stringSchemaEntry -> StringUtils.isBlank(stringSchemaEntry.getValue().getDescription()))
121131
.forEach(stringSchemaEntry -> {
@@ -126,6 +136,7 @@ private void setJavadocDescription(Class<?> cls, List<Field> fields, Schema exis
126136
stringSchemaEntry.getValue().setDescription(fieldJavadoc);
127137
});
128138
});
139+
}
129140
fields.stream().filter(f -> f.isAnnotationPresent(JsonUnwrapped.class))
130141
.forEach(f -> setJavadocDescription(f.getType(), FieldUtils.getAllFieldsList(f.getType()), existingSchema));
131142

springdoc-openapi-javadoc/src/main/java/org/springdoc/openapi/javadoc/SpringDocJavadocProvider.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* *
44
* * *
5-
* * * * Copyright 2019-2022 the original author or authors.
5+
* * * * Copyright 2019-2023 the original author or authors.
66
* * * *
77
* * * * Licensed under the Apache License, Version 2.0 (the "License");
88
* * * * you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import java.lang.reflect.Method;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.stream.Collectors;
2930

3031
import com.github.therapi.runtimejavadoc.ClassJavadoc;
3132
import com.github.therapi.runtimejavadoc.CommentFormatter;
@@ -64,6 +65,19 @@ public String getClassJavadoc(Class<?> cl) {
6465
return formatter.format(classJavadoc.getComment());
6566
}
6667

68+
/**
69+
* Gets param descripton of record class.
70+
*
71+
* @param cl the class
72+
* @return map of field and param descriptions
73+
*/
74+
@Override
75+
public Map<String, String> getRecordClassParamJavadoc(Class<?> cl) {
76+
ClassJavadoc classJavadoc = RuntimeJavadoc.getJavadoc(cl);
77+
return classJavadoc.getRecordComponents().stream()
78+
.collect(Collectors.toMap(ParamJavadoc::getName, record -> formatter.format(record.getComment())));
79+
}
80+
6781
/**
6882
* Gets method javadoc description.
6983
*

0 commit comments

Comments
 (0)