Skip to content

support get javadoc description from getter method #2385

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

Closed
wants to merge 2 commits into from
Closed
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 @@ -24,11 +24,11 @@

package org.springdoc.core.customizers;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

import com.fasterxml.jackson.databind.JavaType;
import io.swagger.v3.core.converter.AnnotatedType;
Expand Down Expand Up @@ -76,15 +76,19 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
Class<?> cls = javaType.getRawClass();
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
List<Field> fields = FieldUtils.getAllFieldsList(cls);
if (!CollectionUtils.isEmpty(fields)) {
List<PropertyDescriptor> clsProperties = new ArrayList<>();
try {
clsProperties = Arrays.asList(Introspector.getBeanInfo(cls).getPropertyDescriptors());
} catch (IntrospectionException ignored) {}
if (!CollectionUtils.isEmpty(fields) || !CollectionUtils.isEmpty(clsProperties)) {
if (!type.isSchemaProperty()) {
Schema existingSchema = context.resolve(type);
setJavadocDescription(cls, fields, existingSchema);
setJavadocDescription(cls, fields, clsProperties, existingSchema);
}
else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedSchema.get$ref().contains(AnnotationsUtils.COMPONENTS_REF)) {
String schemaName = resolvedSchema.get$ref().substring(21);
Schema existingSchema = context.getDefinedModels().get(schemaName);
setJavadocDescription(cls, fields, existingSchema);
setJavadocDescription(cls, fields, clsProperties, existingSchema);
}
}
return resolvedSchema;
Expand All @@ -96,7 +100,9 @@ else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedS
/**
* Sets javadoc description.
*
* @param cls the cls
* @param fields the fields
* @param clsProperties the bean properties of cls
* @param existingSchema the existing schema
*/
private void setJavadocDescription(Class<?> cls, List<Field> fields, Schema existingSchema) {
Expand Down Expand Up @@ -124,6 +130,14 @@ private void setJavadocDescription(Class<?> cls, List<Field> fields, Schema exis
if (StringUtils.isNotBlank(fieldJavadoc))
stringSchemaEntry.getValue().setDescription(fieldJavadoc);
});
if (StringUtils.isBlank(stringSchemaEntry.getValue().getDescription())) {
Optional<PropertyDescriptor> optionalPd = clsProperties.stream().filter(pd -> pd.getName().equals(stringSchemaEntry.getKey())).findAny();
optionalPd.ifPresent(pd1 -> {
String fieldJavadoc = javadocProvider.getMethodJavadocDescription(pd1.getReadMethod());
if (StringUtils.isNotBlank(fieldJavadoc))
stringSchemaEntry.getValue().setDescription(fieldJavadoc);
});
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* * *
* * * *
* * * * * Copyright 2019-2022 the original author or authors.
* * * * * Copyright 2019-2023 the original author or authors.
* * * * *
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -124,31 +124,16 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
MethodParameter p = parameters[i];
Class<?> paramClass = AdditionalModelsConverter.getParameterObjectReplacement(p.getParameterType());

if (!MethodParameterPojoExtractor.isSimpleType(paramClass) && (p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class))) {
boolean hasFlatAnnotation = p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class);
boolean hasNotFlatAnnotation = Arrays.stream(p.getParameterAnnotations())
.anyMatch(annotation -> Arrays.asList(RequestBody.class, RequestPart.class).contains(annotation.annotationType()));
if (!MethodParameterPojoExtractor.isSimpleType(paramClass)
&& (hasFlatAnnotation || (defaultFlatParamObject && !hasNotFlatAnnotation && !AbstractRequestService.isRequestTypeToIgnore(paramClass)))) {
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
optionalDelegatingMethodParameterCustomizer.ifPresent(customizer -> customizer.customize(p, methodParameter));
explodedParameters.add(methodParameter);
});
}
else if (defaultFlatParamObject) {
boolean isSimpleType = MethodParameterPojoExtractor.isSimpleType(paramClass);
List<Annotation> annotations = Arrays.stream(p.getParameterAnnotations())
.filter(annotation -> Arrays.asList(RequestBody.class, RequestPart.class).contains(annotation.annotationType()))
.toList();
boolean hasAnnotation = !annotations.isEmpty();
boolean shouldFlat = !isSimpleType && !hasAnnotation;
if (shouldFlat && !AbstractRequestService.isRequestTypeToIgnore(paramClass)) {
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
optionalDelegatingMethodParameterCustomizer
.ifPresent(customizer -> customizer.customize(p, methodParameter));
explodedParameters.add(methodParameter);
});
}
else {
String name = pNames != null ? pNames[i] : p.getParameterName();
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false, false));
}
}
else {
String name = pNames != null ? pNames[i] : p.getParameterName();
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false, false));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
*
* * Copyright 2019-2023 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app170;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* The type Hello controller.
*/
@RestController
public class HelloController {

/**
* Persons string.
*
* @param dto the dto
* @return the PersonProjection
*/
@GetMapping(value = "/persons")
public PersonProjection persons(final PersonDTO dto) {
return new PersonDTO();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* * Copyright 2019-2023 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app170;

/**
* The type Person dto.
*/
public class PersonDTO implements PersonProjection {
private String email;

private String firstName;

private String lastName;

/**
* Instantiates a new Person dto.
*/
public PersonDTO() {
}

/**
* Instantiates a new Person dto.
*
* @param email the email
* @param firstName the first name
* @param lastName the last name
*/
public PersonDTO(final String email, final String firstName, final String lastName) {
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(final String email) {
this.email = email;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(final String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(final String lastName) {
this.lastName = lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* * Copyright 2019-2023 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app170;

/**
* The type Person dto.
*/
public interface PersonProjection {
/**
* Gets email.
*
*/
String getEmail();

/**
* Gets first name.
*
*/
String getFirstName();

/**
* Gets last name.
*
*/
String getLastName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* * Copyright 2019-2023 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app170;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import test.org.springdoc.api.AbstractSpringDocTest;

/**
* The type Spring doc app 170 test.
*/
public class SpringDocApp170Test extends AbstractSpringDocTest {

/**
* The type Spring doc test app.
*/
@SpringBootApplication
static class SpringDocTestApp {}
}
Loading