Skip to content

Commit 09ff222

Browse files
authored
fix: Inline model schema's name is wrong (#14687)
1 parent 2101ea6 commit 09ff222

1 file changed

Lines changed: 43 additions & 31 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.fasterxml.jackson.databind.ObjectMapper;
2424
import io.swagger.v3.core.util.Json;
2525
import io.swagger.v3.oas.models.*;
26+
import io.swagger.v3.oas.models.PathItem.HttpMethod;
2627
import io.swagger.v3.oas.models.callbacks.Callback;
2728
import io.swagger.v3.oas.models.media.*;
2829
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -107,52 +108,63 @@ private void flattenPaths() {
107108

108109
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
109110
PathItem path = pathsEntry.getValue();
110-
List<Operation> operations = new ArrayList<>(path.readOperations());
111+
Map<HttpMethod, Operation> operationsMap = new LinkedHashMap<>(path.readOperationsMap());
111112

112113
// use path name (e.g. /foo/bar) and HTTP verb to come up with a name
113114
// in case operationId is not defined later in other methods
114115
String pathname = pathsEntry.getKey();
115-
String name = pathname;
116-
if (path.getDelete() != null) {
117-
name = pathname + "_delete";
118-
} else if (path.getGet() != null) {
119-
name = pathname + "_get";
120-
} else if (path.getHead() != null) {
121-
name = pathname + "_head";
122-
} else if (path.getOptions() != null) {
123-
name = pathname + "_options";
124-
} else if (path.getPatch() != null) {
125-
name = pathname + "_patch";
126-
} else if (path.getPost() != null) {
127-
name = pathname + "_post";
128-
} else if (path.getPut() != null) {
129-
name = pathname + "_put";
130-
} else if (path.getTrace() != null) {
131-
name = pathname + "_trace";
132-
} else {
133-
// no HTTP verb defined?
134-
//throw new RuntimeException("No HTTP verb found/detected in the inline model resolver");
135-
}
136116

137117
// Include callback operation as well
138-
for (Operation operation : path.readOperations()) {
118+
for (Map.Entry<HttpMethod, Operation> operationEntry : new LinkedHashMap<>(path.readOperationsMap()).entrySet()) {
119+
Operation operation = operationEntry.getValue();
139120
Map<String, Callback> callbacks = operation.getCallbacks();
140121
if (callbacks != null) {
141-
operations.addAll(callbacks.values().stream()
142-
.flatMap(callback -> callback.values().stream())
143-
.flatMap(pathItem -> pathItem.readOperations().stream())
144-
.collect(Collectors.toList()));
122+
for (Map.Entry<String, Callback> callbackEntry : callbacks.entrySet()) {
123+
Callback callback = callbackEntry.getValue();
124+
for (Map.Entry<String, PathItem> pathItemEntry : callback.entrySet()) {
125+
PathItem pathItem = pathItemEntry.getValue();
126+
operationsMap.putAll(pathItem.readOperationsMap());
127+
}
128+
}
145129
}
146130
}
147131

148-
for (Operation operation : operations) {
149-
flattenRequestBody(name, operation);
150-
flattenParameters(name, operation);
151-
flattenResponses(name, operation);
132+
for (Map.Entry<HttpMethod, Operation> operationEntry : operationsMap.entrySet()) {
133+
Operation operation = operationEntry.getValue();
134+
String inlineSchemaName = this.getInlineSchemaName(operationEntry.getKey(), pathname);
135+
flattenRequestBody(inlineSchemaName, operation);
136+
flattenParameters(inlineSchemaName, operation);
137+
flattenResponses(inlineSchemaName, operation);
152138
}
153139
}
154140
}
155141

142+
private String getInlineSchemaName(HttpMethod httpVerb, String pathname) {
143+
String name = pathname;
144+
if (httpVerb.equals(HttpMethod.DELETE)) {
145+
name += "_delete";
146+
} else if (httpVerb.equals(HttpMethod.GET)) {
147+
name += "_get";
148+
} else if (httpVerb.equals(HttpMethod.HEAD)) {
149+
name += "_head";
150+
} else if (httpVerb.equals(HttpMethod.OPTIONS)) {
151+
name += "_options";
152+
} else if (httpVerb.equals(HttpMethod.PATCH)) {
153+
name += "_patch";
154+
} else if (httpVerb.equals(HttpMethod.POST)) {
155+
name += "_post";
156+
} else if (httpVerb.equals(HttpMethod.PUT)) {
157+
name += "_put";
158+
} else if (httpVerb.equals(HttpMethod.TRACE)) {
159+
name += "_trace";
160+
} else {
161+
// no HTTP verb defined?
162+
// throw new RuntimeException("No HTTP verb found/detected in the inline model
163+
// resolver");
164+
}
165+
return name;
166+
}
167+
156168
/**
157169
* Return false if model can be represented by primitives e.g. string, object
158170
* without properties, array or map of other model (model contanier), etc.

0 commit comments

Comments
 (0)