Skip to content

Commit

Permalink
Merge branch 'main' into #66-Generate-multiple-clients
Browse files Browse the repository at this point in the history
  • Loading branch information
jmejutovazquez committed Jun 21, 2022
2 parents ea86bb3 + b6147e5 commit d8a29a0
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void execute() {
}
}

private boolean isValidOperation(
final OperationParameterObject operation, final String operationId, final JsonNode channel, final String channelType, final boolean excludingOperationExists) {
private boolean isValidOperation(final OperationParameterObject operation, final String operationId,
final JsonNode channel, final String channelType, final boolean excludingOperationExists) {
final boolean result;
if (operation != null) {
final List<String> operationIds = operation.getOperationIds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ContentObject {

private String name;

private RefNameObject refNameObject;
private String refName;

private String typeData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
Expand All @@ -34,7 +35,6 @@
import net.coru.api.generator.plugin.openapi.model.OperationObject;
import net.coru.api.generator.plugin.openapi.model.ParameterObject;
import net.coru.api.generator.plugin.openapi.model.PathObject;
import net.coru.api.generator.plugin.openapi.model.RefNameObject;
import net.coru.api.generator.plugin.openapi.model.RequestObject;
import net.coru.api.generator.plugin.openapi.model.ResponseObject;
import net.coru.api.generator.plugin.openapi.parameter.FileSpec;
Expand Down Expand Up @@ -252,8 +252,7 @@ private static List<ResponseObject> mapResponseObject(final FileSpec fileSpec, f
responseObjects.add(ResponseObject.builder()
.responseName(key)
.description(value.getDescription())
.contentObjects(
mapContentObject(fileSpec, value.getContent(), "InlineResponse" + key + operationIdWithCap, globalObject))
.contentObjects(mapContentObject(fileSpec, value.getContent(), "InlineResponse" + key + operationIdWithCap, globalObject))
.build());
});
}
Expand All @@ -263,42 +262,34 @@ private static List<ResponseObject> mapResponseObject(final FileSpec fileSpec, f
private static List<ContentObject> mapContentObject(final FileSpec fileSpec, final Content content, final String inlineObject, final GlobalObject globalObject) {
final List<ContentObject> contentObjects = new ArrayList<>();
if (Objects.nonNull(content)) {
content.forEach((key, value) -> {
if (MapUtils.isNotEmpty(value.getSchema().getProperties())) {
for (Entry<String, MediaType> mediaTypeEntry : content.entrySet()) {
if (Objects.nonNull(mediaTypeEntry.getValue().getSchema().getProperties())) {
contentObjects.add(ContentObject.builder()
.typeData(mapDataType(value.getSchema(), globalObject.getComponentsTypeMap()))
.name(key)
.importName(MapperUtil.getPojoName(inlineObject, fileSpec))
.refNameObject(mapRefNameObject(MapperUtil.getPojoName(inlineObject, fileSpec), true))
.typeData(mapDataType(mediaTypeEntry.getValue().getSchema(), globalObject.getComponentsTypeMap()))
.name(mediaTypeEntry.getKey())
.importName(getPojoName(inlineObject, fileSpec))
.refName(getPojoName(inlineObject, fileSpec))
.build());
} else if (StringUtils.isNotEmpty(value.getSchema().getType())
&& BasicTypeConstants.BASIC_OBJECT_TYPE.contains(value.getSchema().getType())) {
} else if (Objects.nonNull(mediaTypeEntry.getValue().getSchema().getType())
&& BasicTypeConstants.BASIC_OBJECT_TYPE.contains(mediaTypeEntry.getValue().getSchema().getType())) {
contentObjects.add(ContentObject.builder()
.typeData(mapDataType(value.getSchema(), globalObject.getComponentsTypeMap()))
.name(key)
.refNameObject(mapRefNameObject(defineTypeName(value.getSchema()), false))
.typeData(mapDataType(mediaTypeEntry.getValue().getSchema(), globalObject.getComponentsTypeMap()))
.name(mediaTypeEntry.getKey())
.refName(defineTypeName(mediaTypeEntry.getValue().getSchema()))
.build());
} else {
contentObjects.add(ContentObject.builder()
.typeData(mapDataType(value.getSchema(), globalObject.getComponentsTypeMap()))
.name(key)
.importName(mapRefName(value.getSchema(), globalObject.getComponentsTypeMap()))
.refNameObject(mapRefNameObject(mapRefName(value.getSchema(), globalObject.getComponentsTypeMap()), true))
.typeData(mapDataType(mediaTypeEntry.getValue().getSchema(), globalObject.getComponentsTypeMap()))
.name(mediaTypeEntry.getKey())
.importName(mapRefName(mediaTypeEntry.getValue().getSchema(), globalObject.getComponentsTypeMap()))
.refName(mapRefName(mediaTypeEntry.getValue().getSchema(), globalObject.getComponentsTypeMap()))
.build());
}
});
}
}
return contentObjects;
}

private static RefNameObject mapRefNameObject(final String refName, final Boolean checkImport) {
return RefNameObject.builder()
.refName(refName)
.checkImport(checkImport)
.build();

}

private static String defineTypeName(final Schema schema) {
String typeName = "";
switch (schema.getType()) {
Expand Down Expand Up @@ -397,4 +388,10 @@ private static List<String> getSecurityRequirementList(final List<SecurityRequir
return authSecList;
}

public static String getPojoName(final String namePojo, final FileSpec fileSpec) {
return (StringUtils.isNotBlank(fileSpec.getModelNamePrefix()) ? fileSpec.getModelNamePrefix() : "")
+ namePojo
+ (StringUtils.isNotBlank(fileSpec.getModelNameSuffix()) ? fileSpec.getModelNameSuffix() : "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public static OpenAPI getPojoFromSwagger(final FileSpec fileSpec) throws MojoExe
try {
final SwaggerParseResult result = new OpenAPIParser().readLocation(fileSpec.getInputSpec(), null, null);
openAPI = result.getOpenAPI();

} catch (final ReadContentException e) {
throw new MojoExecutionException("Code generation failed when parser the .yaml file ");
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/templates/openapi/template.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface ${className?cap_first}Api {
*<#list operation.parameterObjects as parameter> @param ${parameter.name} ${parameter.description} ${parameter.required?c}</#list>
</#if>
<#if operation.requestObjects?has_content>
*<#list operation.requestObjects as request><#list request.contentObjects as content> @param ${content.refNameObject.refName?uncap_first}<#if content?has_next>, </#if></#list>${request.description! ""}<#if request.required == true> (required)</#if></#list>
*<#list operation.requestObjects as request><#list request.contentObjects as content> @param ${content.refName?uncap_first}<#if content?has_next>, </#if></#list>${request.description! ""}<#if request.required == true> (required)</#if></#list>
</#if>
* @return<#list operation.responseObjects as response><#if response.responseName != "default"> ${response.description}; (status code ${response.responseName})</#if></#list></#if>
*/
Expand All @@ -65,7 +65,7 @@ public interface ${className?cap_first}Api {
summary = "${operation.summary}",
tags = { <#list operation.tags as tag>"${tag}"</#list> }<#if operation.responseObjects?has_content>,
responses = {
<#list operation.responseObjects as response> @ApiResponse(responseCode = "${response.responseName}", description = "${response.description}" <#if response.contentObjects?has_content>, content = @Content(<#list response.contentObjects as content>mediaType = "${content.name}" <#if content.refNameObject.refName?has_content>,schema = @Schema(implementation = ${content.refNameObject.refName?cap_first}.class))</#if> </#list></#if>)<#if !response?is_last>,</#if></#list>
<#list operation.responseObjects as response> @ApiResponse(responseCode = "${response.responseName}", description = "${response.description}" <#if response.contentObjects?has_content>, content = @Content(<#list response.contentObjects as content>mediaType = "${content.name}" <#if content.refName?has_content>,schema = @Schema(implementation = ${content.refName?cap_first}.class))</#if> </#list></#if>)<#if !response?is_last>,</#if></#list>
}</#if>
)
@RequestMapping(
Expand All @@ -75,15 +75,15 @@ public interface ${className?cap_first}Api {
)

default ResponseEntity<@compress single_line=true><<#if operation.responseObjects[0].contentObjects[0]??><#if operation.responseObjects[0].contentObjects[0].typeData?? && (operation.responseObjects[0].contentObjects[0].typeData == "array")>
List<${operation.responseObjects[0].contentObjects[0].refNameObject.refName?cap_first}>
List<${operation.responseObjects[0].contentObjects[0].refName?cap_first}>
<#elseif operation.responseObjects[0].contentObjects[0].typeData?? && (operation.responseObjects[0].contentObjects[0].typeData == "map")>
Map<String,${operation.responseObjects[0].contentObjects[0].refNameObject.refName?cap_first}>
<#else>${operation.responseObjects[0].contentObjects[0].refNameObject.refName?cap_first}</#if><#else>Void</#if>></@compress> ${operation.operationId}(<@compress single_line=true>
Map<String,${operation.responseObjects[0].contentObjects[0].refName?cap_first}>
<#else>${operation.responseObjects[0].contentObjects[0].refName?cap_first}</#if><#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 </#if> ${parameter.className?cap_first} ${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].refNameObject.refName}", description = "${request.description! ""}", required = ${request.required?c} , schema = @Schema(description = "${request.contentObjects[0].description! ""}")) @Valid @RequestBody
<#if request.contentObjects[0].typeData?? && (request.contentObjects[0].typeData == "array")> List<${request.contentObjects[0].refNameObject.refName?cap_first}>
<#elseif request.contentObjects[0].typeData?? && (request.contentObjects[0].typeData == "map")> Map<String,${request.contentObjects[0].refNameObject.refName?cap_first}>
<#else> ${request.contentObjects[0].refNameObject.refName?cap_first} </#if> ${request.contentObjects[0].refNameObject.refName?uncap_first} <#if request?has_next> , </#if></#list></#if></@compress>) {
<#if operation.requestObjects?has_content><#list operation.requestObjects as request> @Parameter(name = "${request.contentObjects[0].refName}", description = "${request.description! ""}", required = ${request.required?c} , schema = @Schema(description = "${request.contentObjects[0].description! ""}")) @Valid @RequestBody
<#if request.contentObjects[0].typeData?? && (request.contentObjects[0].typeData == "array")> List<${request.contentObjects[0].refName?cap_first}>
<#elseif request.contentObjects[0].typeData?? && (request.contentObjects[0].typeData == "map")> Map<String,${request.contentObjects[0].refName?cap_first}>
<#else> ${request.contentObjects[0].refName?cap_first} </#if> ${request.contentObjects[0].refName?uncap_first} <#if request?has_next> , </#if></#list></#if></@compress>) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
</#list>
Expand Down
Loading

0 comments on commit d8a29a0

Please sign in to comment.