Skip to content

Commit

Permalink
Support returning null in ParameterCustomizer/ Fixes #2822
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Dec 29, 2024
1 parent 39ef9f4 commit 915a071
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,12 @@ protected void customiseParameter(Parameter parameter, ParameterInfo parameterIn
int index = operationParameters.indexOf(parameter);
for (ParameterCustomizer parameterCustomizer : parameterCustomizerList)
parameter = parameterCustomizer.customize(parameter, parameterInfo.getMethodParameter());
if (index != -1)
operationParameters.set(index, parameter);
if (index != -1) {
if (parameter == null)
operationParameters.remove(index);
else
operationParameters.set(index, parameter);
}
}
}

Expand Down Expand Up @@ -780,18 +784,18 @@ private boolean isRequestBodyParam(RequestMethod requestMethod, ParameterInfo pa
}

/**
* Checks whether Swagger's or Spring's RequestBody annotation is present on a parameter or method
*
* Checks whether Swagger's or Spring's RequestBody annotation is present on a parameter or method
*
* @param methodParameter the method parameter
* @return the boolean
*/
*/
private boolean checkRequestBodyAnnotation(MethodParameter methodParameter) {
return methodParameter.hasParameterAnnotation(org.springframework.web.bind.annotation.RequestBody.class)
|| methodParameter.hasParameterAnnotation(io.swagger.v3.oas.annotations.parameters.RequestBody.class)
|| AnnotatedElementUtils.isAnnotated(Objects.requireNonNull(methodParameter.getParameter()), io.swagger.v3.oas.annotations.parameters.RequestBody.class)
|| AnnotatedElementUtils.isAnnotated(Objects.requireNonNull(methodParameter.getMethod()), io.swagger.v3.oas.annotations.parameters.RequestBody.class);
}

/**
* Check file boolean.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2024 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.v30.app234;

import test.org.springdoc.api.v30.app179.MyObj;

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


@RestController
public class HelloController {

@GetMapping("/test")
String test(MyObj obj) {
return obj.getContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2024 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.v30.app234;

public class MyObj {
private final String id;

private final String content;

public MyObj(String id, String content) {
this.id = id;
this.content = content;
}

public String getId() {
return id;
}

public String getContent() {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2024 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.v30.app234;

import io.swagger.v3.oas.models.parameters.Parameter;
import org.springdoc.core.customizers.ParameterCustomizer;

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;

@Component
public class MyPathParameterCustomizer implements ParameterCustomizer {
@Override
public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2024 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.v30.app234;

import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp234Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/test": {
"get": {
"tags": [
"hello-controller"
],
"operationId": "test",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {}
}
}

0 comments on commit 915a071

Please sign in to comment.