Skip to content

Commit d3398c8

Browse files
authored
fix: Inlining of references with dot (#2109) (#2384)
1 parent 0255d18 commit d3398c8

2 files changed

Lines changed: 317 additions & 50 deletions

File tree

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
import java.util.List;
66
import java.util.Map;
77

8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
10-
118
import io.swagger.v3.core.util.Json;
129
import io.swagger.v3.oas.models.Components;
1310
import io.swagger.v3.oas.models.OpenAPI;
@@ -27,7 +24,6 @@
2724
@SuppressWarnings("rawtypes")
2825
public class InlineModelResolver {
2926
private OpenAPI openAPI;
30-
static final Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);
3127

3228
Map<String, Schema> addedModels = new HashMap<>();
3329
Map<String, String> generatedSignature = new HashMap<>();
@@ -103,14 +99,14 @@ private void flattenBody(String pathname, RequestBody body)
10399
if (model.getProperties() != null && model.getProperties().size() > 0) {
104100
flattenProperties(model.getProperties(), pathname);
105101
String modelName = resolveModelName(model.getTitle(), genericName);
106-
mediaType.setSchema(new Schema().$ref(modelName));
102+
mediaType.setSchema(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
107103
addGenerated(modelName, model);
108104
openAPI.getComponents().addSchemas(modelName, model);
109105
} else if (model instanceof ComposedSchema) {
110106
flattenComposedSchema(model, pathname);
111107
if (model.get$ref() == null) {
112108
String modelName = resolveModelName(model.getTitle(), genericName);
113-
mediaType.setSchema(this.makeRefProperty(modelName, model));
109+
mediaType.setSchema(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, model));
114110
addGenerated(modelName, model);
115111
openAPI.getComponents().addSchemas(modelName, model);
116112
}
@@ -123,17 +119,17 @@ private void flattenBody(String pathname, RequestBody body)
123119
String modelName = resolveModelName(inner.getTitle(), genericName);
124120
String existing = matchGenerated(inner);
125121
if (existing != null) {
126-
am.setItems(new Schema().$ref(existing));
122+
am.setItems(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + existing));
127123
} else {
128-
am.setItems(new Schema().$ref(modelName));
124+
am.setItems(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
129125
addGenerated(modelName, inner);
130126
openAPI.getComponents().addSchemas(modelName, inner);
131127
}
132128
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
133129
flattenComposedSchema(inner,key);
134130
if (inner.get$ref() == null) {
135131
String modelName = resolveModelName(inner.getTitle(), "inline_body_items_" + key + "_" + pathname);
136-
am.setItems(this.makeRefProperty(modelName, inner));
132+
am.setItems(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, inner));
137133
addGenerated(modelName, inner);
138134
openAPI.getComponents().addSchemas(modelName, inner);
139135
}
@@ -158,14 +154,14 @@ private void flattenParams(String pathname, List<Parameter> parameters)
158154
if (model.getProperties() != null && model.getProperties().size() > 0) {
159155
flattenProperties(model.getProperties(), pathname);
160156
String modelName = resolveModelName(model.getTitle(), parameter.getName());
161-
parameter.setSchema(new Schema().$ref(modelName));
157+
parameter.setSchema(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
162158
addGenerated(modelName, model);
163159
openAPI.getComponents().addSchemas(modelName, model);
164160
}
165161
}
166162
}else if (model instanceof ComposedSchema) {
167163
String modelName = resolveModelName(model.getTitle(), parameter.getName());
168-
parameter.setSchema(new Schema().$ref(modelName));
164+
parameter.setSchema(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
169165
addGenerated(modelName, model);
170166
openAPI.getComponents().addSchemas(modelName, model);
171167
}else if (model instanceof ArraySchema) {
@@ -177,17 +173,17 @@ private void flattenParams(String pathname, List<Parameter> parameters)
177173
String modelName = resolveModelName(inner.getTitle(), parameter.getName());
178174
String existing = matchGenerated(inner);
179175
if (existing != null) {
180-
am.setItems(new Schema().$ref(existing));
176+
am.setItems(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + existing));
181177
} else {
182-
am.setItems(new Schema().$ref(modelName));
178+
am.setItems(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
183179
addGenerated(modelName, am);
184180
openAPI.getComponents().addSchemas(modelName, am);
185181
}
186182
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
187183
flattenComposedSchema(inner, parameter.getName());
188184
if (inner.get$ref() == null) {
189185
String modelName = resolveModelName(inner.getTitle(), "inline_parameter_items_" + parameter.getName());
190-
am.setItems(this.makeRefProperty(modelName, inner));
186+
am.setItems(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, inner));
191187
addGenerated(modelName, inner);
192188
openAPI.getComponents().addSchemas(modelName, inner);
193189
}
@@ -217,9 +213,9 @@ private void flattenResponses(String pathname, Map<String, ApiResponse> response
217213
String modelName = resolveModelName(mediaSchema.getTitle(), "inline_response_" + key);
218214
String existing = matchGenerated(mediaSchema);
219215
if (existing != null) {
220-
media.setSchema(this.makeRefProperty(existing, mediaSchema));
216+
media.setSchema(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + existing, mediaSchema));
221217
} else {
222-
media.setSchema(this.makeRefProperty(modelName, mediaSchema));
218+
media.setSchema(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, mediaSchema));
223219
addGenerated(modelName, mediaSchema);
224220
openAPI.getComponents().addSchemas(modelName, mediaSchema);
225221
}
@@ -279,15 +275,15 @@ private void flattenDefinitions(Map<String, Schema> models)
279275
if (existing == null) {
280276
openAPI.getComponents().addSchemas(innerModelName, inner);
281277
addGenerated(innerModelName, inner);
282-
m.setItems(new Schema().$ref(innerModelName));
278+
m.setItems(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + innerModelName));
283279
} else {
284-
m.setItems(new Schema().$ref(existing));
280+
m.setItems(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + existing));
285281
}
286282
}else if (inner instanceof ComposedSchema && this.flattenComposedSchemas){
287283
flattenComposedSchema(inner,modelName);
288284
if (inner.get$ref() == null) {
289285
modelName = resolveModelName(inner.getTitle(), "inline_array_items_" + modelName);
290-
m.setItems(this.makeRefProperty(modelName, inner));
286+
m.setItems(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, inner));
291287
addGenerated(modelName, inner);
292288
openAPI.getComponents().addSchemas(modelName, inner);
293289
}
@@ -317,7 +313,7 @@ private void flattenDefinitions(Map<String, Schema> models)
317313
if (this.flattenComposedSchemas) {
318314
int position = i+1;
319315
inlineModelName = resolveModelName(inline.getTitle(), modelName + inlineModelName + "_" + position);
320-
list.set(i,new Schema().$ref(inlineModelName));
316+
list.set(i,new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + inlineModelName));
321317
addGenerated(inlineModelName, inline);
322318
openAPI.getComponents().addSchemas(inlineModelName, inline);
323319
}
@@ -334,9 +330,9 @@ private void flattenArraySchema(Schema inner, String key, String pathname, Array
334330
String modelName = resolveModelName(inner.getTitle(), key);
335331
String existing = matchGenerated(inner);
336332
if (existing != null) {
337-
ap.setItems(this.makeRefProperty(existing, inner));
333+
ap.setItems(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + existing, inner));
338334
} else {
339-
ap.setItems(this.makeRefProperty(modelName, inner));
335+
ap.setItems(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, inner));
340336
addGenerated(modelName, inner);
341337
openAPI.getComponents().addSchemas(modelName, inner);
342338
}
@@ -345,7 +341,7 @@ private void flattenArraySchema(Schema inner, String key, String pathname, Array
345341
if (inner.get$ref() == null) {
346342
key = "inline_response_items" + key;
347343
String modelName = resolveModelName(inner.getTitle(), key );
348-
ap.setItems(this.makeRefProperty(modelName, inner));
344+
ap.setItems(this.makeRefProperty(Components.COMPONENTS_SCHEMAS_REF + modelName, inner));
349345
addGenerated(modelName, inner);
350346
openAPI.getComponents().addSchemas(modelName, inner);
351347
}
@@ -359,17 +355,17 @@ private void flattenMapSchema(Schema innerProperty, String key, String pathname,
359355
String modelName = resolveModelName(innerProperty.getTitle(), key);
360356
String existing = matchGenerated(innerProperty);
361357
if (existing != null) {
362-
mediaSchema.setAdditionalProperties(new Schema().$ref(existing));
358+
mediaSchema.setAdditionalProperties(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + existing));
363359
} else {
364-
mediaSchema.setAdditionalProperties(new Schema().$ref(modelName));
360+
mediaSchema.setAdditionalProperties(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
365361
addGenerated(modelName, innerProperty);
366362
openAPI.getComponents().addSchemas(modelName, innerProperty);
367363
}
368364
}else if (innerProperty instanceof ComposedSchema && this.flattenComposedSchemas){
369365
flattenComposedSchema(innerProperty,key);
370366
if (innerProperty.get$ref() == null) {
371367
String modelName = resolveModelName(innerProperty.getTitle(), key);
372-
mediaSchema.setAdditionalProperties(new Schema().$ref(modelName));
368+
mediaSchema.setAdditionalProperties(new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + modelName));
373369
addGenerated(modelName, innerProperty);
374370
openAPI.getComponents().addSchemas(modelName, innerProperty);
375371
}
@@ -579,7 +575,7 @@ private void flattenComposedSchema(Schema inner, String key) {
579575
if (this.flattenComposedSchemas) {
580576
int position = i+1;
581577
inlineModelName = resolveModelName(inline.getTitle(), key + inlineModelName + "_" + position);
582-
list.set(i,new Schema().$ref(inlineModelName));
578+
list.set(i,new Schema().$ref(Components.COMPONENTS_SCHEMAS_REF + inlineModelName));
583579
addGenerated(inlineModelName, inline);
584580
openAPI.getComponents().addSchemas(inlineModelName, inline);
585581
}

0 commit comments

Comments
 (0)