Skip to content

Can now read record Javadocs for OpenApi #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
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 @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Stream;
Expand Down Expand Up @@ -168,7 +169,7 @@ Schema<?> toSchema(TypeMirror type) {
private Schema<?> buildEnumSchema(Element e) {
var schema = new StringSchema();
e.getEnclosedElements().stream()
.filter(ec -> ec.getKind().equals(ElementKind.ENUM_CONSTANT))
.filter(ec -> ElementKind.ENUM_CONSTANT.equals(ec.getKind()))
.forEach(ec -> schema.addEnumItem(ec.getSimpleName().toString()));

var doc = Javadoc.parse(elements.getDocComment(e));
Expand Down Expand Up @@ -267,8 +268,21 @@ private void setFormatFromValidation(Element element, Schema<?> propSchema) {

private void setDescription(Element element, Schema<?> propSchema) {
var doc = Javadoc.parse(elements.getDocComment(element));

if (!doc.getSummary().isEmpty()) {
propSchema.setDescription(doc.getSummary());
return;
}
try {

final var enclosingElement = element.getEnclosingElement();
if (enclosingElement.getKind() == ElementKind.valueOf("RECORD")) {
Optional.of(Javadoc.parse(elements.getDocComment(enclosingElement)))
.map(d -> d.getParams().get(element.getSimpleName().toString()))
.ifPresent(propSchema::setDescription);
}
} catch (IllegalArgumentException e) {
// not on jdk 16+
}
}

Expand Down Expand Up @@ -322,7 +336,9 @@ private void gatherProperties(List<VariableElement> fields, Element element) {
}
if (element instanceof TypeElement) {
Element mappedSuper = types.asElement(((TypeElement) element).getSuperclass());
if (mappedSuper != null && !"java.lang.Object".equals(mappedSuper.toString())) {
if (mappedSuper != null
&& !"java.lang.Object".equals(mappedSuper.toString())
&& !"java.lang.Record".equals(mappedSuper.toString())) {
gatherProperties(fields, mappedSuper);
}
for (VariableElement field : ElementFilter.fieldsIn(element.getEnclosedElements())) {
Expand Down
8 changes: 5 additions & 3 deletions tests/test-nima-jsonb/src/main/java/org/example/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import io.avaje.jsonb.Json;

/**
* @param id the id
* @param name the name
*/
@Json
public record Person(long id, String name) {

}
public record Person(long id, String name) {}