- 샘플 생성 로직 리팩토링 - 길이 및 소수 자릿수 기준 상세화 - 공통 스키마 처리 함수 추가 - 코드 중복 제거 및 가독성 향상
This commit is contained in:
@@ -343,7 +343,7 @@ public class ApiSpecManService {
|
||||
String defaultValue = item.getLoutItemDefault();
|
||||
String fieldName = item.getLoutItemName();
|
||||
|
||||
boolean isInteger = isIntegerType(dataType);
|
||||
boolean isInteger = isIntegerType(dataType, item.getLoutItemDecimal());
|
||||
|
||||
if (isNumericType(dataType)) {
|
||||
if (StringUtils.isNotEmpty(defaultValue)) {
|
||||
@@ -393,7 +393,7 @@ public class ApiSpecManService {
|
||||
private void addScalarToArray(ArrayNode arrayNode, LayoutItemUI item) {
|
||||
String dataType = item.getLoutItemDataType();
|
||||
String defaultValue = item.getLoutItemDefault();
|
||||
boolean isInteger = isIntegerType(dataType);
|
||||
boolean isInteger = isIntegerType(dataType, item.getLoutItemDecimal());
|
||||
|
||||
if (isNumericType(dataType)) {
|
||||
try {
|
||||
@@ -416,23 +416,29 @@ public class ApiSpecManService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isIntegerType(String dataType) {
|
||||
/** BIGDECIMAL/DECIMAL 은 소수 자릿수가 0 이면 정수로 취급한다. */
|
||||
private boolean isIntegerType(String dataType, int decimalLen) {
|
||||
if (dataType == null) return false;
|
||||
switch (dataType.toUpperCase()) {
|
||||
case "INTEGER":
|
||||
case "INT":
|
||||
case "LONG":
|
||||
return true;
|
||||
case "DECIMAL":
|
||||
case "BIGDECIMAL":
|
||||
return decimalLen <= 0;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNumericType(String dataType) {
|
||||
if (isIntegerType(dataType)) return true;
|
||||
if (dataType == null) return false;
|
||||
|
||||
switch (dataType.toUpperCase()) {
|
||||
case "INTEGER":
|
||||
case "INT":
|
||||
case "LONG":
|
||||
case "DECIMAL":
|
||||
case "DOUBLE":
|
||||
case "BIGDECIMAL":
|
||||
@@ -621,11 +627,7 @@ public class ApiSpecManService {
|
||||
parameter.put("required", false); // 기본값으로 false 설정, 필요시 true로 변경 가능
|
||||
|
||||
ObjectNode schema = parameter.putObject("schema");
|
||||
schema.put("type", mapSwaggerType(item.getLoutItemDataType()));
|
||||
int length = item.getLoutItemLength();
|
||||
if (length > 0) {
|
||||
schema.put("maxLength", item.getLoutItemLength());
|
||||
}
|
||||
putScalarSchema(schema, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -651,11 +653,7 @@ public class ApiSpecManService {
|
||||
parameter.put("required", false); // 기본값 false, 필요시 사용자가 변경
|
||||
|
||||
ObjectNode schema = parameter.putObject("schema");
|
||||
schema.put("type", mapSwaggerType(item.getLoutItemDataType()));
|
||||
int length = item.getLoutItemLength();
|
||||
if (length > 0) {
|
||||
schema.put("maxLength", length);
|
||||
}
|
||||
putScalarSchema(schema, item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -665,11 +663,7 @@ public class ApiSpecManService {
|
||||
ObjectNode header = headers.putObject(item.getLoutItemName());
|
||||
header.put("description", item.getLoutItemDesc());
|
||||
ObjectNode schema = header.putObject("schema");
|
||||
schema.put("type", mapSwaggerType(item.getLoutItemDataType()));
|
||||
int length = item.getLoutItemLength();
|
||||
if (length > 0) {
|
||||
schema.put("maxLength", item.getLoutItemLength());
|
||||
}
|
||||
putScalarSchema(schema, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -695,14 +689,10 @@ public class ApiSpecManService {
|
||||
switch (item.getLoutItemType().toUpperCase()) {
|
||||
case "FIELD":
|
||||
ObjectNode fieldSchema = parentNode.putObject(item.getLoutItemName());
|
||||
fieldSchema.put("type", mapSwaggerType(item.getLoutItemDataType()));
|
||||
putScalarSchema(fieldSchema, item);
|
||||
if (StringUtils.isNotEmpty(item.getLoutItemDesc())) {
|
||||
fieldSchema.put("description", item.getLoutItemDesc());
|
||||
}
|
||||
int length = item.getLoutItemLength();
|
||||
if (length > 0) {
|
||||
fieldSchema.put("maxLength", item.getLoutItemLength());
|
||||
}
|
||||
break;
|
||||
|
||||
case "GROUP":
|
||||
@@ -730,14 +720,18 @@ public class ApiSpecManService {
|
||||
arrItems.put("type", "object");
|
||||
depthMap.put(item.getLoutItemDepth(), arrItems.putObject("properties"));
|
||||
} else {
|
||||
arrItems.put("type", mapSwaggerType(item.getLoutItemDataType()));
|
||||
putScalarSchema(arrItems, item);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String mapSwaggerType(String dataType) {
|
||||
/**
|
||||
* 레이아웃 데이터타입 → OpenAPI type.
|
||||
* <p>BIGDECIMAL/DECIMAL 은 소수 자릿수(decimalLen)가 0 이면 정수로 취급한다.
|
||||
*/
|
||||
private String mapSwaggerType(String dataType, int decimalLen) {
|
||||
if (dataType == null) return "string";
|
||||
|
||||
switch (dataType.toUpperCase()) {
|
||||
@@ -747,8 +741,9 @@ public class ApiSpecManService {
|
||||
case "LONG":
|
||||
return "integer";
|
||||
case "DECIMAL":
|
||||
case "DOUBLE":
|
||||
case "BIGDECIMAL":
|
||||
return decimalLen > 0 ? "number" : "integer";
|
||||
case "DOUBLE":
|
||||
return "number";
|
||||
case "BOOLEAN":
|
||||
return "boolean";
|
||||
@@ -757,6 +752,23 @@ public class ApiSpecManService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 스칼라 FIELD 의 스키마(type/maxLength/소수 자릿수)를 채운다.
|
||||
* <p>길이(maxLength)는 레이아웃 길이 그대로이며, 소수가 있는 숫자의 예제값은
|
||||
* 소수점(.)을 포함해 이 길이를 넘지 않도록 프론트에서 생성한다(예: 길이 9/소수 5 → 123.45678).
|
||||
*/
|
||||
private void putScalarSchema(ObjectNode schema, LayoutItemUI item) {
|
||||
String type = mapSwaggerType(item.getLoutItemDataType(), item.getLoutItemDecimal());
|
||||
schema.put("type", type);
|
||||
int length = item.getLoutItemLength();
|
||||
if (length > 0) {
|
||||
schema.put("maxLength", length);
|
||||
}
|
||||
if ("number".equals(type) && item.getLoutItemDecimal() > 0) {
|
||||
schema.put("x-djb-decimal", item.getLoutItemDecimal());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRequestBodyRequired(String method) {
|
||||
if (method == null) return false;
|
||||
return "POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method);
|
||||
|
||||
+1
-1
@@ -45,7 +45,7 @@ public class DjbApiSpecLayoutMergeService {
|
||||
r.put("desc", it.getLoutItemDesc());
|
||||
r.put("itemType", it.getLoutItemType()); // FIELD / GROUP / GRID
|
||||
r.put("dataType", dataType);
|
||||
r.put("category", sampleGenerator.categoryOf(dataType));
|
||||
r.put("category", sampleGenerator.categoryOf(dataType, dec));
|
||||
r.put("length", len);
|
||||
r.put("decimal", dec);
|
||||
r.put("depth", it.getLoutItemDepth());
|
||||
|
||||
+59
-23
@@ -4,19 +4,25 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* D10 예제값 자동 생성 규칙.
|
||||
* D10 예제값 자동 생성 규칙. (프론트 app.js 의 _defScalar/_numSample 와 동일 규칙)
|
||||
* <ul>
|
||||
* <li>string → {@code sample_} + 항목명. 최대길이 초과 시 left-cut(앞에서 자름).</li>
|
||||
* <li>integer → {@code 1}</li>
|
||||
* <li>number → {@code 1.1} (소수 자릿수 지정 시 {@code 1.1000} 형태)</li>
|
||||
* <li>integer → {@code 123456} (길이 지정 시 그 길이에 맞춰 자릿수 축소)</li>
|
||||
* <li>number → 소수점(.)을 포함한 전체 길이 기준. 길이 9 / 소수 5 → {@code 123.45678}</li>
|
||||
* <li>boolean → {@code false}</li>
|
||||
* </ul>
|
||||
* <p>BIGDECIMAL/DECIMAL 은 소수 자릿수가 0 이면 정수(integer)로 분류한다.
|
||||
*/
|
||||
@Service
|
||||
public class DjbApiSpecSampleGenerator {
|
||||
|
||||
/** 원시 데이터타입(레이아웃 값)을 OpenAPI 계열 카테고리로 분류. */
|
||||
public String categoryOf(String rawDataType) {
|
||||
/**
|
||||
* 원시 데이터타입(레이아웃 값)을 OpenAPI 계열 카테고리로 분류.
|
||||
*
|
||||
* @param rawDataType 레이아웃 데이터타입(원시)
|
||||
* @param decimalLen 소수 자릿수. decimal 계열에서 0 이면 정수로 본다.
|
||||
*/
|
||||
public String categoryOf(String rawDataType, int decimalLen) {
|
||||
if (rawDataType == null) {
|
||||
return "string";
|
||||
}
|
||||
@@ -24,18 +30,22 @@ public class DjbApiSpecSampleGenerator {
|
||||
if (t.contains("bool")) {
|
||||
return "boolean";
|
||||
}
|
||||
if (t.contains("decimal") || t.contains("double") || t.contains("float") || t.contains("number")) {
|
||||
return "number";
|
||||
}
|
||||
if (t.contains("int") || t.contains("long")) {
|
||||
return "integer";
|
||||
}
|
||||
if (t.contains("object") || t.contains("group")) {
|
||||
return "object";
|
||||
}
|
||||
if (t.contains("array") || t.contains("grid") || t.contains("list")) {
|
||||
return "array";
|
||||
}
|
||||
if (t.contains("decimal")) {
|
||||
// BigDecimal/Decimal: 소수 자릿수 0 = 정수
|
||||
return decimalLen > 0 ? "number" : "integer";
|
||||
}
|
||||
if (t.contains("double") || t.contains("float") || t.contains("number")) {
|
||||
return "number";
|
||||
}
|
||||
if (t.contains("int") || t.contains("long")) {
|
||||
return "integer";
|
||||
}
|
||||
return "string";
|
||||
}
|
||||
|
||||
@@ -43,22 +53,15 @@ public class DjbApiSpecSampleGenerator {
|
||||
* 예제값 생성.
|
||||
* @param rawDataType 레이아웃 데이터타입(원시)
|
||||
* @param fieldName 항목명
|
||||
* @param maxLen 최대 길이(0/음수면 무제한)
|
||||
* @param decimalLen 소수 자릿수(0이면 미적용)
|
||||
* @param maxLen 최대 길이(0/음수면 무제한). 숫자는 소수점(.)을 포함한 문자열 길이 기준.
|
||||
* @param decimalLen 소수 자릿수(0이면 정수)
|
||||
*/
|
||||
public String exampleFor(String rawDataType, String fieldName, int maxLen, int decimalLen) {
|
||||
switch (categoryOf(rawDataType)) {
|
||||
switch (categoryOf(rawDataType, decimalLen)) {
|
||||
case "integer":
|
||||
return "1";
|
||||
return numberSample(maxLen, 0);
|
||||
case "number":
|
||||
if (decimalLen > 0) {
|
||||
StringBuilder sb = new StringBuilder("1.");
|
||||
for (int i = 0; i < decimalLen; i++) {
|
||||
sb.append(i == 0 ? '1' : '0');
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
return "1.1";
|
||||
return numberSample(maxLen, decimalLen > 0 ? decimalLen : 3);
|
||||
case "boolean":
|
||||
return "false";
|
||||
case "object":
|
||||
@@ -72,4 +75,37 @@ public class DjbApiSpecSampleGenerator {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 숫자 예제값. 길이는 소수점(.)까지 포함한 문자열 전체 길이로 계산한다.
|
||||
* <p>예) 길이 9 / 소수 5 → {@code 123.45678} (정수부 3 + '.' 1 + 소수부 5 = 9자).
|
||||
* 길이가 소수부+2 보다 짧으면 정수부 1자리를 확보하고 소수부를 줄인다.
|
||||
* 길이 미지정이면 정수부 6자리를 사용한다.
|
||||
*/
|
||||
private String numberSample(int maxLen, int decimalLen) {
|
||||
if (decimalLen <= 0) {
|
||||
return digits(0, maxLen > 0 ? Math.min(maxLen, 6) : 6);
|
||||
}
|
||||
int dec = decimalLen;
|
||||
int intLen;
|
||||
if (maxLen > 0) {
|
||||
intLen = maxLen - dec - 1; // '.' 1자리 포함
|
||||
if (intLen < 1) {
|
||||
intLen = 1;
|
||||
dec = Math.max(1, maxLen - 2);
|
||||
}
|
||||
} else {
|
||||
intLen = 6;
|
||||
}
|
||||
return digits(0, intLen) + "." + digits(intLen, dec);
|
||||
}
|
||||
|
||||
/** start 위치부터 1~9 를 순환하는 자릿수 문자열('123456789123...'). */
|
||||
private String digits(int start, int count) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < count; i++) {
|
||||
sb.append((char) ('1' + ((start + i) % 9)));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user