-
-
Notifications
You must be signed in to change notification settings - Fork 598
fix: pass along type_use annotations to swagger(-core) #3300
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
Open
tthornton3-chwy
wants to merge
2
commits into
springdoc:main
Choose a base branch
from
tthornton3-chwy:bugfix/pass-along-type-use-annotations-to-swagger-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...penapi-javadoc-tests/src/test/java/test/org/springdoc/api/v30/app176/HelloController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * | ||
| * * | ||
| * * * | ||
| * * * * | ||
| * * * * * | ||
| * * * * * * 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.v30.app176; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
| import org.springdoc.core.annotations.ParameterObject; | ||
|
|
||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| class HelloController { | ||
|
|
||
| @GetMapping("/clinics/{clinicId}/vets") | ||
| @Parameter(name = "clinicId", in = ParameterIn.PATH) | ||
| public void find(@ParameterObject SearchCriteria searchCriteria) { | ||
| } | ||
|
|
||
| } |
37 changes: 37 additions & 0 deletions
37
...ngdoc-openapi-javadoc-tests/src/test/java/test/org/springdoc/api/v30/app176/Nullable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * | ||
| * * | ||
| * * * | ||
| * * * * | ||
| * * * * * | ||
| * * * * * * 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.v30.app176; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.TYPE_USE) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @interface Nullable { | ||
| } |
59 changes: 59 additions & 0 deletions
59
...openapi-javadoc-tests/src/test/java/test/org/springdoc/api/v30/app176/SearchCriteria.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * | ||
| * * | ||
| * * * | ||
| * * * * | ||
| * * * * * | ||
| * * * * * * 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.v30.app176; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Parameter; | ||
|
|
||
| /** | ||
| * A parameter object whose {@code clinicId} field is reused as both an optional, nullable | ||
| * query parameter and a (required, non-null) path parameter, depending on the controller. | ||
| */ | ||
| class SearchCriteria { | ||
|
|
||
| @Parameter(description = "Find vets affiliated with this clinic id.") | ||
| private @Nullable String clinicId; | ||
|
|
||
| @Parameter(description = "Find vets with this name.") | ||
| private @Nullable String name; | ||
|
|
||
| public String getClinicId() { | ||
| return clinicId; | ||
| } | ||
|
|
||
| public void setClinicId(String clinicId) { | ||
| this.clinicId = clinicId; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| } | ||
94 changes: 94 additions & 0 deletions
94
...pi-javadoc-tests/src/test/java/test/org/springdoc/api/v30/app176/SpringDocApp176Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * | ||
| * * | ||
| * * * | ||
| * * * * | ||
| * * * * * | ||
| * * * * * * 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.v30.app176; | ||
|
|
||
| import com.jayway.jsonpath.JsonPath; | ||
| import net.minidev.json.JSONArray; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springdoc.core.utils.Constants; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.context.TestPropertySource; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.MvcResult; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| /** | ||
| * Verifies that {@code nullable: true} (propagated from a TYPE_USE {@code @Nullable} | ||
| * annotation on a {@code @ParameterObject} field under OpenAPI 3.0) is cleared when that | ||
| * field is reused as a path parameter, while it is preserved for query parameters. | ||
| */ | ||
| @ActiveProfiles("test") | ||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| @TestPropertySource(properties = "springdoc.api-docs.version=openapi_3_0") | ||
| class SpringDocApp176Test { | ||
|
|
||
| private static final String PATH = "$.paths.['/clinics/{clinicId}/vets'].get.parameters"; | ||
|
|
||
| @Autowired | ||
| protected MockMvc mockMvc; | ||
|
|
||
| private static Object readSingle(String result, String jsonPath) { | ||
| return ((JSONArray) JsonPath.parse(result).read(jsonPath)).get(0); | ||
| } | ||
|
|
||
| @Test | ||
| void pathParameterIsNotNullableButQueryParameterIs() throws Exception { | ||
| MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)) | ||
| .andExpect(status().isOk()).andReturn(); | ||
| String result = mockMvcResult.getResponse().getContentAsString(); | ||
|
|
||
| // A path parameter is always required and can never be null. | ||
| assertThat(readSingle(result, PATH + "[?(@.name == 'clinicId')].required")) | ||
| .isEqualTo(Boolean.TRUE); | ||
| assertThat(readSingle(result, PATH + "[?(@.name == 'clinicId')].schema.type")) | ||
| .isEqualTo("string"); | ||
| assertThat((JSONArray) JsonPath.parse(result).read(PATH + "[?(@.name == 'clinicId')].schema.nullable")) | ||
| .isEmpty(); | ||
|
|
||
| // A nullable query parameter keeps nullable: true. | ||
| assertThat(readSingle(result, PATH + "[?(@.name == 'name')].required")) | ||
| .isEqualTo(Boolean.FALSE); | ||
| assertThat(readSingle(result, PATH + "[?(@.name == 'name')].schema.type")) | ||
| .isEqualTo("string"); | ||
| assertThat(readSingle(result, PATH + "[?(@.name == 'name')].schema.nullable")) | ||
| .isEqualTo(Boolean.TRUE); | ||
| } | ||
|
|
||
| @SpringBootApplication | ||
| static class SpringDocTestApp { | ||
| } | ||
|
|
||
| } |
41 changes: 41 additions & 0 deletions
41
...penapi-javadoc-tests/src/test/java/test/org/springdoc/api/v31/app175/HelloController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * | ||
| * * | ||
| * * * | ||
| * * * * | ||
| * * * * * | ||
| * * * * * * 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.app175; | ||
|
|
||
| import org.springdoc.core.annotations.ParameterObject; | ||
|
|
||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| class HelloController { | ||
|
|
||
| @GetMapping("/vets") | ||
| public void find(@ParameterObject SearchCriteria searchCriteria) { | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for wanting reuse this object in different contexts where
@Nullableis not always correct? For the case where it is used in a path-parameter context, then the annotation misleads the consumer using the object, since the object saysnullablebut it actually is required?Isn't the better approach to have an object specifically for the path scenario, so that it both documents the
nullablecorrectly inwards and outwards?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example: we follow the CQRS-pattern, and have types that get deserialized for some queries. There's an "admin" path in, something like /admin/vet which can do whatever it wants (add a resource id as a query param, not add an id, put any id it wants). But there is also a "scoped" path, think of something like /resource/{resourceId}/vet that requires it (and authenticates that it is able to access said resourceId). They do the absolute same thing though, and come back to the same type. Sure, we could have an AdminSearchCriteria vs ScopedSearchCriteria, but now we have two methods in our service and gotta map one probably back to the other so we can stay DRY.
So the case where a PathParameter exists, is valid, then its required no matter what, and this helps us do that! Swagger Core didn't seem the right place to think about this, since that's a layer deeper than it probably should be. And with this I get both:
for the scoped and
which is exactly what we want :)
This use case may be semi-niche, but I don't think the case of forcing a path parameter to be required is niche at all!