Skip to content

Commit 5cd4ce9

Browse files
committed
Refactor to shallower nesting
1 parent fe9ba65 commit 5cd4ce9

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SpringDocAnnotationsUtils.java

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -354,64 +354,62 @@ private static void setExamples(MediaType mediaType, ExampleObject[] examples) {
354354
private static MediaType getMediaType(Schema schema, Components components, JsonView jsonViewAnnotation,
355355
io.swagger.v3.oas.annotations.media.Content annotationContent, boolean openapi31) {
356356
MediaType mediaType = new MediaType();
357-
if (!annotationContent.schema().hidden()) {
358-
if (components != null) {
359-
try {
360-
getSchema(annotationContent, components, jsonViewAnnotation, openapi31).ifPresent(mediaType::setSchema);
361-
if (annotationContent.schemaProperties().length > 0) {
362-
if (mediaType.getSchema() == null) {
363-
mediaType.schema(new Schema<Object>().type("object"));
364-
}
365-
Schema oSchema = mediaType.getSchema();
366-
for (SchemaProperty sp : annotationContent.schemaProperties()) {
367-
Class<?> schemaImplementation = sp.schema().implementation();
368-
boolean isArray = false;
369-
if (schemaImplementation == Void.class) {
370-
schemaImplementation = sp.array().schema().implementation();
371-
if (schemaImplementation != Void.class) {
372-
isArray = true;
373-
}
374-
}
375-
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation, openapi31)
376-
.ifPresent(s -> {
377-
if ("array".equals(oSchema.getType())) {
378-
oSchema.getItems().addProperty(sp.name(), s);
379-
}
380-
else {
381-
oSchema.addProperty(sp.name(), s);
382-
}
383-
});
384-
357+
if (annotationContent.schema().hidden()) {
358+
return mediaType;
359+
}
360+
if (components == null) {
361+
mediaType.setSchema(schema);
362+
}
363+
try {
364+
getSchema(annotationContent, components, jsonViewAnnotation, openapi31).ifPresent(mediaType::setSchema);
365+
if (annotationContent.schemaProperties().length > 0) {
366+
if (mediaType.getSchema() == null) {
367+
mediaType.schema(new Schema<Object>().type("object"));
368+
}
369+
Schema oSchema = mediaType.getSchema();
370+
for (SchemaProperty sp : annotationContent.schemaProperties()) {
371+
Class<?> schemaImplementation = sp.schema().implementation();
372+
boolean isArray = false;
373+
if (schemaImplementation == Void.class) {
374+
schemaImplementation = sp.array().schema().implementation();
375+
if (schemaImplementation != Void.class) {
376+
isArray = true;
385377
}
386378
}
387-
if (
388-
hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) &&
389-
mediaType.getSchema() != null &&
390-
!Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) &&
391-
!Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
392-
getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation, openapi31)
393-
.ifPresent(s -> {
394-
if ("array".equals(mediaType.getSchema().getType())) {
395-
mediaType.getSchema().getItems().additionalProperties(s);
396-
}
397-
else {
398-
mediaType.getSchema().additionalProperties(s);
399-
}
400-
}
401-
);
402-
}
403-
}
404-
catch (Exception e) {
405-
if (isArray(annotationContent))
406-
mediaType.setSchema(new ArraySchema().items(new StringSchema()));
407-
else
408-
mediaType.setSchema(new StringSchema());
379+
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation, openapi31)
380+
.ifPresent(s -> {
381+
if ("array".equals(oSchema.getType())) {
382+
oSchema.getItems().addProperty(sp.name(), s);
383+
}
384+
else {
385+
oSchema.addProperty(sp.name(), s);
386+
}
387+
});
409388
}
410389
}
411-
else {
412-
mediaType.setSchema(schema);
390+
if (
391+
hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) &&
392+
mediaType.getSchema() != null &&
393+
!Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) &&
394+
!Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
395+
getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation, openapi31)
396+
.ifPresent(s -> {
397+
if ("array".equals(mediaType.getSchema().getType())) {
398+
mediaType.getSchema().getItems().additionalProperties(s);
399+
}
400+
else {
401+
mediaType.getSchema().additionalProperties(s);
402+
}
403+
}
404+
);
413405
}
414406
}
407+
catch (Exception e) {
408+
if (isArray(annotationContent))
409+
mediaType.setSchema(new ArraySchema().items(new StringSchema()));
410+
else
411+
mediaType.setSchema(new StringSchema());
412+
}
415413
return mediaType;
416414
}
417415

0 commit comments

Comments
 (0)