openapi.yaml ์ฌ์์ ์์ฑํ ๋ Swagger๊ฐ ๊ฐ์ฒด ํ๋์ @Schema(required = true)
์ฃผ์์ ๋ฌด์ํ๋ ์ฌํ ๊ฐ๋ฅํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
์ด๋ฅผ ์ฌํํ๋ ค๋ฉด ์ผ๋ฐ ๋จ๊ณ๋ฅผ ๋ฐ๋ฅด์ญ์์ค.
1) 2.0์์ swagger-samples ์ ์ฅ์๋ฅผ ๋ณต์ ํฉ๋๋ค(https://github.com/swagger-api/swagger-samples/tree/2.0).
2) Swagger 2.0.8(๋น์ค๋
์ท)์ ์ฌ์ฉํ๋๋ก ๋ฃจํธ pom.xml์ ๋ณ๊ฒฝํฉ๋๋ค.
3) java-jersey2-minimal๋ก ์ด๋ํฉ๋๋ค.
4) Pet
๊ฐ์ฒด ์๋์์ ์ฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ ํฉ๋๋ค.
@XmlElement(name = "id")
@Schema(required = true)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@XmlElement(name = "category")
@Schema(required = true)
public Category getCategory() {
return category;
}
(์ฌ๊ธฐ์ "id" ๋ฐ "category" ํ๋ ๋ชจ๋์ @Schema(required = true)
ํ์ ํ๊ทธ๋ฅผ ์ถ๊ฐํฉ๋๋ค.)
5) ์ค๋ช ๋ ๋๋ก ์ํ์ ๋น๋ํ๊ณ (https://github.com/swagger-api/swagger-samples/tree/2.0/java/java-jersey2-minimal) yaml์ ์์ฑํฉ๋๋ค.
์ด ๋จ๊ณ๋ฅผ ์ํํ ํ ์์ฑ๋ yaml์ ๋ค์์ด ํฌํจ๋์ด ์์์ ์ ์ ์์ต๋๋ค.
...
Pet:
required:
- id
type: object
properties:
id:
type: integer
format: int64
category:
$ref: '#/components/schemas/Category'
...
์ด๊ฒ์ ์์์น ๋ชปํ ์ผ์
๋๋ค. ๋ด๊ฐ ์ถ๊ฐํ๋ ๊ฒ์ ๊ธฐ๋ @Schema(required = true)
๋ก id
๋ฐ category
ํ๋๋ฅผ ๋ชจ๋ ์ถ๊ฐ ํ ์ id
๋ฐ category
์ "์๊ตฌ"์น์
์ ์ ๋๋ฌผ ์. ๊ทธ๋ฌ๋ id
์ถ๊ฐ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ๋ค๋ฅธ ์๋๋ฆฌ์ค์์ ์ด๊ฒ์ ํ
์คํธํ์ผ๋ฉฐ OpenApi ํ๋ฆฌ๋ฏธํฐ๋ธ(์ฆ, $ref๊ฐ ์๋ ๊ฒฝ์ฐ)์ธ ๊ฒฝ์ฐ์๋ง ํ๋๋ฅผ "ํ์"๋ก ์ถ๊ฐํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
category
๋ฅผ "ํ์" ์น์
์ ์๋์ผ๋ก ์ถ๊ฐํ๋ฉด Swagger UI์ category
๊ฐ ํ์ํ๋ค๋ ๊ฒ์ด ์ฌ๋ฐ๋ฅด๊ฒ ํ์๋ฉ๋๋ค. ๊ทธ๋ฌ๋ ์๋์ผ๋ก ์ถ๊ฐํ์ง ์์ผ๋ฉด category
์ ๋ค๋ฅธ ํ์ ํ๋์ ๋์ผํ ๋นจ๊ฐ์ ๋ณํ๊ฐ ํ์๋์ง ์์ต๋๋ค.
์ด ๋ฌธ์ ๋ก ์ธํด ๋ณต์กํ ์ ํ์ ๊ฐ์ฒด๊ฐ API ์์ฒญ์์ ์ฑ์์ ธ์ผ ํ๊ณ ์๋ต๋ ์ ์๋ ๊ฐ์ฒด๋ฅผ ์ง์ ํ๊ธฐ๊ฐ ๋ ์ด๋ ต์ต๋๋ค. ์ด๊ฒ์ Immutables๋ฅผ ์ฌ์ฉํ ๋ ํนํ ์ ์ฉํฉ๋๋ค. Immutables์ ์ผ๋ถ ํ๋๋ ์๋ฌด ๊ฒ๋ ์ง์ ๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ๊ณ ๋ค๋ฅธ ํ๋์ ๊ฒฝ์ฐ immutables๋ ๋๋ฝ๋ ๊ฒฝ์ฐ ์์ธ๋ฅผ throwํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
https://stackoverflow.com/questions/51671883/swagger-ignores-schema-properties-for-referenced-schemas
์ด๊ฒ์ ๋ฐ์ฐฝ๊ณ ํด๊ฒฐ ๋ฐฉ๋ฒ์ฒ๋ผ ๋ณด์ด์ง๋ง ๊ทธ๊ฐ ์ง์ ํ๋ฏ์ด ์์ ํ ์๋ฃจ์
์ ์๋๋๋ค.
์ด ๋ฌธ์ ์ ์ง์ ์ด ์์ต๋๊น?
์ ๊ณ ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค! ๊ฒฐ๊ตญ # 3482์์ ์์ ๋์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ๋ฌธ์ ์ ์ง์ ์ด ์์ต๋๊น?