Skip to content

Commit

Permalink
Fix ability to generating by multiple swagger files (#1738)
Browse files Browse the repository at this point in the history
Fixed #1734
  • Loading branch information
altro3 authored Sep 5, 2024
1 parent ce296f8 commit 6b8209a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ private static void withPath(File file, Consumer<? super String> action) {
* Performs code generation.
*/
public void generate() {
var opts = new ParseOptions();
opts.setResolve(true);
var openAPI = new OpenAPIParser()
.readLocation(definitionFile.toString(), null, new ParseOptions()).getOpenAPI();
.readLocation(definitionFile.toString(), null, opts).getOpenAPI();

// Configure codegen
withPath(outputDirectory, codeGenerator::setOutputDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void flattenPaths() {

for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
PathItem path = pathsEntry.getValue();
if (path.get$ref() != null) {
if (path.get$ref() != null && path.get$ref().startsWith("#/components/pathItems/")) {
path = openAPI.getComponents().getPathItems().get(path.get$ref().substring("#/components/pathItems/".length()));
}
var operationsMap = new LinkedHashMap<>(path.readOperationsMap());
Expand Down Expand Up @@ -183,7 +183,7 @@ private void flattenResponses(String modelName, Operation operation) {
String key = responsesEntry.getKey();
ApiResponse response = responsesEntry.getValue();

if (response.get$ref() != null) {
if (response.get$ref() != null && response.get$ref().startsWith("#/components/responses/")) {
response = openAPI.getComponents().getResponses().get(response.get$ref().substring("#/components/responses/".length()));
}

Expand Down Expand Up @@ -261,7 +261,7 @@ private void flattenParameters(String modelName, Operation operation) {
continue;
}

if (parameter.get$ref() != null) {
if (parameter.get$ref() != null && parameter.get$ref().startsWith("#/components/parameters/")) {
if (openAPI.getComponents().getParameters() != null) {
parameter = openAPI.getComponents().getParameters().get(parameter.get$ref().substring("#/components/parameters/".length()));
}
Expand Down Expand Up @@ -312,7 +312,7 @@ private void flattenPathItemParameters(String modelName, Operation operation, Pa
}

Schema parameterSchema = null;
if (parameter.get$ref() != null) {
if (parameter.get$ref() != null && parameter.get$ref().startsWith("#/components/parameters/")) {
if (openAPI.getComponents().getParameters() != null) {
var param = openAPI.getComponents().getParameters().get(parameter.get$ref().substring("#/components/parameters/".length()));
parameterSchema = param.getSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,45 @@ void testMultipartFormData() {
);
""");
}

@Test
void testGenerateByMultipleFiles() {

var codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/multiple/swagger.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/CustomerApi.java",
"""
@Post("/api/customer/{id}/files")
Mono<HttpResponse<@NotNull String>> uploadFile(
@PathVariable("id") @NotNull UUID id,
@Body @NotNull @Valid FileCreateDto fileCreateDto
);
""");
assertFileContains(path + "model/FileCreateDto.java",
"""
public class FileCreateDto {
public static final String JSON_PROPERTY_TYPE_CODE = "typeCode";
public static final String JSON_PROPERTY_ORG_NAME = "orgName";
/**
* Customer type ORG
*/
@NotNull
@Pattern(regexp = "^ORG$")
@JsonProperty(JSON_PROPERTY_TYPE_CODE)
private String typeCode = "ORG";
@NotNull
@JsonProperty(JSON_PROPERTY_ORG_NAME)
private String orgName;
public FileCreateDto(String typeCode, String orgName) {
this.typeCode = typeCode;
this.orgName = orgName;
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,33 @@ fun profilePasswordPost(
): Mono<SuccessResetPassword>
""");
}

@Test
void testGenerateByMultipleFiles() {

var codegen = new KotlinMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/multiple/swagger.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "api/CustomerApi.kt",
"""
@Post("/api/customer/{id}/files")
fun uploadFile(
@PathVariable("id") @NotNull id: UUID,
@Body @NotNull @Valid fileCreateDto: FileCreateDto
): Mono<HttpResponse<String>>
""");
assertFileContains(path + "model/FileCreateDto.kt",
"""
data class FileCreateDto(
@field:NotNull
@field:Pattern(regexp = "^ORG$")
@field:JsonProperty(JSON_PROPERTY_TYPE_CODE)
var typeCode: String = "ORG",
@field:NotNull
@field:JsonProperty(JSON_PROPERTY_ORG_NAME)
var orgName: String,
) {
""");
}
}
28 changes: 28 additions & 0 deletions openapi-generator/src/test/resources/3_0/multiple/paths/files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
post:
tags:
- Customer
operationId: uploadFile
summary: Upload customer files
description: Save file in customer file repository
parameters:
- name: id
in: path
required: true
description: Unique identifier of the Customer
schema:
type: string
format: uuid
requestBody:
required: true
content:
application/json:
schema:
$ref: '../schemas/files.yml#/FileCreateDto'

responses:
201:
description: File uploaded correctly
content:
application/json:
schema:
$ref: '../schemas/files.yml#/FileId'
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FileCreateDto:
type: object
required:
- typeCode
- orgName
description: Organization customer data
properties:
typeCode:
type: string
pattern: ^ORG$
description: Customer type ORG
default: ORG
example: ORG
orgName:
type: string

FileId:
type: string
8 changes: 8 additions & 0 deletions openapi-generator/src/test/resources/3_0/multiple/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Sample
description: Multi-file for OpenAPI Specification.
paths:
/api/customer/{id}/files:
$ref: "./paths/files.yml"

0 comments on commit 6b8209a

Please sign in to comment.