-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,59 @@ | ||
package de.tum.in.www1.hephaestus; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.info.Contact; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.info.License; | ||
import io.swagger.v3.oas.annotations.servers.Server; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springdoc.core.customizers.OpenApiCustomizer; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.info.Contact; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.info.License; | ||
import io.swagger.v3.oas.annotations.servers.Server; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
|
||
@Configuration | ||
@OpenAPIDefinition( | ||
info = @Info( | ||
title = "Hephaestus API", | ||
description = "API documentation for the Hephaestus application server.", | ||
title = "Hephaestus API", | ||
description = "API documentation for the Hephaestus application server.", | ||
version = "0.0.1", | ||
contact = @Contact( | ||
name = "Felix T.J. Dietrich", | ||
email = "[email protected]" | ||
), | ||
license = @License( | ||
name = "MIT License", | ||
url = "https://github.com/ls1intum/Hephaestus/blob/develop/LICENSE" | ||
) | ||
), | ||
servers = { | ||
@Server(url = "/", description = "Default Server URL"), | ||
} | ||
contact = @Contact(name = "Felix T.J. Dietrich", email = "[email protected]"), | ||
license = @License(name = "MIT License", url = "https://github.com/ls1intum/Hephaestus/blob/develop/LICENSE") | ||
), | ||
servers = { @Server(url = "/", description = "Default Server URL") } | ||
) | ||
public class OpenAPIConfiguration { | ||
|
||
Logger logger = LoggerFactory.getLogger(OpenAPIConfiguration.class); | ||
|
||
@Bean | ||
public OpenApiCustomizer schemaCustomizer() { | ||
return openApi -> { | ||
return openApi -> { | ||
var components = openApi.getComponents(); | ||
|
||
// Only include schemas with DTO suffix and remove the suffix | ||
var schemas = components | ||
.getSchemas() | ||
.entrySet() | ||
.stream() | ||
.filter(entry -> entry.getKey().endsWith("DTO")) | ||
.collect(Collectors.toMap(entry -> entry.getKey().substring(0, entry.getKey().length() - 3), | ||
entry -> { | ||
var schema = entry.getValue(); | ||
schema.setName(entry.getKey().substring(0, entry.getKey().length() - 3)); | ||
return schema; | ||
})); | ||
.getSchemas() | ||
.entrySet() | ||
.stream() | ||
.filter(entry -> entry.getKey().endsWith("DTO")) | ||
.collect( | ||
Collectors.toMap( | ||
entry -> entry.getKey().substring(0, entry.getKey().length() - 3), | ||
entry -> { | ||
var schema = entry.getValue(); | ||
schema.setName(entry.getKey().substring(0, entry.getKey().length() - 3)); | ||
return schema; | ||
} | ||
) | ||
); | ||
|
||
// Remove DTO suffix from attribute names | ||
schemas.forEach((key, value) -> { | ||
@SuppressWarnings("unchecked") | ||
Map<String, Schema<?>> properties = value.getProperties(); | ||
if (properties != null) { | ||
properties.forEach((propertyKey, propertyValue) -> { | ||
|
@@ -72,25 +67,30 @@ public OpenApiCustomizer schemaCustomizer() { | |
var paths = openApi.getPaths(); | ||
paths.forEach((path, pathItem) -> { | ||
logger.info(path); | ||
pathItem.readOperations().forEach(operation -> { | ||
// Remove DTO suffix from reponse schemas | ||
var responses = operation.getResponses(); | ||
responses.forEach((responseCode, response) -> { | ||
var content = response.getContent(); | ||
content.forEach((contentType, mediaType) -> { | ||
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema()); | ||
|
||
pathItem | ||
.readOperations() | ||
.forEach(operation -> { | ||
// Remove DTO suffix from reponse schemas | ||
var responses = operation.getResponses(); | ||
responses.forEach((responseCode, response) -> { | ||
var content = response.getContent(); | ||
content.forEach((contentType, mediaType) -> { | ||
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema()); | ||
}); | ||
}); | ||
}); | ||
if (operation.getRequestBody() != null) { | ||
var requestBodyContent = operation.getRequestBody().getContent(); | ||
requestBodyContent.forEach((contentType, mediaType) -> { | ||
removeDTOSuffixesFromSchemaRecursively(mediaType.getSchema()); | ||
}); | ||
} | ||
|
||
// Remove -controller suffix from tags | ||
operation.setTags(operation.getTags() | ||
.stream() | ||
.map(tag -> tag.substring(0, tag.length() - 11)).toList()); | ||
}); | ||
// Remove -controller suffix from tags | ||
operation.setTags( | ||
operation.getTags().stream().map(tag -> tag.substring(0, tag.length() - 11)).toList() | ||
); | ||
}); | ||
}); | ||
|
||
|
||
}; | ||
} | ||
|
||
|