Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<central-publishing-maven-plugin.version>0.7.0
</central-publishing-maven-plugin.version>
<flatten-maven-plugin.version>1.5.0</flatten-maven-plugin.version>
<swagger-api.version>2.2.43</swagger-api.version>
<swagger-api.version>2.2.45</swagger-api.version>
<swagger-ui.version>5.32.0</swagger-ui.version>
<gmavenplus-plugin.version>1.13.1</gmavenplus-plugin.version>
<jjwt.version>0.9.1</jjwt.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.v3.core.jackson.SwaggerAnnotationIntrospector;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.commons.lang3.StringUtils;

import static org.springdoc.core.utils.SpringDocAnnotationsUtils.hasADefaultValue;

/**
* The type Spring doc required module.
Expand All @@ -57,7 +58,7 @@ public Boolean hasRequiredMarker(AnnotatedMember annotatedMember) {
if (schemaAnnotation.required() || requiredMode == Schema.RequiredMode.REQUIRED) {
return true;
}
else if (requiredMode == Schema.RequiredMode.NOT_REQUIRED || StringUtils.isNotEmpty(schemaAnnotation.defaultValue())) {
else if (requiredMode == Schema.RequiredMode.NOT_REQUIRED || hasADefaultValue(schemaAnnotation)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ private String getDescription(String parameterName, String originalDescription)
* @return the default value
*/
private String getDefaultValue(String parameterName, PageableDefault pageableDefault, String defaultSchemaVal) {
String defaultValue = null;
String defaultValue = defaultSchemaVal;
switch (parameterName) {
case "size":
if (pageableDefault != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public class Builder {
/**
* Provides a default value.
*/
private String defaultValue = "";
private String defaultValue = Schema.DEFAULT_SENTINEL;

/**
* Provides a discriminator property value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.lang.annotation.Annotation;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityRequirementEntry;

/**
* The type Security requirement builder.
Expand All @@ -47,6 +48,12 @@ public class Builder {
*/
private String[] scopes = {};

/**
* If multiple requirements apply at the same time, use this value instead of {@link #name} and {@link #scopes}.
* If any one of multiple security schemes is required, use multiple {@link SecurityRequirement} annotations instead.
* <p>Exactly one of this and {@link #name} must be set.</p>
*/
private SecurityRequirementEntry[] securityRequirementEntries = {};

/**
* Instantiates a new Security requirement builder.
Expand Down Expand Up @@ -85,6 +92,17 @@ public Builder scopes(String[] scopes) {
return this;
}

/**
* SecurityRequirementEntries security requirement builder.
*
* @param securityRequirementEntries the securityRequirementEntries
* @return the security requirement builder
*/
public Builder securityRequirementEntries(SecurityRequirementEntry[] securityRequirementEntries) {
this.securityRequirementEntries = securityRequirementEntries;
return this;
}

/**
* Build security requirement.
*
Expand All @@ -106,6 +124,11 @@ public String name() {
public String[] scopes() {
return scopes;
}

@Override
public SecurityRequirementEntry[] combine() {
return securityRequirementEntries;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.Principal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -639,21 +627,36 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo, null,
jsonView);
if (parameterInfo.getDefaultValue() != null && schema != null) {
Object defaultValue = parameterInfo.getDefaultValue();
// Cast default value
PrimitiveType primitiveType = PrimitiveType.fromTypeAndFormat(schema.getType(), schema.getFormat());
if (primitiveType != null) {
Schema<?> primitiveSchema = primitiveType.createProperty();
primitiveSchema.setDefault(parameterInfo.getDefaultValue());
defaultValue = primitiveSchema.getDefault();
}
Object defaultValue = castDefaultValue(primitiveType, parameterInfo.getDefaultValue());
schema.setDefault(defaultValue);
}
parameter.setSchema(schema);
}
return parameter;
}

/**
* Cast the default value so that it matches the {@link PrimitiveType}
*
* @param primitiveType the primitive type
* @param defaultValue the default value
* @return the cast default value
*/
private Object castDefaultValue(PrimitiveType primitiveType, Object defaultValue) {
if (primitiveType != null) {
Schema<?> primitiveSchema = primitiveType.createProperty();
if (primitiveType.equals(PrimitiveType.DATE) && defaultValue instanceof LocalDate localDate) {
defaultValue = localDate.toString();
}
primitiveSchema.setDefault(defaultValue);
if (primitiveSchema.getDefault() != null) {
defaultValue = primitiveSchema.getDefault();
}
}
return defaultValue;
}

/**
* Apply bean validator annotations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private void setSchema(io.swagger.v3.oas.annotations.Parameter parameterDoc, Com
if (schema == null && parameterDoc.array() != null) {
schema = AnnotationsUtils.getSchema(parameterDoc.schema(), parameterDoc.array(), true, parameterDoc.array().schema().implementation(), components, jsonView, propertyResolverUtils.isOpenapi31()).orElse(null);
// default value not set by swagger-core for array !
if (schema != null) {
if (schema != null && SpringDocAnnotationsUtils.hasADefaultValue(parameterDoc.array().arraySchema())) {
Object defaultValue = SpringDocAnnotationsUtils.resolveDefaultValue(parameterDoc.array().arraySchema().defaultValue(), objectMapperProvider.jsonMapper());
schema.setDefault(defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.jackson.TypeNameResolver;
import io.swagger.v3.core.util.AnnotationsUtils;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Json31;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Webhook;
import io.swagger.v3.oas.annotations.Webhooks;
import io.swagger.v3.oas.annotations.security.SecuritySchemes;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
Expand All @@ -76,6 +75,7 @@
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.providers.JavadocProvider;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springdoc.core.utils.PropertyResolverUtils;

import org.springframework.beans.BeansException;
Expand Down Expand Up @@ -247,7 +247,7 @@ public OpenAPI build(Locale locale) {
calculatedOpenAPI.setPaths(new Paths());
}
else {
calculatedOpenAPI = cloneViaJson(openAPI, OpenAPI.class, new ObjectMapper());
calculatedOpenAPI = cloneViaJson(openAPI, OpenAPI.class, ObjectMapperProvider.createJson(springDocConfigProperties));
}

if (apiDef.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,16 @@ private static boolean isArray(io.swagger.v3.oas.annotations.media.Content annot
return isArray;
}

/**
* Whether the schema has defined a default value.
*
* @param schema The schema annotation
* @return Whether the schema annotation has defined a default value
*/
public static boolean hasADefaultValue(io.swagger.v3.oas.annotations.media.Schema schema) {
return schema != null && !schema.defaultValue().equals(io.swagger.v3.oas.annotations.media.Schema.DEFAULT_SENTINEL);
}

/**
* Resolve default value object.
*
Expand Down Expand Up @@ -519,7 +529,7 @@ public static Optional<Map<String, Header>> getHeaders(io.swagger.v3.oas.annotat
public static void clearCache(JavadocProvider javadocProvider) {
if (javadocProvider != null)
javadocProvider.clearCache();
MODEL_CONVERTER_CONTEXT_MAP.remove();;
MODEL_CONVERTER_CONTEXT_MAP.remove();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"in": "query",
"required": false,
"schema": {
"type": "string"
"type": "string",
"nullable": true
}
},
{
Expand Down Expand Up @@ -86,7 +87,8 @@
"in": "query",
"required": false,
"schema": {
"type": "string"
"type": "string",
"nullable": true
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"in": "query",
"required": false,
"schema": {
"type": "string"
"type" : [ "string", "null" ]
}
},
{
Expand Down Expand Up @@ -86,7 +86,7 @@
"in": "query",
"required": false,
"schema": {
"type": "string"
"type" : [ "string", "null" ]
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ static class SpringDocTestApp {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info().extensions(Map.of("TEST", "HELLO")))
.extensions(Map.of("TEST", "HELLO"));
.info(new Info().extensions(Map.of("x-TEST", "HELLO")))
.extensions(Map.of("x-TEST", "HELLO"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ static class SpringDocTestApp {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info().extensions(Map.of("TEST", "HELLO")))
.extensions(Map.of("TEST", "HELLO"));
.info(new Info().extensions(Map.of("x-TEST", "HELLO")))
.extensions(Map.of("x-TEST", "HELLO"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2026 the original author or authors.
* * * * *
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * you may not use this file except in compliance with the License.
* * * * * You may obtain a copy of the License at
* * * * *
* * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * *
* * * * * Unless required by applicable law or agreed to in writing, software
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * See the License for the specific language governing permissions and
* * * * * limitations under the License.
* * * *
* * *
* *
*
*/
package test.org.springdoc.api.v31.app245;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.info.Info;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.fasterxml.jackson.annotation.JsonView;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@OpenAPIDefinition(
info = @Info(
title = "My App",
version = "${springdoc.version}",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = "x-build-time", value = "${git.build.time}")
})
}
)
)
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
public class HelloController {

@GetMapping("/hello")
public String hello() {
return "hello";
}
@JsonView(Views.Hello.class)
@GetMapping(value = "/hello-paged", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Page<HelloWorld>> getHelloWorldPaged(@ParameterObject Pageable pageable) {
return ResponseEntity.ok(new PageImpl<>(List.of(new HelloWorld("hello", "world")), pageable, 1));
}
}
Loading
Loading