mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 05:01:07 +00:00
Try to add hints about unrecognized field names (if they're in other api versions)
This commit is contained in:
@ -35,6 +35,8 @@ import com.kingsrook.qqq.api.model.APIVersion;
|
||||
import com.kingsrook.qqq.api.model.APIVersionRange;
|
||||
import com.kingsrook.qqq.api.model.actions.ApiFieldCustomValueMapper;
|
||||
import com.kingsrook.qqq.api.model.actions.GetTableApiFieldsInput;
|
||||
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaData;
|
||||
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaDataContainer;
|
||||
import com.kingsrook.qqq.api.model.metadata.fields.ApiFieldMetaData;
|
||||
import com.kingsrook.qqq.api.model.metadata.fields.ApiFieldMetaDataContainer;
|
||||
import com.kingsrook.qqq.api.model.metadata.tables.ApiAssociationMetaData;
|
||||
@ -289,7 +291,27 @@ public class QRecordApiAdapter
|
||||
|
||||
if(!unrecognizedFieldNames.isEmpty())
|
||||
{
|
||||
throw (new QBadRequestException("Request body contained " + unrecognizedFieldNames.size() + " unrecognized field name" + StringUtils.plural(unrecognizedFieldNames) + ": " + StringUtils.joinWithCommasAndAnd(unrecognizedFieldNames)));
|
||||
List<String> otherVersionHints = new ArrayList<>();
|
||||
try
|
||||
{
|
||||
for(String unrecognizedFieldName : unrecognizedFieldNames)
|
||||
{
|
||||
String hint = lookForFieldInOtherVersions(unrecognizedFieldName, tableName, apiName, apiVersion);
|
||||
if(hint != null)
|
||||
{
|
||||
otherVersionHints.add(hint);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
LOG.warn("Error looking for unrecognized field names in other api versions", e);
|
||||
}
|
||||
|
||||
throw (new QBadRequestException("Request body contained "
|
||||
+ (unrecognizedFieldNames.size() + " unrecognized field name" + StringUtils.plural(unrecognizedFieldNames) + ": " + StringUtils.joinWithCommasAndAnd(unrecognizedFieldNames) + ". ")
|
||||
+ (CollectionUtils.nullSafeIsEmpty(otherVersionHints) ? "" : StringUtils.join(" ", otherVersionHints))
|
||||
));
|
||||
}
|
||||
|
||||
return (qRecord);
|
||||
@ -297,6 +319,37 @@ public class QRecordApiAdapter
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static String lookForFieldInOtherVersions(String unrecognizedFieldName, String tableName, String apiName, String apiVersion) throws QException
|
||||
{
|
||||
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(QContext.getQInstance());
|
||||
ApiInstanceMetaData apiInstanceMetaData = apiInstanceMetaDataContainer.getApiInstanceMetaData(apiName);
|
||||
|
||||
List<String> versionsWithThisField = new ArrayList<>();
|
||||
for(APIVersion supportedVersion : apiInstanceMetaData.getSupportedVersions())
|
||||
{
|
||||
if(!supportedVersion.toString().equals(apiVersion))
|
||||
{
|
||||
Map<String, QFieldMetaData> versionFields = getTableApiFieldMap(new ApiNameVersionAndTableName(apiName, supportedVersion.toString(), tableName));
|
||||
if(versionFields.containsKey(unrecognizedFieldName))
|
||||
{
|
||||
versionsWithThisField.add(supportedVersion.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(CollectionUtils.nullSafeHasContents(versionsWithThisField))
|
||||
{
|
||||
return (unrecognizedFieldName + " does not exist in version " + apiVersion + ", but does exist in versions: " + StringUtils.joinWithCommasAndAnd(versionsWithThisField) + ". ");
|
||||
}
|
||||
|
||||
return (null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -158,7 +158,8 @@ class QRecordApiAdapterTest extends BaseTest
|
||||
{"firstName": "Tim", "noOfShoes": 2}
|
||||
"""), TestUtils.TABLE_NAME_PERSON, TestUtils.API_NAME, TestUtils.V2022_Q4, true))
|
||||
.isInstanceOf(QBadRequestException.class)
|
||||
.hasMessageContaining("unrecognized field name: noOfShoes");
|
||||
.hasMessageContaining("unrecognized field name: noOfShoes")
|
||||
.hasMessageContaining("noOfShoes does not exist in version 2022.Q4, but does exist in versions: 2023.Q1");
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// current version doesn't have cost field - fail if you send it to us //
|
||||
@ -167,7 +168,8 @@ class QRecordApiAdapterTest extends BaseTest
|
||||
{"firstName": "Tim", "cost": 2}
|
||||
"""), TestUtils.TABLE_NAME_PERSON, TestUtils.API_NAME, TestUtils.V2023_Q1, true))
|
||||
.isInstanceOf(QBadRequestException.class)
|
||||
.hasMessageContaining("unrecognized field name: cost");
|
||||
.hasMessageContaining("unrecognized field name: cost")
|
||||
.hasMessageNotContaining("cost does not exist in version 2023.Q1, but does exist in versions: 2023.Q2"); // this field only appears in a future version, not any current/supported versions.
|
||||
|
||||
/////////////////////////////////
|
||||
// excluded field always fails //
|
||||
@ -178,7 +180,8 @@ class QRecordApiAdapterTest extends BaseTest
|
||||
{"firstName": "Tim", "price": 2}
|
||||
"""), TestUtils.TABLE_NAME_PERSON, TestUtils.API_NAME, version, true))
|
||||
.isInstanceOf(QBadRequestException.class)
|
||||
.hasMessageContaining("unrecognized field name: price");
|
||||
.hasMessageContaining("unrecognized field name: price")
|
||||
.hasMessageNotContaining("price does not exist in version"); // this field never appears, so no message about when it appears.
|
||||
}
|
||||
|
||||
////////////////////////////////////////////
|
||||
|
Reference in New Issue
Block a user