Skip to content

fix for flaky operationIds #886

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
Oct 1, 2020
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 @@ -20,13 +20,7 @@

package org.springdoc.webflux.api;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.core.util.Json;
Expand Down Expand Up @@ -190,7 +184,9 @@ protected void getPaths(Map<String, Object> restControllers) {
* @param map the map
*/
protected void calculatePath(Map<String, Object> restControllers, Map<RequestMappingInfo, HandlerMethod> map) {
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) {
List<Map.Entry<RequestMappingInfo, HandlerMethod>> entries = new ArrayList<>(map.entrySet());
entries.sort(byReversedRequestMappingInfos());
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : entries) {
RequestMappingInfo requestMappingInfo = entry.getKey();
HandlerMethod handlerMethod = entry.getValue();
PatternsRequestCondition patternsRequestCondition = requestMappingInfo.getPatternsCondition();
Expand All @@ -215,6 +211,12 @@ && isFilterCondition(handlerMethod, operationPath, produces, consumes, headers))
}
}

private Comparator<Map.Entry<RequestMappingInfo, HandlerMethod>> byReversedRequestMappingInfos() {
return Comparator.<Map.Entry<RequestMappingInfo, HandlerMethod>, String>
comparing(a -> a.getKey().toString())
.reversed();
}

/**
* Gets web flux router function paths.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* * Copyright 2019-2020 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.app81;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class OperationIdController {

@GetMapping(path = "/test_0") // gets operationId opIdTest_3
public Mono<String> opIdTest() {
return null;
}

@GetMapping(path = "/test_1") // gets operationId opIdTest_2
public Mono<String> opIdTest(@RequestParam String param) {
return null;
}

@GetMapping(path = "/test_2") // gets operationId opIdTest_1
public Mono<String> opIdTest(@RequestParam Integer param) {
return null;
}

@GetMapping(path = "/test_3") // gets operationId opIdTest
public Mono<String> opIdTest(@RequestParam Boolean param) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
*
* *
* * *
* * * * Copyright 2019-2020 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.app81;

import org.junit.jupiter.api.RepeatedTest;
import org.springdoc.webflux.api.OpenApiResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;

import java.net.URI;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.springdoc.core.Constants.SPRINGDOC_CACHE_DISABLED;
import static test.org.springdoc.api.AbstractSpringDocTest.getContent;


/**
* Tests deterministic creation of operationIds
*/
@AutoConfigureWebTestClient(timeout = "3600000")
@WebFluxTest(properties = SPRINGDOC_CACHE_DISABLED + "=true")
@ActiveProfiles("test")
public class SpringDocApp81Test {

@SpringBootApplication
@ComponentScan(basePackages = {"org.springdoc", "test.org.springdoc.api.app81"})
static class SpringDocTestApp {
}

@Autowired
OpenApiResource resource;

@Autowired
RequestMappingInfoHandlerMapping mappingInfoHandlerMapping;

@RepeatedTest(10)
public void shouldGenerateOperationIdsDeterministically() throws Exception {
shuffleSpringHandlerMethods();

ServerHttpRequest request = mock(ServerHttpRequest.class);
when(request.getURI()).thenReturn(URI.create("http://localhost"));

String expected = getContent("results/app81.json");
String openApi = resource.openapiJson(request, "").block();
assertEquals(expected, openApi, true);
}

private void shuffleSpringHandlerMethods() {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = mappingInfoHandlerMapping.getHandlerMethods();
List<Map.Entry<RequestMappingInfo, HandlerMethod>> collect = new ArrayList<>(handlerMethods.entrySet());
collect.sort(Comparator.comparing(a -> ThreadLocalRandom.current().nextBoolean() ? -1 : 1));

collect.forEach(e -> mappingInfoHandlerMapping.unregisterMapping(e.getKey()));
collect.forEach(e -> mappingInfoHandlerMapping.registerMapping(e.getKey(), e.getValue().getBean(), e.getValue().getMethod()));
}

}
127 changes: 127 additions & 0 deletions springdoc-openapi-webflux-core/src/test/resources/results/app81.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/test_3": {
"get": {
"tags": [
"operation-id-controller"
],
"operationId": "opIdTest",
"parameters": [
{
"name": "param",
"in": "query",
"required": true,
"schema": {
"type": "boolean"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/test_2": {
"get": {
"tags": [
"operation-id-controller"
],
"operationId": "opIdTest_1",
"parameters": [
{
"name": "param",
"in": "query",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/test_1": {
"get": {
"tags": [
"operation-id-controller"
],
"operationId": "opIdTest_2",
"parameters": [
{
"name": "param",
"in": "query",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/test_0": {
"get": {
"tags": [
"operation-id-controller"
],
"operationId": "opIdTest_3",
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@

package org.springdoc.webmvc.api;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

Expand Down Expand Up @@ -241,7 +236,9 @@ protected void getPaths(Map<String, Object> restControllers) {
* @param map the map
*/
protected void calculatePath(Map<String, Object> restControllers, Map<RequestMappingInfo, HandlerMethod> map) {
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) {
List<Map.Entry<RequestMappingInfo, HandlerMethod>> entries = new ArrayList<>(map.entrySet());
entries.sort(byReversedRequestMappingInfos());
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : entries) {
RequestMappingInfo requestMappingInfo = entry.getKey();
HandlerMethod handlerMethod = entry.getValue();
PatternsRequestCondition patternsRequestCondition = requestMappingInfo.getPatternsCondition();
Expand All @@ -265,6 +262,12 @@ && isFilterCondition(handlerMethod, operationPath, produces, consumes, headers))
}
}

private Comparator<Map.Entry<RequestMappingInfo, HandlerMethod>> byReversedRequestMappingInfos() {
return Comparator.<Map.Entry<RequestMappingInfo, HandlerMethod>, String>
comparing(a -> a.getKey().toString())
.reversed();
}

/**
* Is rest controller boolean.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
*
* * Copyright 2019-2020 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.app136;

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

@RestController
public class OperationIdController {

@GetMapping(path = "/test_0") // gets operationId opIdTest_3
public String opIdTest() {
return "";
}

@GetMapping(path = "/test_1") // gets operationId opIdTest_2
public String opIdTest(@RequestParam String param) {
return "";
}

@GetMapping(path = "/test_2") // gets operationId opIdTest_1
public String opIdTest(@RequestParam Integer param) {
return "";
}

@GetMapping(path = "/test_3") // gets operationId opIdTest
public String opIdTest(@RequestParam Boolean param) {
return "";
}

}
Loading