Skip to content

Support Schema added in OpenAPI Specification v3.1 #2345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.springdoc.core.models.GroupedOpenApi;
import org.springdoc.core.parsers.ReturnTypeParser;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion;
import org.springdoc.core.providers.ActuatorProvider;
import org.springdoc.core.providers.CloudFunctionProvider;
import org.springdoc.core.providers.JavadocProvider;
Expand Down Expand Up @@ -297,8 +298,9 @@ OpenAPIService openAPIBuilder(Optional<OpenAPI> openAPI,
*/
@Bean
@Lazy(false)
ModelConverterRegistrar modelConverterRegistrar(Optional<List<ModelConverter>> modelConverters) {
return new ModelConverterRegistrar(modelConverters.orElse(Collections.emptyList()));
ModelConverterRegistrar modelConverterRegistrar(Optional<List<ModelConverter>> modelConverters, SpringDocConfigProperties springDocConfigProperties) {
boolean openapi31 = OpenApiVersion.OPENAPI_3_1 == springDocConfigProperties.getApiDocs().getVersion();
return new ModelConverterRegistrar(modelConverters.orElse(Collections.emptyList()), openapi31);
}

/**
Expand Down Expand Up @@ -637,10 +639,11 @@ static class QuerydslProvider {
@Bean
@ConditionalOnMissingBean
@Lazy(false)
QuerydslPredicateOperationCustomizer queryDslQuerydslPredicateOperationCustomizer(Optional<QuerydslBindingsFactory> querydslBindingsFactory) {
QuerydslPredicateOperationCustomizer queryDslQuerydslPredicateOperationCustomizer(Optional<QuerydslBindingsFactory> querydslBindingsFactory, SpringDocConfigProperties springDocConfigProperties) {
if (querydslBindingsFactory.isPresent()) {
getConfig().addRequestWrapperToIgnore(Predicate.class);
return new QuerydslPredicateOperationCustomizer(querydslBindingsFactory.get());
boolean openapi31 = OpenApiVersion.OPENAPI_3_1 == springDocConfigProperties.getApiDocs().getVersion();
return new QuerydslPredicateOperationCustomizer(querydslBindingsFactory.get(), openapi31);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springdoc.core.data.DataRestTagsService;
import org.springdoc.core.discoverer.SpringDocParameterNameDiscoverer;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion;
import org.springdoc.core.providers.DataRestHalProvider;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springdoc.core.providers.SpringRepositoryRestResourceProvider;
Expand Down Expand Up @@ -232,8 +233,9 @@ DataRestTagsService dataRestTagsBuilder(OpenAPIService openAPIService) {
@Bean
@ConditionalOnMissingBean
@Lazy(false)
SpringDocDataRestUtils springDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration) {
return new SpringDocDataRestUtils(linkRelationProvider, repositoryRestConfiguration);
SpringDocDataRestUtils springDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration, SpringDocConfigProperties springDocConfigProperties) {
boolean openapi31 = OpenApiVersion.OPENAPI_3_1 == springDocConfigProperties.getApiDocs().getVersion();
return new SpringDocDataRestUtils(linkRelationProvider, repositoryRestConfiguration, openapi31);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.utils.SpringDocAnnotationsUtils;

/**
* Wrapper for model converters to only register converters once
Expand All @@ -43,7 +45,7 @@ public class ModelConverterRegistrar {
/**
* The constant modelConvertersInstance.
*/
private static final ModelConverters modelConvertersInstance = ModelConverters.getInstance();
private static ModelConverters modelConvertersInstance = null;

/**
* The constant LOGGER.
Expand All @@ -55,7 +57,9 @@ public class ModelConverterRegistrar {
*
* @param modelConverters spring registered model converter beans which have to be registered in {@link ModelConverters} instance
*/
public ModelConverterRegistrar(List<ModelConverter> modelConverters) {
public ModelConverterRegistrar(List<ModelConverter> modelConverters, boolean openapi31) {
modelConvertersInstance = ModelConverters.getInstance(openapi31);
SpringDocAnnotationsUtils.setOpenapi31(openapi31);
for (ModelConverter modelConverter : modelConverters) {
Optional<ModelConverter> registeredConverterOptional = getRegisteredConverterSameAs(modelConverter);
registeredConverterOptional.ifPresent(modelConvertersInstance::removeConverter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion;

import org.springframework.hateoas.Link;

Expand All @@ -59,7 +60,8 @@ public OpenApiHateoasLinksCustomizer(SpringDocConfigProperties springDocConfigPr

@Override
public void customise(OpenAPI openApi) {
ResolvedSchema resolvedLinkSchema = ModelConverters.getInstance()
boolean openapi31 = OpenApiVersion.OPENAPI_3_1 == springDocConfigProperties.getApiDocs().getVersion();
ResolvedSchema resolvedLinkSchema = ModelConverters.getInstance(openapi31)
.resolveAsResolvedSchema(new AnnotatedType(Link.class));
openApi
.schema("Link", resolvedLinkSchema.schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ public class QuerydslPredicateOperationCustomizer implements GlobalOperationCust
*/
private final QuerydslBindingsFactory querydslBindingsFactory;

/**
* OpenAPI v3.1
*/
private final boolean openapi31;

/**
* Instantiates a new Querydsl predicate operation customizer.
*
* @param querydslBindingsFactory the querydsl bindings factory
*/
public QuerydslPredicateOperationCustomizer(QuerydslBindingsFactory querydslBindingsFactory) {
public QuerydslPredicateOperationCustomizer(QuerydslBindingsFactory querydslBindingsFactory, boolean openapi31) {
this.querydslBindingsFactory = querydslBindingsFactory;
this.openapi31 = openapi31;
}

@Override
Expand Down Expand Up @@ -286,7 +292,7 @@ private Parameter buildParam(Type type, String name) {
schema = primitiveType.createProperty();
}
else {
ResolvedSchema resolvedSchema = ModelConverters.getInstance()
ResolvedSchema resolvedSchema = ModelConverters.getInstance(this.openapi31)
.resolveAsResolvedSchema(
new io.swagger.v3.core.converter.AnnotatedType(type).resolveAsRef(true));
// could not resolve the schema or this schema references other schema
Expand All @@ -303,4 +309,4 @@ private Parameter buildParam(Type type, String name) {
}
return parameter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public class SpringDocAnnotationsUtils extends AnnotationsUtils {
ANNOTATIONS_TO_IGNORE.add(RequestAttribute.class);
}

private static boolean OPENAPI31 = false;

/**
* Resolve schema from type schema.
*
Expand Down Expand Up @@ -118,7 +120,7 @@ public static Schema extractSchema(Components components, Type returnType, JsonV
Schema schemaN = null;
ResolvedSchema resolvedSchema;
try {
resolvedSchema = ModelConverters.getInstance()
resolvedSchema = ModelConverters.getInstance(OPENAPI31)
.resolveAsResolvedSchema(
new AnnotatedType(returnType).resolveAsRef(true).jsonViewAnnotation(jsonView).ctxAnnotations(annotations));
}
Expand Down Expand Up @@ -283,6 +285,15 @@ public static void removeAnnotationsToIgnore(Class<?>... classes) {
ANNOTATIONS_TO_IGNORE.removeAll(Arrays.asList(classes));
}

/**
* set openapi31
*
* @param openapi31
*/
public static void setOpenapi31(boolean openapi31) {
OPENAPI31 = openapi31;
}

/**
* Add encoding to media type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public class SpringDocDataRestUtils {
*/
private final LinkRelationProvider linkRelationProvider;

/**
* OpenAPI v3.1
*/
private final boolean openapi31;


/**
* The Entity ino map.
*/
Expand All @@ -103,9 +109,10 @@ public class SpringDocDataRestUtils {
* @param linkRelationProvider the link relation provider
* @param repositoryRestConfiguration the repository rest configuration
*/
public SpringDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration) {
public SpringDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration, boolean openapi31) {
this.linkRelationProvider = linkRelationProvider;
this.repositoryRestConfiguration = repositoryRestConfiguration;
this.openapi31 = openapi31;
}

/**
Expand Down Expand Up @@ -205,7 +212,7 @@ private void updateRequestBodySchema(String className, Schema schema, Components
schema.set$ref(newKey);
//create new schema
Class schemaImplementation = entityInoMap.get(className).getDomainType();
ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation));
ResolvedSchema resolvedSchema = ModelConverters.getInstance(openapi31).readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation));
Map<String, Schema> schemaMap;
if (resolvedSchema != null) {
schemaMap = resolvedSchema.referencedSchemas;
Expand Down Expand Up @@ -302,7 +309,7 @@ else if (itemsSchema instanceof ComposedSchema) {
private Schema createNewResponseSchema(String className, Components components) {
Class schemaImplementation = entityInoMap.get(className).getDomainType();
Schema schemaObject = new Schema();
ResolvedSchema resolvedSchema = ModelConverters.getInstance().readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation));
ResolvedSchema resolvedSchema = ModelConverters.getInstance(openapi31).readAllAsResolvedSchema(new AnnotatedType().type(schemaImplementation));
Map<String, Schema> schemaMap;
if (resolvedSchema != null) {
schemaMap = resolvedSchema.referencedSchemas;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* *
* * *
* * * * Copyright 2019-2023 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.app7;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExamplesController {

@GetMapping(value = "/")
public ExamplesResponse index() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.org.springdoc.api.v31.app7;

import io.swagger.v3.oas.annotations.media.Schema;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

public record ExamplesResponse(
@Schema(description = "name", requiredMode = REQUIRED, examples = { "name" })
String name,
@Schema(description = "subject", requiredMode = REQUIRED, example = "Hello", examples = { "Hello", "World" })
String subject
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* *
* * *
* * * * Copyright 2019-2023 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.app7;

import test.org.springdoc.api.v31.AbstractSpringDocV31Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp7Test extends AbstractSpringDocV31Test {

@SpringBootApplication
static class SpringDocTestApp {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/": {
"get": {
"tags": [
"examples-controller"
],
"operationId": "index",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/ExamplesResponse"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ExamplesResponse": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the schema type object is missing here.

"properties": {
"name": {
"type": "string",
"description": "name",
"examples": [
"name"
]
},
"subject": {
"type": "string",
"description": "subject",
"example":"Hello",
"examples": [
"Hello",
"World"
]
}
},
"required": [
"name",
"subject"
]
}
}
}
}