77import io .swagger .v3 .oas .models .OpenAPI ;
88import io .swagger .v3 .oas .models .Operation ;
99import io .swagger .v3 .oas .models .PathItem ;
10+ import io .swagger .v3 .oas .models .SpecVersion ;
1011import io .swagger .v3 .oas .models .headers .Header ;
1112import io .swagger .v3 .oas .models .media .ArraySchema ;
1213import io .swagger .v3 .oas .models .media .Content ;
@@ -83,33 +84,35 @@ public class SpringDocSecurityOAuth2Customizer implements GlobalOpenApiCustomize
8384 @ Override
8485 public void customise (OpenAPI openAPI ) {
8586 FilterChainProxy filterChainProxy = applicationContext .getBean (AbstractSecurityWebApplicationInitializer .DEFAULT_FILTER_NAME , FilterChainProxy .class );
87+ boolean openapi31 = SpecVersion .V31 == openAPI .getSpecVersion ();
8688 for (SecurityFilterChain filterChain : filterChainProxy .getFilterChains ()) {
87- getNimbusJwkSetEndpoint (openAPI , filterChain );
88- getOAuth2AuthorizationServerMetadataEndpoint (openAPI , filterChain );
89- getOAuth2TokenEndpoint (openAPI , filterChain );
90- getOAuth2AuthorizationEndpoint (openAPI , filterChain );
91- getOAuth2TokenIntrospectionEndpointFilter (openAPI , filterChain );
92- getOAuth2TokenRevocationEndpointFilter (openAPI , filterChain );
93- getOidcProviderConfigurationEndpoint (openAPI , filterChain );
89+ getNimbusJwkSetEndpoint (openAPI , filterChain , openapi31 );
90+ getOAuth2AuthorizationServerMetadataEndpoint (openAPI , filterChain , openapi31 );
91+ getOAuth2TokenEndpoint (openAPI , filterChain , openapi31 );
92+ getOAuth2AuthorizationEndpoint (openAPI , filterChain , openapi31 );
93+ getOAuth2TokenIntrospectionEndpointFilter (openAPI , filterChain , openapi31 );
94+ getOAuth2TokenRevocationEndpointFilter (openAPI , filterChain , openapi31 );
95+ getOidcProviderConfigurationEndpoint (openAPI , filterChain , openapi31 );
9496 getOidcUserInfoEndpoint (openAPI , filterChain );
95- getOidcClientRegistrationEndpoint (openAPI , filterChain );
97+ getOidcClientRegistrationEndpoint (openAPI , filterChain , openapi31 );
9698 }
9799 }
98100
99101 /**
100102 * Gets o auth 2 token revocation endpoint filter.
101103 *
102- * @param openAPI the open api
104+ * @param openAPI the open api
103105 * @param securityFilterChain the security filter chain
106+ * @param openapi31 the openapi 31
104107 */
105- private void getOAuth2TokenRevocationEndpointFilter (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
108+ private void getOAuth2TokenRevocationEndpointFilter (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
106109 Object oAuth2EndpointFilter =
107110 new SpringDocSecurityOAuth2EndpointUtils (OAuth2TokenRevocationEndpointFilter .class ).findEndpoint (securityFilterChain );
108111 if (oAuth2EndpointFilter != null ) {
109112 ApiResponses apiResponses = new ApiResponses ();
110113 apiResponses .addApiResponse (String .valueOf (HttpStatus .OK .value ()), new ApiResponse ().description (HttpStatus .OK .getReasonPhrase ()));
111114 buildApiResponsesOnInternalServerError (apiResponses );
112- buildApiResponsesOnBadRequest (apiResponses , openAPI );
115+ buildApiResponsesOnBadRequest (apiResponses , openAPI , openapi31 );
113116
114117 Operation operation = buildOperation (apiResponses );
115118 Schema <?> schema = new ObjectSchema ()
@@ -126,17 +129,18 @@ private void getOAuth2TokenRevocationEndpointFilter(OpenAPI openAPI, SecurityFil
126129 /**
127130 * Gets o auth 2 token introspection endpoint filter.
128131 *
129- * @param openAPI the open api
132+ * @param openAPI the open api
130133 * @param securityFilterChain the security filter chain
134+ * @param openapi31 the openapi 31
131135 */
132- private void getOAuth2TokenIntrospectionEndpointFilter (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
136+ private void getOAuth2TokenIntrospectionEndpointFilter (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
133137 Object oAuth2EndpointFilter =
134138 new SpringDocSecurityOAuth2EndpointUtils (OAuth2TokenIntrospectionEndpointFilter .class ).findEndpoint (securityFilterChain );
135139 if (oAuth2EndpointFilter != null ) {
136140 ApiResponses apiResponses = new ApiResponses ();
137- buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOAuth2TokenIntrospection .class , openAPI .getComponents (), null ));
141+ buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOAuth2TokenIntrospection .class , openAPI .getComponents (), null , openapi31 ));
138142 buildApiResponsesOnInternalServerError (apiResponses );
139- buildApiResponsesOnBadRequest (apiResponses , openAPI );
143+ buildApiResponsesOnBadRequest (apiResponses , openAPI , openapi31 );
140144
141145 Operation operation = buildOperation (apiResponses );
142146 Schema <?> requestSchema = new ObjectSchema ()
@@ -154,15 +158,16 @@ private void getOAuth2TokenIntrospectionEndpointFilter(OpenAPI openAPI, Security
154158 /**
155159 * Gets o auth 2 authorization server metadata endpoint.
156160 *
157- * @param openAPI the open api
161+ * @param openAPI the open api
158162 * @param securityFilterChain the security filter chain
163+ * @param openapi31 the openapi 31
159164 */
160- private void getOAuth2AuthorizationServerMetadataEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
165+ private void getOAuth2AuthorizationServerMetadataEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
161166 Object oAuth2EndpointFilter =
162167 new SpringDocSecurityOAuth2EndpointUtils (OAuth2AuthorizationServerMetadataEndpointFilter .class ).findEndpoint (securityFilterChain );
163168 if (oAuth2EndpointFilter != null ) {
164169 ApiResponses apiResponses = new ApiResponses ();
165- buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOAuth2AuthorizationServerMetadata .class , openAPI .getComponents (), null ));
170+ buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOAuth2AuthorizationServerMetadata .class , openAPI .getComponents (), null , openapi31 ));
166171 buildApiResponsesOnInternalServerError (apiResponses );
167172 Operation operation = buildOperation (apiResponses );
168173 buildPath (oAuth2EndpointFilter , "requestMatcher" , openAPI , operation , HttpMethod .GET );
@@ -172,10 +177,11 @@ private void getOAuth2AuthorizationServerMetadataEndpoint(OpenAPI openAPI, Secur
172177 /**
173178 * Gets nimbus jwk set endpoint.
174179 *
175- * @param openAPI the open api
180+ * @param openAPI the open api
176181 * @param securityFilterChain the security filter chain
182+ * @param openapi31 the openapi 31
177183 */
178- private void getNimbusJwkSetEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
184+ private void getNimbusJwkSetEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
179185 Object oAuth2EndpointFilter =
180186 new SpringDocSecurityOAuth2EndpointUtils (NimbusJwkSetEndpointFilter .class ).findEndpoint (securityFilterChain );
181187 if (oAuth2EndpointFilter != null ) {
@@ -188,7 +194,7 @@ private void getNimbusJwkSetEndpoint(OpenAPI openAPI, SecurityFilterChain securi
188194 new MediaType ().schema (schema )));
189195 apiResponses .addApiResponse (String .valueOf (HttpStatus .OK .value ()), response );
190196 buildApiResponsesOnInternalServerError (apiResponses );
191- buildApiResponsesOnBadRequest (apiResponses , openAPI );
197+ buildApiResponsesOnBadRequest (apiResponses , openAPI , openapi31 );
192198
193199 Operation operation = buildOperation (apiResponses );
194200 operation .responses (apiResponses );
@@ -199,19 +205,20 @@ private void getNimbusJwkSetEndpoint(OpenAPI openAPI, SecurityFilterChain securi
199205 /**
200206 * Gets o auth 2 token endpoint.
201207 *
202- * @param openAPI the open api
208+ * @param openAPI the open api
203209 * @param securityFilterChain the security filter chain
210+ * @param openapi31 the openapi 31
204211 */
205- private void getOAuth2TokenEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
212+ private void getOAuth2TokenEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
206213 Object oAuth2EndpointFilter =
207214 new SpringDocSecurityOAuth2EndpointUtils (OAuth2TokenEndpointFilter .class ).findEndpoint (securityFilterChain );
208215
209216 if (oAuth2EndpointFilter != null ) {
210217 ApiResponses apiResponses = new ApiResponses ();
211- buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOAuth2Token .class , openAPI .getComponents (), null ));
218+ buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOAuth2Token .class , openAPI .getComponents (), null , openapi31 ));
212219 buildApiResponsesOnInternalServerError (apiResponses );
213- buildApiResponsesOnBadRequest (apiResponses , openAPI );
214- buildOAuth2Error (openAPI , apiResponses , HttpStatus .UNAUTHORIZED );
220+ buildApiResponsesOnBadRequest (apiResponses , openAPI , openapi31 );
221+ buildOAuth2Error (openAPI , apiResponses , HttpStatus .UNAUTHORIZED , openapi31 );
215222 Operation operation = buildOperation (apiResponses );
216223
217224 Schema <?> requestSchema = new ObjectSchema ()
@@ -242,10 +249,11 @@ private void getOAuth2TokenEndpoint(OpenAPI openAPI, SecurityFilterChain securit
242249 /**
243250 * Gets o auth 2 authorization endpoint.
244251 *
245- * @param openAPI the open api
252+ * @param openAPI the open api
246253 * @param securityFilterChain the security filter chain
254+ * @param openapi31 the openapi 31
247255 */
248- private void getOAuth2AuthorizationEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
256+ private void getOAuth2AuthorizationEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
249257 Object oAuth2EndpointFilter =
250258 new SpringDocSecurityOAuth2EndpointUtils (OAuth2AuthorizationEndpointFilter .class ).findEndpoint (securityFilterChain );
251259 if (oAuth2EndpointFilter != null ) {
@@ -256,7 +264,7 @@ private void getOAuth2AuthorizationEndpoint(OpenAPI openAPI, SecurityFilterChain
256264 new MediaType ()));
257265 apiResponses .addApiResponse (String .valueOf (HttpStatus .OK .value ()), response );
258266 buildApiResponsesOnInternalServerError (apiResponses );
259- buildApiResponsesOnBadRequest (apiResponses , openAPI );
267+ buildApiResponsesOnBadRequest (apiResponses , openAPI , openapi31 );
260268 apiResponses .addApiResponse (String .valueOf (HttpStatus .MOVED_TEMPORARILY .value ()),
261269 new ApiResponse ().description (HttpStatus .MOVED_TEMPORARILY .getReasonPhrase ())
262270 .addHeaderObject ("Location" , new Header ().schema (new StringSchema ())));
@@ -270,16 +278,17 @@ private void getOAuth2AuthorizationEndpoint(OpenAPI openAPI, SecurityFilterChain
270278 /**
271279 * Gets OpenID Provider endpoint filter
272280 *
273- * @param openAPI the open api
281+ * @param openAPI the open api
274282 * @param securityFilterChain the security filter chain
283+ * @param openapi31 the openapi 31
275284 */
276- private void getOidcProviderConfigurationEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
285+ private void getOidcProviderConfigurationEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
277286 Object oAuth2EndpointFilter =
278287 new SpringDocSecurityOAuth2EndpointUtils (OidcProviderConfigurationEndpointFilter .class ).findEndpoint (securityFilterChain );
279288
280289 if (oAuth2EndpointFilter != null ) {
281290 ApiResponses apiResponses = new ApiResponses ();
282- buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOidcProviderConfiguration .class , openAPI .getComponents (), null ));
291+ buildApiResponsesOnSuccess (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOidcProviderConfiguration .class , openAPI .getComponents (), null , openapi31 ));
283292 buildApiResponsesOnInternalServerError (apiResponses );
284293 Operation operation = buildOperation (apiResponses );
285294 buildPath (oAuth2EndpointFilter , "requestMatcher" , openAPI , operation , HttpMethod .GET );
@@ -309,24 +318,25 @@ private void getOidcUserInfoEndpoint(OpenAPI openAPI, SecurityFilterChain securi
309318 /**
310319 * Gets OpenID Client Registration endpoint filter
311320 *
312- * @param openAPI the open api
321+ * @param openAPI the open api
313322 * @param securityFilterChain the security filter chain
323+ * @param openapi31 the openapi 31
314324 */
315- private void getOidcClientRegistrationEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain ) {
325+ private void getOidcClientRegistrationEndpoint (OpenAPI openAPI , SecurityFilterChain securityFilterChain , boolean openapi31 ) {
316326 Object oAuth2EndpointFilter =
317327 new SpringDocSecurityOAuth2EndpointUtils (OidcClientRegistrationEndpointFilter .class ).findEndpoint (securityFilterChain );
318328
319329 if (oAuth2EndpointFilter != null ) {
320330 ApiResponses apiResponses = new ApiResponses ();
321- buildApiResponsesOnCreated (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOidcClientRegistrationResponse .class , openAPI .getComponents (), null ));
331+ buildApiResponsesOnCreated (apiResponses , AnnotationsUtils .resolveSchemaFromType (SpringDocOidcClientRegistrationResponse .class , openAPI .getComponents (), null , openapi31 ));
322332 buildApiResponsesOnInternalServerError (apiResponses );
323- buildApiResponsesOnBadRequest (apiResponses , openAPI );
324- buildOAuth2Error (openAPI , apiResponses , HttpStatus .UNAUTHORIZED );
325- buildOAuth2Error (openAPI , apiResponses , HttpStatus .FORBIDDEN );
333+ buildApiResponsesOnBadRequest (apiResponses , openAPI , openapi31 );
334+ buildOAuth2Error (openAPI , apiResponses , HttpStatus .UNAUTHORIZED , openapi31 );
335+ buildOAuth2Error (openAPI , apiResponses , HttpStatus .FORBIDDEN , openapi31 );
326336 Operation operation = buildOperation (apiResponses );
327337
328338 // OidcClientRegistration
329- Schema schema = AnnotationsUtils .resolveSchemaFromType (SpringDocOidcClientRegistrationRequest .class , openAPI .getComponents (), null );
339+ Schema schema = AnnotationsUtils .resolveSchemaFromType (SpringDocOidcClientRegistrationRequest .class , openAPI .getComponents (), null , openapi31 );
330340
331341 String mediaType = APPLICATION_JSON_VALUE ;
332342 RequestBody requestBody = new RequestBody ().content (new Content ().addMediaType (mediaType , new MediaType ().schema (schema )));
@@ -395,23 +405,25 @@ private ApiResponses buildApiResponsesOnInternalServerError(ApiResponses apiResp
395405 * Build api responses on bad request.
396406 *
397407 * @param apiResponses the api responses
398- * @param openAPI the open api
408+ * @param openAPI the open api
409+ * @param openapi31 the openapi 31
399410 * @return the api responses
400411 */
401- private ApiResponses buildApiResponsesOnBadRequest (ApiResponses apiResponses , OpenAPI openAPI ) {
402- buildOAuth2Error (openAPI , apiResponses , HttpStatus .BAD_REQUEST );
412+ private ApiResponses buildApiResponsesOnBadRequest (ApiResponses apiResponses , OpenAPI openAPI , boolean openapi31 ) {
413+ buildOAuth2Error (openAPI , apiResponses , HttpStatus .BAD_REQUEST , openapi31 );
403414 return apiResponses ;
404415 }
405416
406417 /**
407418 * Build o auth 2 error.
408419 *
409- * @param openAPI the open api
420+ * @param openAPI the open api
410421 * @param apiResponses the api responses
411- * @param httpStatus the http status
422+ * @param httpStatus the http status
423+ * @param openapi31 the openapi 31
412424 */
413- private static void buildOAuth2Error (OpenAPI openAPI , ApiResponses apiResponses , HttpStatus httpStatus ) {
414- Schema oAuth2ErrorSchema = AnnotationsUtils .resolveSchemaFromType (OAuth2Error .class , openAPI .getComponents (), null );
425+ private static void buildOAuth2Error (OpenAPI openAPI , ApiResponses apiResponses , HttpStatus httpStatus , boolean openapi31 ) {
426+ Schema oAuth2ErrorSchema = AnnotationsUtils .resolveSchemaFromType (OAuth2Error .class , openAPI .getComponents (), null , openapi31 );
415427 apiResponses .addApiResponse (String .valueOf (httpStatus .value ()), new ApiResponse ().description (httpStatus .getReasonPhrase ()).content (new Content ().addMediaType (
416428 APPLICATION_JSON_VALUE ,
417429 new MediaType ().schema (oAuth2ErrorSchema ))));
0 commit comments