diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/OpenAPIConfiguration.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/OpenAPIConfiguration.java index a225cbe5..0323e4b2 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/OpenAPIConfiguration.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/OpenAPIConfiguration.java @@ -1,39 +1,29 @@ 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 = "felixtj.dietrich@tum.de" - ), - 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 = "felixtj.dietrich@tum.de"), + license = @License(name = "MIT License", url = "https://github.com/ls1intum/Hephaestus/blob/develop/LICENSE") + ), + servers = { @Server(url = "/", description = "Default Server URL") } ) public class OpenAPIConfiguration { @@ -41,24 +31,29 @@ public class OpenAPIConfiguration { @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> 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() + ); + }); }); - - }; }