Tolerate missing blueprint request fields
This commit is contained in:
@@ -296,6 +296,7 @@ public class AiGenerateServiceImpl implements IAiGenerateService
|
||||
return;
|
||||
}
|
||||
Set<String> actionCodes = new HashSet<String>();
|
||||
Set<String> columnNames = collectColumnNames(response);
|
||||
int actionIndex = 1;
|
||||
for (BusinessActionDesign action : response.getBusinessActions())
|
||||
{
|
||||
@@ -310,7 +311,7 @@ public class AiGenerateServiceImpl implements IAiGenerateService
|
||||
action.setMethod(normalizeHttpMethod(action.getMethod()));
|
||||
action.setPath(normalizeBusinessPath(action.getPath(), code));
|
||||
action.setTransaction(action.getTransaction() == null ? Boolean.TRUE : action.getTransaction());
|
||||
action.setRequestFields(normalizeFieldList(action.getRequestFields()));
|
||||
action.setRequestFields(normalizeExistingFieldList(action.getRequestFields(), columnNames));
|
||||
action.setRules(trimStringList(action.getRules(), 200));
|
||||
normalizeBusinessEffects(action);
|
||||
actionIndex++;
|
||||
@@ -361,7 +362,7 @@ public class AiGenerateServiceImpl implements IAiGenerateService
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private List<String> normalizeFieldList(List<String> fields)
|
||||
private List<String> normalizeExistingFieldList(List<String> fields, Set<String> columnNames)
|
||||
{
|
||||
List<String> normalizedFields = new ArrayList<String>();
|
||||
if (fields == null)
|
||||
@@ -370,11 +371,39 @@ public class AiGenerateServiceImpl implements IAiGenerateService
|
||||
}
|
||||
for (int i = 0; i < fields.size(); i++)
|
||||
{
|
||||
normalizedFields.add(normalizeDatabaseName(fields.get(i), "field_" + (i + 1)));
|
||||
String fieldName = normalizeDatabaseName(fields.get(i), "field_" + (i + 1));
|
||||
if (columnNames.contains(fieldName))
|
||||
{
|
||||
normalizedFields.add(fieldName);
|
||||
}
|
||||
}
|
||||
return normalizedFields;
|
||||
}
|
||||
|
||||
private Set<String> collectColumnNames(DatabaseDesignResponse response)
|
||||
{
|
||||
Set<String> columnNames = new HashSet<String>();
|
||||
if (response == null || response.getTables() == null)
|
||||
{
|
||||
return columnNames;
|
||||
}
|
||||
for (DatabaseTableDesign table : response.getTables())
|
||||
{
|
||||
if (table == null || table.getColumns() == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for (DatabaseColumnDesign column : table.getColumns())
|
||||
{
|
||||
if (column != null && StringUtils.isNotBlank(column.getColumnName()))
|
||||
{
|
||||
columnNames.add(column.getColumnName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return columnNames;
|
||||
}
|
||||
|
||||
private List<String> trimStringList(List<String> values, int maxLength)
|
||||
{
|
||||
List<String> normalizedValues = new ArrayList<String>();
|
||||
|
||||
@@ -239,6 +239,22 @@ public class AiGenerateServiceImplTest
|
||||
assertTrue(projectCaptor.getValue().getBusinessBlueprint().contains("borrow_book"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDatabaseDropsMissingBusinessActionRequestFields()
|
||||
{
|
||||
when(frontProjectMapper.selectFrontProjectByUserAndId(7L, 10L)).thenReturn(project());
|
||||
when(deepSeekClient.chat(anyString())).thenReturn(responseWithMissingBusinessActionRequestField());
|
||||
|
||||
DatabaseDesignResponse response = service.generateDatabase(7L, 10L, request());
|
||||
|
||||
List<String> requestFields = response.getBusinessActions().get(0).getRequestFields();
|
||||
assertEquals(1, requestFields.size());
|
||||
assertEquals("reader_id", requestFields.get(0));
|
||||
ArgumentCaptor<FrontProject> projectCaptor = ArgumentCaptor.forClass(FrontProject.class);
|
||||
verify(frontProjectMapper).updateFrontProject(projectCaptor.capture());
|
||||
assertFalse(projectCaptor.getValue().getBusinessBlueprint().contains("fine_record_id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDatabaseRejectsBusinessActionReferencingMissingTable()
|
||||
{
|
||||
@@ -513,6 +529,22 @@ public class AiGenerateServiceImplTest
|
||||
+ "}";
|
||||
}
|
||||
|
||||
private String responseWithMissingBusinessActionRequestField()
|
||||
{
|
||||
return "{\n"
|
||||
+ " \"tables\": [\n"
|
||||
+ " {\"tableName\": \"fine_record\", \"tableComment\": \"罚金记录表\", \"columns\": [\n"
|
||||
+ " {\"columnName\": \"id\", \"columnType\": \"bigint(20)\", \"primaryKey\": true, \"autoIncrement\": true, \"columnComment\": \"ID\"},\n"
|
||||
+ " {\"columnName\": \"reader_id\", \"columnType\": \"bigint(20)\", \"columnComment\": \"读者ID\"},\n"
|
||||
+ " {\"columnName\": \"fine_amount\", \"columnType\": \"decimal(10,2)\", \"columnComment\": \"罚金金额\"}\n"
|
||||
+ " ]}\n"
|
||||
+ " ],\n"
|
||||
+ " \"businessActions\": [\n"
|
||||
+ " {\"code\": \"pay_fine\", \"name\": \"缴纳罚金\", \"ownerTable\": \"fine_record\", \"method\": \"POST\", \"path\": \"/library/fine/pay\", \"requestFields\": [\"fine_record_id\", \"reader_id\"]}\n"
|
||||
+ " ]\n"
|
||||
+ "}";
|
||||
}
|
||||
|
||||
private String responseWithMissingBusinessActionTable()
|
||||
{
|
||||
return "{\n"
|
||||
|
||||
Reference in New Issue
Block a user