Skip to content

Commit

Permalink
#251 Fix Parameter without description
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Aug 8, 2023
1 parent 9f5f984 commit 3d23b91
Show file tree
Hide file tree
Showing 27 changed files with 427 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.sngular.api.generator.plugin.openapi.utils.MapperUtil;
import com.sngular.api.generator.plugin.openapi.utils.OpenApiUtil;
import freemarker.template.TemplateException;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -164,13 +165,13 @@ private void createAuthTemplates(final SpecFile specFile) throws TemplateExcepti
}

private GlobalObject createApiTemplate(final SpecFile specFile, final String filePathToSave, final JsonNode openAPI) {
final Map<String, Map<String, JsonNode>> apis = OpenApiUtil.mapApiGroups(openAPI, specFile.isUseTagsGroup());
final MultiValuedMap<String, Map<String, JsonNode>> apis = OpenApiUtil.mapApiGroups(openAPI, specFile.isUseTagsGroup());
final var authSchemaList = MapperAuthUtil.createAuthSchemaList(openAPI);
final GlobalObject globalObject = MapperPathUtil.mapOpenApiObjectToOurModels(openAPI, authSchemaList);

for (Map.Entry<String, Map<String, JsonNode>> apisEntry : apis.entrySet()) {
final String javaFileName = OpenApiUtil.processJavaFileName(apisEntry.getKey());
final List<PathObject> pathObjects = MapperPathUtil.mapPathObjects(specFile, apisEntry, globalObject, baseDir);
for (var apisKey : apis.keySet()) {
final String javaFileName = OpenApiUtil.processJavaFileName(apisKey);
final List<PathObject> pathObjects = MapperPathUtil.mapPathObjects(specFile, apis.get(apisKey), globalObject, baseDir);
final AuthObject authObject = MapperAuthUtil.getApiAuthObject(globalObject.getAuthSchemas(), pathObjects);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -71,15 +72,17 @@ private static List<String> getSecurityRequirementList(final JsonNode securityNo
return authSecList;
}

public static List<PathObject> mapPathObjects(final SpecFile specFile, final Entry<String, Map<String, JsonNode>> path, final GlobalObject globalObject, final Path baseDir) {
public static List<PathObject> mapPathObjects(final SpecFile specFile, final Collection<Map<String, JsonNode>> path, final GlobalObject globalObject, final Path baseDir) {
final List<PathObject> pathObjects = new ArrayList<>();
for (Entry<String, JsonNode> pathItem : path.getValue().entrySet()) {
final PathObject pathObject = PathObject.builder()
.pathName(pathItem.getKey())
.globalObject(globalObject)
.operationObjects(mapOperationObject(specFile, pathItem, globalObject, baseDir))
.build();
pathObjects.add(pathObject);
for (var pathMap : path) {
for (var pathItem : pathMap.entrySet()) {
final PathObject pathObject = PathObject.builder()
.pathName(pathItem.getKey())
.globalObject(globalObject)
.operationObjects(mapOperationObject(specFile, pathItem, globalObject, baseDir))
.build();
pathObjects.add(pathObject);
}
}

return pathObjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@

public class OpenApiUtil {

static final ObjectMapper PARSER = new ObjectMapper(new YAMLFactory());
public static final String PATHS = "paths";

static final ObjectMapper PARSER = new ObjectMapper(new YAMLFactory());
static final Set<String> REST_VERB_SET = Set.of("get", "post", "delete", "patch", "put");
public static final String PATHS = "paths";

private OpenApiUtil() {}

public static Map<String, Map<String, JsonNode>> mapApiGroups(final JsonNode openAPI, final boolean groupByTags) {
final Map<String, Map<String, JsonNode>> mapApis = new HashMap<>();
public static MultiValuedMap<String, Map<String, JsonNode>> mapApiGroups(final JsonNode openAPI, final boolean groupByTags) {
final MultiValuedMap<String, Map<String, JsonNode>> mapApis = new ArrayListValuedHashMap<>();
final var pathList = openAPI.findValue(PATHS).fields();
if (pathList.hasNext()) {
mapApis.putAll(groupByTags ? mapApiGroupsByTags(pathList) : mapApiGroupsByUrl(openAPI));
Expand All @@ -54,31 +54,21 @@ public static Map<String, Map<String, JsonNode>> mapApiGroups(final JsonNode ope
return mapApis;
}

private static Map<String, Map<String, JsonNode>> mapApiGroupsByTags(final Iterator<Entry<String, JsonNode>> pathList) {
private static MultiValuedMap<String, Map<String, JsonNode>> mapApiGroupsByTags(final Iterator<Entry<String, JsonNode>> pathList) {

final Map<String, Map<String, JsonNode>> mapApis = new HashMap<>();
final MultiValuedMap<String, Map<String, JsonNode>> mapApis = new ArrayListValuedHashMap<>();
while (pathList.hasNext()) {
final Entry<String, JsonNode> openAPIPath = pathList.next();
final var mapMethodsByTag = getMapMethodsByTag(openAPIPath);
for (Entry<String, Map<String, JsonNode>> tagMethodEntry : mapMethodsByTag.entries()) {
mapApis.compute(tagMethodEntry.getKey(), (key, value) -> initOrInsert(tagMethodEntry.getValue(), value));
mapApis.put(tagMethodEntry.getKey(), tagMethodEntry.getValue());
}
}

return mapApis;

}

private static Map<String, JsonNode> initOrInsert(final Map<String, JsonNode> mapPathMethod, final Map<String, JsonNode> value) {
var newValue = value;
if (Objects.isNull(newValue)) {
newValue = new HashMap<>();
}
newValue.putAll(mapPathMethod);

return newValue;
}


private static MultiValuedMap<String, Map<String, JsonNode>> getMapMethodsByTag(final Entry<String, JsonNode> pathItem) {
final MultiValuedMap<String, Map<String, JsonNode>> mapByTag = new ArrayListValuedHashMap<>();
final var operations = IteratorUtils.filteredIterator(pathItem.getValue().fields(), opProperty -> REST_VERB_SET.contains(opProperty.getKey()));
Expand All @@ -92,14 +82,13 @@ private static MultiValuedMap<String, Map<String, JsonNode>> getMapMethodsByTag(
return mapByTag;
}

private static Map<String, Map<String, JsonNode>> mapApiGroupsByUrl(final JsonNode openAPI) {
final var mapByUrl = new HashMap<String, Map<String, JsonNode>>();
private static MultiValuedMap<String, Map<String, JsonNode>> mapApiGroupsByUrl(final JsonNode openAPI) {
final var mapByUrl = new ArrayListValuedHashMap<String, Map<String, JsonNode>>();

for (Iterator<String> it = openAPI.get(PATHS).fieldNames(); it.hasNext();) {
final var pathUrl = it.next();
final String[] pathName = pathUrl.split("/");
mapByUrl.putIfAbsent(pathName[1], new HashMap<>());
mapByUrl.get(pathName[1]).put(pathUrl, openAPI.get(PATHS).get(pathUrl));
mapByUrl.put(pathName[1], Map.of(pathUrl, openAPI.get(PATHS).get(pathUrl)));
}

return mapByUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public interface ${className?cap_first}Api {
/**
* ${operation.operationType} ${path.pathName}<#if operation.summary?has_content>: ${operation.summary}</#if><#if operation.responseObjects?has_content>
<#if operation.parameterObjects?has_content>
*<#list operation.parameterObjects as parameter> @param ${parameter.name} ${parameter.description} ${parameter.required?c}</#list>
*<#list operation.parameterObjects as parameter> @param ${parameter.name} <#if parameter.description?has_content>${parameter.description} </#if>${parameter.required?c}</#list>
</#if>
<#if path.parameterObjects?has_content>
*<#list path.parameterObjects as parameter> @param ${parameter.name} ${parameter.description} ${parameter.required?c}</#list>
*<#list path.parameterObjects as parameter> @param ${parameter.name} <#if parameter.description?has_content>${parameter.description} </#if>${parameter.required?c}</#list>
</#if>
<#if operation.requestObjects?has_content>
*<#list operation.requestObjects as request><#list request.contentObjects as content> @param ${content.dataType?api.getVariableNameString()}<#if content?has_next>, </#if></#list>${request.description! ""}<#if request.required == true> (required)</#if></#list>
Expand All @@ -86,8 +86,8 @@ public interface ${className?cap_first}Api {

default ResponseEntity<@compress single_line=true><#if operation.responseObjects[0].contentObjects[0]??>
<${operation.responseObjects[0].contentObjects[0].dataType}<#else><Void</#if>></@compress> ${operation.operationId}(<@compress single_line=true>
<#if operation.parameterObjects?has_content><#list operation.parameterObjects as parameter> @Parameter(name = "${parameter.name}", description = "${parameter.description}", required = ${parameter.required?c}, schema = @Schema(description = "")) <#if parameter.in == "path"> @PathVariable("${parameter.name}") <#elseif parameter.in == "query"> @RequestParam(required = ${parameter.required?c}) </#if> ${parameter.dataType} ${parameter.name} <#if parameter?has_next || operation.requestObjects?has_content>, </#if></#list></#if>
<#if path.parameterObjects?has_content><#list path.parameterObjects as parameter> @Parameter(name = "${parameter.name}", description = "${parameter.description}", required = ${parameter.required?c}, schema = @Schema(description = "")) <#if parameter.in == "path"> @PathVariable("${parameter.name}") <#elseif parameter.in == "query"> @RequestParam(required = ${parameter.required?c}) </#if> ${parameter.dataType} ${parameter.name} <#if parameter?has_next || operation.requestObjects?has_content>, </#if></#list></#if>
<#if operation.parameterObjects?has_content><#list operation.parameterObjects as parameter> @Parameter(name = "${parameter.name}", <#if parameter.description?has_content>description = "${parameter.description}", </#if>required = ${parameter.required?c}, schema = @Schema(description = "")) <#if parameter.in == "path"> @PathVariable("${parameter.name}") <#elseif parameter.in == "query"> @RequestParam(required = ${parameter.required?c}) </#if> ${parameter.dataType} ${parameter.name} <#if parameter?has_next || operation.requestObjects?has_content>, </#if></#list></#if>
<#if path.parameterObjects?has_content><#list path.parameterObjects as parameter> @Parameter(name = "${parameter.name}", <#if parameter.description?has_content>description = "${parameter.description}", </#if>required = ${parameter.required?c}, schema = @Schema(description = "")) <#if parameter.in == "path"> @PathVariable("${parameter.name}") <#elseif parameter.in == "query"> @RequestParam(required = ${parameter.required?c}) </#if> ${parameter.dataType} ${parameter.name} <#if parameter?has_next || operation.requestObjects?has_content>, </#if></#list></#if>
<#if operation.requestObjects?has_content><#list operation.requestObjects as request> @Parameter(name = "${request.contentObjects[0].dataType?api.getVariableNameString()}", description = "${request.description! ""}", required = ${request.required?c}, schema = @Schema(description = "${request.contentObjects[0].description! ""}")) @Valid @RequestBody
${request.contentObjects[0].dataType} ${request.contentObjects[0].dataType?api.getVariableNameString()} <#if request?has_next>, </#if></#list></#if></@compress>) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class OpenApiGeneratorJakartaTest {

@TempDir
@TempDir(cleanup = CleanupMode.NEVER)
static Path baseDir;

private static OpenApiGenerator openApiGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,57 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import com.sngular.multifileplugin.lombok.testapi.model.ApiTestInfoDTO;
import com.sngular.multifileplugin.lombok.testapi.model.ApiErrorDTO;
import com.sngular.multifileplugin.lombok.testapi.model.ApiTestDTO;
import com.sngular.multifileplugin.lombok.testapi.model.ApiErrorDTO;
import com.sngular.multifileplugin.lombok.testapi.model.ApiTestInfoDTO;

public interface TestApi {

/**
* GET /test/{testId}: Info for a specific test
* @param testId The id of the test to retrieve true
* @return Expected response to a valid request; (status code 200)
* GET /test: List all available test
* @return A paged array of tests; (status code 200)
*/

@Operation(
operationId = "showTestById",
summary = "Info for a specific test",
operationId = "listTest",
summary = "List all available test",
tags = {"test"},
responses = {
@ApiResponse(responseCode = "200", description = "Expected response to a valid request", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestInfoDTO.class))),
@ApiResponse(responseCode = "200", description = "A paged array of tests", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestDTO.class))),
@ApiResponse(responseCode = "default", description = "unexpected error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiErrorDTO.class)))
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/test/{testId}",
value = "/test",
produces = {"application/json"}
)

default ResponseEntity<ApiTestInfoDTO> showTestById(@Parameter(name = "testId", description = "The id of the test to retrieve", required = true, schema = @Schema(description = "")) @PathVariable("testId") Integer testId) {
default ResponseEntity<ApiTestDTO> listTest() {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
/**
* GET /test: List all available test
* @return A paged array of tests; (status code 200)
* GET /test/{testId}: Info for a specific test
* @param testId The id of the test to retrieve true
* @return Expected response to a valid request; (status code 200)
*/

@Operation(
operationId = "listTest",
summary = "List all available test",
operationId = "showTestById",
summary = "Info for a specific test",
tags = {"test"},
responses = {
@ApiResponse(responseCode = "200", description = "A paged array of tests", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestDTO.class))),
@ApiResponse(responseCode = "200", description = "Expected response to a valid request", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestInfoDTO.class))),
@ApiResponse(responseCode = "default", description = "unexpected error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiErrorDTO.class)))
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/test",
value = "/test/{testId}",
produces = {"application/json"}
)

default ResponseEntity<ApiTestDTO> listTest() {
default ResponseEntity<ApiTestInfoDTO> showTestById(@Parameter(name = "testId", description = "The id of the test to retrieve", required = true, schema = @Schema(description = "")) @PathVariable("testId") Integer testId) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,57 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import com.sngular.multifileplugin.testapi.model.ApiTestInfoDTO;
import com.sngular.multifileplugin.testapi.model.ApiErrorDTO;
import com.sngular.multifileplugin.testapi.model.ApiTestDTO;
import com.sngular.multifileplugin.testapi.model.ApiErrorDTO;
import com.sngular.multifileplugin.testapi.model.ApiTestInfoDTO;

public interface TestApi {

/**
* GET /test/{testId}: Info for a specific test
* @param testId The id of the test to retrieve true
* @return Expected response to a valid request; (status code 200)
* GET /test: List all available test
* @return A paged array of tests; (status code 200)
*/

@Operation(
operationId = "showTestById",
summary = "Info for a specific test",
operationId = "listTest",
summary = "List all available test",
tags = {"test"},
responses = {
@ApiResponse(responseCode = "200", description = "Expected response to a valid request", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestInfoDTO.class))),
@ApiResponse(responseCode = "200", description = "A paged array of tests", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestDTO.class))),
@ApiResponse(responseCode = "default", description = "unexpected error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiErrorDTO.class)))
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/test/{testId}",
value = "/test",
produces = {"application/json"}
)

default ResponseEntity<ApiTestInfoDTO> showTestById(@Parameter(name = "testId", description = "The id of the test to retrieve", required = true, schema = @Schema(description = "")) @PathVariable("testId") Integer testId) {
default ResponseEntity<ApiTestDTO> listTest() {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
/**
* GET /test: List all available test
* @return A paged array of tests; (status code 200)
* GET /test/{testId}: Info for a specific test
* @param testId The id of the test to retrieve true
* @return Expected response to a valid request; (status code 200)
*/

@Operation(
operationId = "listTest",
summary = "List all available test",
operationId = "showTestById",
summary = "Info for a specific test",
tags = {"test"},
responses = {
@ApiResponse(responseCode = "200", description = "A paged array of tests", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestDTO.class))),
@ApiResponse(responseCode = "200", description = "Expected response to a valid request", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiTestInfoDTO.class))),
@ApiResponse(responseCode = "default", description = "unexpected error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ApiErrorDTO.class)))
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/test",
value = "/test/{testId}",
produces = {"application/json"}
)

default ResponseEntity<ApiTestDTO> listTest() {
default ResponseEntity<ApiTestInfoDTO> showTestById(@Parameter(name = "testId", description = "The id of the test to retrieve", required = true, schema = @Schema(description = "")) @PathVariable("testId") Integer testId) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}

Expand Down
Loading

0 comments on commit 3d23b91

Please sign in to comment.