Skip to content

Commit

Permalink
#124 bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
milesha committed Nov 19, 2024
1 parent 4a5c287 commit 918ade5
Showing 1 changed file with 49 additions and 49 deletions.
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) -> {
Expand All @@ -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()
);
});
});


};
}

Expand Down

0 comments on commit 918ade5

Please sign in to comment.