Skip to content

Commit 4792755

Browse files
author
bnasslahsen
committed
Added MonetaryAmount support, out of the box. Fixes #606
1 parent 5226e1d commit 4792755

File tree

7 files changed

+171
-1
lines changed

7 files changed

+171
-1
lines changed

springdoc-openapi-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@
4343
<groupId>org.apache.commons</groupId>
4444
<artifactId>commons-lang3</artifactId>
4545
</dependency>
46+
<dependency>
47+
<groupId>javax.money</groupId>
48+
<artifactId>money-api</artifactId>
49+
<optional>true</optional>
50+
</dependency>
4651
</dependencies>
4752
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.springdoc.core;
2+
3+
4+
import javax.money.MonetaryAmount;
5+
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
11+
12+
@ConditionalOnClass(MonetaryAmount.class)
13+
@Configuration
14+
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
15+
public class SpringDocMonetaryAmountConfiguration {
16+
17+
static {
18+
SpringDocUtils.getConfig().replaceWithClass(MonetaryAmount.class, org.springdoc.core.converters.MonetaryAmount.class);
19+
}
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springdoc.core.converters;
2+
3+
import java.math.BigDecimal;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
8+
@Schema
9+
public class MonetaryAmount {
10+
11+
@JsonProperty("amount")
12+
@Schema(example = "99.96")
13+
private BigDecimal amount;
14+
15+
@JsonProperty("currency")
16+
@Schema(example = "USD")
17+
private String currency;
18+
19+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
22
org.springdoc.core.SpringDocConfiguration,\
3-
org.springdoc.core.SpringDocConfigProperties
3+
org.springdoc.core.SpringDocConfigProperties,\
4+
org.springdoc.core.SpringDocMonetaryAmountConfiguration
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app114;
20+
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import org.springdoc.core.converters.MonetaryAmount;
23+
24+
import org.springframework.http.MediaType;
25+
import org.springframework.web.bind.annotation.PostMapping;
26+
import org.springframework.web.bind.annotation.RequestBody;
27+
import org.springframework.web.bind.annotation.RestController;
28+
29+
@RestController
30+
public class HelloController {
31+
32+
@PostMapping(value = "/foos1", consumes = MediaType.APPLICATION_JSON_VALUE)
33+
MonetaryAmount getCurrency(@RequestBody CarDTO carDTO) {
34+
return carDTO.price;
35+
}
36+
37+
class CarDTO {
38+
@JsonProperty("price")
39+
MonetaryAmount price;
40+
}
41+
42+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.app114;
2+
3+
import test.org.springdoc.api.AbstractSpringDocTest;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
8+
public class SpringDocApp114Test extends AbstractSpringDocTest {
9+
10+
@SpringBootApplication
11+
static class SpringDocTestApp {}
12+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/foos1": {
15+
"post": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "getCurrency",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"$ref": "#/components/schemas/CarDTO"
25+
}
26+
}
27+
},
28+
"required": true
29+
},
30+
"responses": {
31+
"200": {
32+
"description": "OK",
33+
"content": {
34+
"*/*": {
35+
"schema": {
36+
"$ref": "#/components/schemas/MonetaryAmount"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
},
45+
"components": {
46+
"schemas": {
47+
"CarDTO": {
48+
"type": "object",
49+
"properties": {
50+
"price": {
51+
"$ref": "#/components/schemas/MonetaryAmount"
52+
}
53+
}
54+
},
55+
"MonetaryAmount": {
56+
"type": "object",
57+
"properties": {
58+
"amount": {
59+
"type": "number",
60+
"example": 99.96
61+
},
62+
"currency": {
63+
"type": "string",
64+
"example": "USD"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)