Skip to content

Commit d2016d8

Browse files
cbornetwing328
authored andcommitted
Fix spring controller validation (#225)
1 parent defedf6 commit d2016d8

File tree

402 files changed

+145
-648
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

402 files changed

+145
-648
lines changed

modules/openapi-generator/src/main/resources/JavaSpring/api.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import org.springframework.http.HttpStatus;
1313
import org.springframework.http.MediaType;
1414
{{/jdk8-no-delegate}}
1515
import org.springframework.http.ResponseEntity;
16+
{{#useBeanValidation}}
17+
import org.springframework.validation.annotation.Validated;
18+
{{/useBeanValidation}}
1619
import org.springframework.web.bind.annotation.PathVariable;
1720
import org.springframework.web.bind.annotation.RequestBody;
1821
import org.springframework.web.bind.annotation.RequestHeader;
@@ -50,6 +53,9 @@ import java.util.Optional;
5053
import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}};
5154
{{/async}}
5255
{{>generatedAnnotation}}
56+
{{#useBeanValidation}}
57+
@Validated
58+
{{/useBeanValidation}}
5359
@Api(value = "{{{baseName}}}", description = "the {{{baseName}}} API")
5460
{{#operations}}
5561
public interface {{classname}} {

modules/openapi-generator/src/main/resources/JavaSpring/model.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import java.util.Objects;
77
import java.io.Serializable;
88
{{/serializableModel}}
99
{{#useBeanValidation}}
10-
import org.springframework.validation.annotation.Validated;
1110
import javax.validation.Valid;
1211
import javax.validation.constraints.*;
1312
{{/useBeanValidation}}

modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}}
33
*/{{#description}}
44
@ApiModel(description = "{{{description}}}"){{/description}}
5-
{{#useBeanValidation}}@Validated{{/useBeanValidation}}
65
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
76
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#serializableModel}}implements Serializable{{/serializableModel}} {
87
{{#serializableModel}}

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.http.HttpStatus;
1313
import org.springframework.http.MediaType;
1414
import org.springframework.http.ResponseEntity;
15+
import org.springframework.validation.annotation.Validated;
1516
import org.springframework.web.bind.annotation.PathVariable;
1617
import org.springframework.web.bind.annotation.RequestBody;
1718
import org.springframework.web.bind.annotation.RequestHeader;
@@ -28,6 +29,7 @@
2829
import java.util.Map;
2930
import java.util.Optional;
3031

32+
@Validated
3133
@Api(value = "Pet", description = "the Pet API")
3234
public interface PetApi {
3335

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/api/StoreApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.MediaType;
1313
import org.springframework.http.ResponseEntity;
14+
import org.springframework.validation.annotation.Validated;
1415
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.RequestBody;
1617
import org.springframework.web.bind.annotation.RequestHeader;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829
import java.util.Optional;
2930

31+
@Validated
3032
@Api(value = "Store", description = "the Store API")
3133
public interface StoreApi {
3234

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/api/UserApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.MediaType;
1313
import org.springframework.http.ResponseEntity;
14+
import org.springframework.validation.annotation.Validated;
1415
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.RequestBody;
1617
import org.springframework.web.bind.annotation.RequestHeader;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829
import java.util.Optional;
2930

31+
@Validated
3032
@Api(value = "User", description = "the User API")
3133
public interface UserApi {
3234

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/model/Category.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* A category for a pet
1413
*/
1514
@ApiModel(description = "A category for a pet")
16-
@Validated
1715

1816
public class Category {
1917
@JsonProperty("id")

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/model/ModelApiResponse.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* Describes the result of uploading an image resource
1413
*/
1514
@ApiModel(description = "Describes the result of uploading an image resource")
16-
@Validated
1715

1816
public class ModelApiResponse {
1917
@JsonProperty("code")

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/model/Order.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
import io.swagger.annotations.ApiModel;
88
import io.swagger.annotations.ApiModelProperty;
99
import java.time.OffsetDateTime;
10-
import org.springframework.validation.annotation.Validated;
1110
import javax.validation.Valid;
1211
import javax.validation.constraints.*;
1312

1413
/**
1514
* An order for a pets from the pet store
1615
*/
1716
@ApiModel(description = "An order for a pets from the pet store")
18-
@Validated
1917

2018
public class Order {
2119
@JsonProperty("id")

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/model/Pet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
import java.util.List;
1111
import org.openapitools.model.Category;
1212
import org.openapitools.model.Tag;
13-
import org.springframework.validation.annotation.Validated;
1413
import javax.validation.Valid;
1514
import javax.validation.constraints.*;
1615

1716
/**
1817
* A pet for sale in the pet store
1918
*/
2019
@ApiModel(description = "A pet for sale in the pet store")
21-
@Validated
2220

2321
public class Pet {
2422
@JsonProperty("id")

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/model/Tag.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* A tag for a pet
1413
*/
1514
@ApiModel(description = "A tag for a pet")
16-
@Validated
1715

1816
public class Tag {
1917
@JsonProperty("id")

samples/client/petstore/spring-cloud/src/main/java/org/openapitools/model/User.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* A User who is purchasing from the pet store
1413
*/
1514
@ApiModel(description = "A User who is purchasing from the pet store")
16-
@Validated
1715

1816
public class User {
1917
@JsonProperty("id")

samples/client/petstore/spring-cloud/src/test/java/org/openapitools/api/StoreApiTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
66
import org.openapitools.Application;
7-
import org.openapitools.TestUtils;
87
import org.openapitools.model.Order;
98
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.boot.test.context.SpringBootTest;
@@ -31,6 +30,11 @@ public void testGetInventory() {
3130
@Test
3231
public void testPlaceOrder() {
3332
Order order = createOrder();
33+
try {
34+
client.deleteOrder(order.getId().toString()).execute();
35+
} catch (HystrixRuntimeException e) {
36+
// noop
37+
}
3438
client.placeOrder(order).execute();
3539

3640
Order fetched = client.getOrderById(order.getId()).execute().getBody();
@@ -43,6 +47,7 @@ public void testPlaceOrder() {
4347
@Test
4448
public void testDeleteOrder() {
4549
Order order = createOrder();
50+
client.deleteOrder(order.getId().toString());
4651
client.placeOrder(order).execute();
4752

4853
Order fetched = client.getOrderById(order.getId()).execute().getBody();
@@ -60,7 +65,7 @@ public void testDeleteOrder() {
6065

6166
private Order createOrder() {
6267
return new Order()
63-
.id(TestUtils.nextId())
68+
.id(1L)
6469
.petId(200L)
6570
.quantity(13)
6671
//Ensure 3 fractional digits because of a bug in the petstore server

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.http.HttpStatus;
1313
import org.springframework.http.MediaType;
1414
import org.springframework.http.ResponseEntity;
15+
import org.springframework.validation.annotation.Validated;
1516
import org.springframework.web.bind.annotation.PathVariable;
1617
import org.springframework.web.bind.annotation.RequestBody;
1718
import org.springframework.web.bind.annotation.RequestHeader;
@@ -28,6 +29,7 @@
2829
import java.util.Map;
2930
import java.util.Optional;
3031

32+
@Validated
3133
@Api(value = "pet", description = "the pet API")
3234
public interface PetApi {
3335

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/api/StoreApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.MediaType;
1313
import org.springframework.http.ResponseEntity;
14+
import org.springframework.validation.annotation.Validated;
1415
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.RequestBody;
1617
import org.springframework.web.bind.annotation.RequestHeader;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829
import java.util.Optional;
2930

31+
@Validated
3032
@Api(value = "store", description = "the store API")
3133
public interface StoreApi {
3234

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/api/UserApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.MediaType;
1313
import org.springframework.http.ResponseEntity;
14+
import org.springframework.validation.annotation.Validated;
1415
import org.springframework.web.bind.annotation.PathVariable;
1516
import org.springframework.web.bind.annotation.RequestBody;
1617
import org.springframework.web.bind.annotation.RequestHeader;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829
import java.util.Optional;
2930

31+
@Validated
3032
@Api(value = "user", description = "the user API")
3133
public interface UserApi {
3234

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/model/Category.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* A category for a pet
1413
*/
1514
@ApiModel(description = "A category for a pet")
16-
@Validated
1715

1816
public class Category {
1917
@JsonProperty("id")

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/model/ModelApiResponse.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* Describes the result of uploading an image resource
1413
*/
1514
@ApiModel(description = "Describes the result of uploading an image resource")
16-
@Validated
1715

1816
public class ModelApiResponse {
1917
@JsonProperty("code")

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/model/Order.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
import io.swagger.annotations.ApiModel;
88
import io.swagger.annotations.ApiModelProperty;
99
import java.time.OffsetDateTime;
10-
import org.springframework.validation.annotation.Validated;
1110
import javax.validation.Valid;
1211
import javax.validation.constraints.*;
1312

1413
/**
1514
* An order for a pets from the pet store
1615
*/
1716
@ApiModel(description = "An order for a pets from the pet store")
18-
@Validated
1917

2018
public class Order {
2119
@JsonProperty("id")

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/model/Pet.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
import java.util.List;
1111
import org.openapitools.model.Category;
1212
import org.openapitools.model.Tag;
13-
import org.springframework.validation.annotation.Validated;
1413
import javax.validation.Valid;
1514
import javax.validation.constraints.*;
1615

1716
/**
1817
* A pet for sale in the pet store
1918
*/
2019
@ApiModel(description = "A pet for sale in the pet store")
21-
@Validated
2220

2321
public class Pet {
2422
@JsonProperty("id")

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/model/Tag.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* A tag for a pet
1413
*/
1514
@ApiModel(description = "A tag for a pet")
16-
@Validated
1715

1816
public class Tag {
1917
@JsonProperty("id")

samples/client/petstore/spring-stubs/src/main/java/org/openapitools/model/User.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import com.fasterxml.jackson.annotation.JsonCreator;
66
import io.swagger.annotations.ApiModel;
77
import io.swagger.annotations.ApiModelProperty;
8-
import org.springframework.validation.annotation.Validated;
98
import javax.validation.Valid;
109
import javax.validation.constraints.*;
1110

1211
/**
1312
* A User who is purchasing from the pet store
1413
*/
1514
@ApiModel(description = "A User who is purchasing from the pet store")
16-
@Validated
1715

1816
public class User {
1917
@JsonProperty("id")

samples/server/petstore/spring-mvc-j8-async/src/main/java/org/openapitools/api/AnotherFakeApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.http.HttpStatus;
1111
import org.springframework.http.MediaType;
1212
import org.springframework.http.ResponseEntity;
13+
import org.springframework.validation.annotation.Validated;
1314
import org.springframework.web.bind.annotation.PathVariable;
1415
import org.springframework.web.bind.annotation.RequestBody;
1516
import org.springframework.web.bind.annotation.RequestHeader;
@@ -27,6 +28,7 @@
2728
import java.util.Optional;
2829
import java.util.concurrent.CompletableFuture;
2930

31+
@Validated
3032
@Api(value = "another-fake", description = "the another-fake API")
3133
public interface AnotherFakeApi {
3234

samples/server/petstore/spring-mvc-j8-async/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.springframework.http.HttpStatus;
1818
import org.springframework.http.MediaType;
1919
import org.springframework.http.ResponseEntity;
20+
import org.springframework.validation.annotation.Validated;
2021
import org.springframework.web.bind.annotation.PathVariable;
2122
import org.springframework.web.bind.annotation.RequestBody;
2223
import org.springframework.web.bind.annotation.RequestHeader;
@@ -34,6 +35,7 @@
3435
import java.util.Optional;
3536
import java.util.concurrent.CompletableFuture;
3637

38+
@Validated
3739
@Api(value = "fake", description = "the fake API")
3840
public interface FakeApi {
3941

0 commit comments

Comments
 (0)