Skip to content
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

Add support generate extensions for openapi generator. #1796

Merged
merged 1 commit into from
Oct 10, 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 @@ -31,6 +31,7 @@ public class JavadocDescription {
private String methodSummary;
private String methodDescription;
private String returnDescription;
private String deprecatedDescription;
private final Map<String, String> parameters = new HashMap<>(4);

/**
Expand Down Expand Up @@ -91,4 +92,23 @@ public String getReturnDescription() {
public void setReturnDescription(String returnDescription) {
this.returnDescription = returnDescription;
}

/**
* The deprecated description.
*
* @return The deprecated description
*/
@Nullable
public String getDeprecatedDescription() {
return deprecatedDescription;
}

/**
* Sets the deprecated description.
*
* @param deprecatedDescription The deprecated description.
*/
public void setDeprecatedDescription(String deprecatedDescription) {
this.deprecatedDescription = deprecatedDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.chhorz.javadoc.JavaDoc;
import com.github.chhorz.javadoc.JavaDocParserBuilder;
import com.github.chhorz.javadoc.OutputType;
import com.github.chhorz.javadoc.tags.DeprecatedTag;
import com.github.chhorz.javadoc.tags.ParamTag;
import com.github.chhorz.javadoc.tags.PropertyTag;
import com.github.chhorz.javadoc.tags.ReturnTag;
Expand All @@ -35,7 +36,7 @@
*/
public class JavadocParser {

private static final Set<String> IGNORED = CollectionUtils.setOf("see", "since", "author", "version", "deprecated", "throws", "exception", "category");
private static final Set<String> IGNORED = CollectionUtils.setOf("see", "since", "author", "version", "throws", "exception", "category");

private final FlexmarkHtmlConverter htmlToMarkdownConverter = FlexmarkHtmlConverter.builder()
.build();
Expand Down Expand Up @@ -77,6 +78,8 @@ public JavadocDescription parse(String text) {
} else if (tag instanceof PropertyTag propertyTag) {
String paramDesc = htmlToMarkdownConverter.convert(propertyTag.getParamDescription()).strip();
javadocDescription.getParameters().put(propertyTag.getPropertyName(), paramDesc);
} else if (tag instanceof DeprecatedTag deprecatedTag) {
javadocDescription.setDeprecatedDescription(htmlToMarkdownConverter.convert(deprecatedTag.getDeprecatedText()).strip());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@
import static io.micronaut.openapi.visitor.ContextUtils.warn;
import static io.micronaut.openapi.visitor.ConvertUtils.MAP_TYPE;
import static io.micronaut.openapi.visitor.ElementUtils.getJsonViewClass;
import static io.micronaut.openapi.visitor.ElementUtils.isDeprecated;
import static io.micronaut.openapi.visitor.ElementUtils.isFileUpload;
import static io.micronaut.openapi.visitor.ElementUtils.isIgnoredParameter;
import static io.micronaut.openapi.visitor.ElementUtils.isNotNullable;
import static io.micronaut.openapi.visitor.ElementUtils.isNullable;
import static io.micronaut.openapi.visitor.ElementUtils.isResponseType;
import static io.micronaut.openapi.visitor.ElementUtils.isSingleResponseType;
import static io.micronaut.openapi.visitor.GeneratorUtils.addOperationDeprecatedExtension;
import static io.micronaut.openapi.visitor.GeneratorUtils.addParameterDeprecatedExtension;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_ADD_ALWAYS;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_ALLOW_EMPTY_VALUE;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_ALLOW_RESERVED;
Expand Down Expand Up @@ -467,7 +470,7 @@ public void visitMethod(MethodElement element, VisitorContext context) {

javadocDescription = getMethodDescription(element, swaggerOperation);

if (element.isAnnotationPresent(Deprecated.class)) {
if (isDeprecated(element)) {
swaggerOperation.setDeprecated(true);
}

Expand Down Expand Up @@ -838,7 +841,7 @@ private void processParameter(VisitorContext context, OpenAPI openAPI,

io.swagger.v3.oas.models.media.MediaType mediaType = entry.getValue();

Schema<?> propertySchema = bindSchemaForElement(context, parameter, parameterType, mediaType.getSchema(), null);
Schema<?> propertySchema = bindSchemaForElement(context, parameter, parameterType, mediaType.getSchema(), null, false);

var bodyAnn = parameter.getAnnotation(Body.class);

Expand Down Expand Up @@ -918,7 +921,7 @@ private void processParameter(VisitorContext context, OpenAPI openAPI,
}

if (schema != null) {
schema = bindSchemaForElement(context, parameter, parameterType, schema, null);
schema = bindSchemaForElement(context, parameter, parameterType, schema, null, false);
newParameter.setSchema(schema);
}
}
Expand All @@ -943,20 +946,21 @@ private void processBodyParameter(VisitorContext context, OpenAPI openAPI, Javad
var jsonViewClass = getJsonViewClass(parameter, context);

Schema<?> propertySchema = resolveSchema(openAPI, parameter, parameter.getType(), context, Collections.singletonList(mediaType), jsonViewClass, null, null);
if (propertySchema != null) {

parameter.stringValue(io.swagger.v3.oas.annotations.Parameter.class, PROP_DESCRIPTION)
.ifPresent(propertySchema::setDescription);
processSchemaProperty(context, parameter, parameter.getType(), null, schema, propertySchema);
if (isNullable(parameter) && !isNotNullable(parameter)) {
// Keep null if not
SchemaUtils.setNullable(propertySchema);
}
if (javadocDescription != null && StringUtils.isEmpty(propertySchema.getDescription())) {
String doc = javadocDescription.getParameters().get(parameter.getName());
if (doc != null) {
propertySchema.setDescription(doc);
}
if (propertySchema == null) {
return;
}

parameter.stringValue(io.swagger.v3.oas.annotations.Parameter.class, PROP_DESCRIPTION)
.ifPresent(propertySchema::setDescription);
processSchemaProperty(context, parameter, parameter.getType(), null, schema, propertySchema);
if (isNullable(parameter) && !isNotNullable(parameter)) {
// Keep null if not
SchemaUtils.setNullable(propertySchema);
}
if (javadocDescription != null && StringUtils.isEmpty(propertySchema.getDescription())) {
String doc = javadocDescription.getParameters().get(parameter.getName());
if (doc != null) {
propertySchema.setDescription(doc);
}
}
}
Expand Down Expand Up @@ -1163,6 +1167,11 @@ private Parameter processMethodParameterAnnotation(VisitorContext context, Opera
newParameter.setRequired(null);
}

if (newParameter != null && isDeprecated(parameter)) {
newParameter.setDeprecated(true);
addParameterDeprecatedExtension(parameter, newParameter, context);
}

return newParameter;
}

Expand Down Expand Up @@ -1393,6 +1402,8 @@ private Map<PathItem, Operation> readOperations(String path, HttpMethod httpMeth
swaggerOperation = new Operation();
}

addOperationDeprecatedExtension(element, swaggerOperation, context);

if (CollectionUtils.isNotEmpty(swaggerOperation.getParameters())) {
swaggerOperation.getParameters().removeIf(Objects::isNull);
}
Expand Down Expand Up @@ -1439,10 +1450,6 @@ private Map<PathItem, Operation> readOperations(String path, HttpMethod httpMeth
if (required) {
swaggerParam.setRequired(true);
}
var deprecated = paramAnn.booleanValue(PROP_DEPRECATED).orElse(false);
if (deprecated) {
swaggerParam.setDeprecated(true);
}
var allowEmptyValue = paramAnn.booleanValue(PROP_ALLOW_EMPTY_VALUE).orElse(false);
if (allowEmptyValue) {
swaggerParam.setAllowEmptyValue(true);
Expand Down Expand Up @@ -1723,59 +1730,60 @@ private void processResponses(Operation operation,
apiResponses = new ApiResponses();
operation.setResponses(apiResponses);
}
if (CollectionUtils.isNotEmpty(responseAnns)) {
for (var responseAnn : responseAnns) {
String responseCode = responseAnn.stringValue(PROP_RESPONSE_CODE).orElse("default");
if (apiResponses.containsKey(responseCode)) {
continue;
}
ApiResponse newApiResponse = toValue(responseAnn.getValues(), context, ApiResponse.class, jsonViewClass);
if (newApiResponse != null) {
if (responseAnn.booleanValue("useReturnTypeSchema").orElse(false) && element != null) {
addResponseContent(element, context, Utils.resolveOpenApi(context), newApiResponse, jsonViewClass);
} else {
if (CollectionUtils.isEmpty(responseAnns)) {
return;
}
for (var responseAnn : responseAnns) {
String responseCode = responseAnn.stringValue(PROP_RESPONSE_CODE).orElse("default");
if (apiResponses.containsKey(responseCode)) {
continue;
}
ApiResponse newApiResponse = toValue(responseAnn.getValues(), context, ApiResponse.class, jsonViewClass);
if (newApiResponse != null) {
if (responseAnn.booleanValue("useReturnTypeSchema").orElse(false) && element != null) {
addResponseContent(element, context, Utils.resolveOpenApi(context), newApiResponse, jsonViewClass);
} else {

List<MediaType> producesMediaTypes = producesMediaTypes(element);
List<MediaType> producesMediaTypes = producesMediaTypes(element);

var contentAnns = responseAnn.get(PROP_CONTENT, io.swagger.v3.oas.annotations.media.Content[].class).orElse(null);
var mediaTypes = new ArrayList<String>();
if (ArrayUtils.isNotEmpty(contentAnns)) {
for (io.swagger.v3.oas.annotations.media.Content contentAnn : contentAnns) {
if (StringUtils.isNotEmpty(contentAnn.mediaType())) {
mediaTypes.add(contentAnn.mediaType());
} else {
mediaTypes.add(MediaType.APPLICATION_JSON);
}
var contentAnns = responseAnn.get(PROP_CONTENT, io.swagger.v3.oas.annotations.media.Content[].class).orElse(null);
var mediaTypes = new ArrayList<String>();
if (ArrayUtils.isNotEmpty(contentAnns)) {
for (io.swagger.v3.oas.annotations.media.Content contentAnn : contentAnns) {
if (StringUtils.isNotEmpty(contentAnn.mediaType())) {
mediaTypes.add(contentAnn.mediaType());
} else {
mediaTypes.add(MediaType.APPLICATION_JSON);
}
}
Content newContent = newApiResponse.getContent();
if (newContent != null) {
io.swagger.v3.oas.models.media.MediaType defaultMediaType = newContent.get(MediaType.APPLICATION_JSON);
var contentFromProduces = new Content();
for (String mt : mediaTypes) {
if (mt.equals(MediaType.APPLICATION_JSON)) {
for (MediaType mediaType : producesMediaTypes) {
contentFromProduces.put(mediaType.toString(), defaultMediaType);
}
} else {
contentFromProduces.put(mt, newContent.get(mt));
}
Content newContent = newApiResponse.getContent();
if (newContent != null) {
io.swagger.v3.oas.models.media.MediaType defaultMediaType = newContent.get(MediaType.APPLICATION_JSON);
var contentFromProduces = new Content();
for (String mt : mediaTypes) {
if (mt.equals(MediaType.APPLICATION_JSON)) {
for (MediaType mediaType : producesMediaTypes) {
contentFromProduces.put(mediaType.toString(), defaultMediaType);
}
} else {
contentFromProduces.put(mt, newContent.get(mt));
}
newApiResponse.setContent(contentFromProduces);
}
newApiResponse.setContent(contentFromProduces);
}
try {
if (StringUtils.isEmpty(newApiResponse.getDescription())) {
newApiResponse.setDescription(responseCode.equals(PROP_DEFAULT) ? "OK response" : HttpStatus.getDefaultReason(Integer.parseInt(responseCode)));
}
} catch (Exception e) {
newApiResponse.setDescription("Response " + responseCode);
}
try {
if (StringUtils.isEmpty(newApiResponse.getDescription())) {
newApiResponse.setDescription(responseCode.equals(PROP_DEFAULT) ? "OK response" : HttpStatus.getDefaultReason(Integer.parseInt(responseCode)));
}
apiResponses.put(responseCode, newApiResponse);
} catch (Exception e) {
newApiResponse.setDescription("Response " + responseCode);
}
apiResponses.put(responseCode, newApiResponse);
}
operation.setResponses(apiResponses);
}
operation.setResponses(apiResponses);
}

// boolean - is swagger schema has implementation
Expand Down
Loading
Loading