Use warning instead of throwing exceptions (#9188)

* use warning instead of throwing exceptions

* comment out tests
This commit is contained in:
William Cheng 2021-04-13 14:09:19 +08:00 committed by GitHub
parent 1f227061c6
commit 13c0b2c3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -2702,14 +2702,14 @@ public class DefaultCodegen implements CodegenConfig {
String modelName = ModelUtils.getSimpleRef(oneOf.get$ref()); String modelName = ModelUtils.getSimpleRef(oneOf.get$ref());
CodegenProperty thisCp = discriminatorFound(composedSchemaName, oneOf, discPropName, openAPI); CodegenProperty thisCp = discriminatorFound(composedSchemaName, oneOf, discPropName, openAPI);
if (thisCp == null) { if (thisCp == null) {
throw new RuntimeException("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the referenced OneOf schema '" + modelName + "' is missing " + discPropName); LOGGER.warn("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the referenced OneOf schema '" + modelName + "' is missing " + discPropName);
} }
if (cp.dataType == null) { if (cp.dataType == null) {
cp = thisCp; cp = thisCp;
continue; continue;
} }
if (cp != thisCp) { if (cp != thisCp) {
throw new RuntimeException("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the OneOf schema '" + modelName + "' has a different " + discPropName + " definition than the prior OneOf schema's. Make sure the " + discPropName + " type and required values are the same"); LOGGER.warn("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the OneOf schema '" + modelName + "' has a different " + discPropName + " definition than the prior OneOf schema's. Make sure the " + discPropName + " type and required values are the same");
} }
} }
return cp; return cp;
@ -2721,14 +2721,14 @@ public class DefaultCodegen implements CodegenConfig {
String modelName = ModelUtils.getSimpleRef(anyOf.get$ref()); String modelName = ModelUtils.getSimpleRef(anyOf.get$ref());
CodegenProperty thisCp = discriminatorFound(composedSchemaName, anyOf, discPropName, openAPI); CodegenProperty thisCp = discriminatorFound(composedSchemaName, anyOf, discPropName, openAPI);
if (thisCp == null) { if (thisCp == null) {
throw new RuntimeException("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the referenced AnyOf schema '" + modelName + "' is missing " + discPropName); LOGGER.warn("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the referenced AnyOf schema '" + modelName + "' is missing " + discPropName);
} }
if (cp.dataType == null) { if (cp.dataType == null) {
cp = thisCp; cp = thisCp;
continue; continue;
} }
if (cp != thisCp) { if (cp != thisCp) {
throw new RuntimeException("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the AnyOf schema '" + modelName + "' has a different " + discPropName + " definition than the prior AnyOf schema's. Make sure the " + discPropName + " type and required values are the same"); LOGGER.warn("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the AnyOf schema '" + modelName + "' has a different " + discPropName + " definition than the prior AnyOf schema's. Make sure the " + discPropName + " type and required values are the same");
} }
} }
return cp; return cp;
@ -2787,7 +2787,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (discriminatorsPropNames.size() > 1) { if (discriminatorsPropNames.size() > 1) {
throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " + LOGGER.warn("The oneOf schemas have conflicting discriminator property names. " +
"oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
} }
if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) { if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) {
@ -2816,7 +2816,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
} }
if (discriminatorsPropNames.size() > 1) { if (discriminatorsPropNames.size() > 1) {
throw new RuntimeException("The anyOf schemas have conflicting discriminator property names. " + LOGGER.warn("The anyOf schemas have conflicting discriminator property names. " +
"anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); "anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
} }
if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) { if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) {
@ -2866,7 +2866,7 @@ public class DefaultCodegen implements CodegenConfig {
// schemas also has inline composed schemas // schemas also has inline composed schemas
// Note: if it is only inline one level, then the inline model resolver will move it into its own // Note: if it is only inline one level, then the inline model resolver will move it into its own
// schema and make it a $ref schema in the oneOf/anyOf location // schema and make it a $ref schema in the oneOf/anyOf location
throw new RuntimeException("Invalid inline schema defined in oneOf/anyOf in '" + composedSchemaName + "'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the oneOf/anyOf schemas must use $ref. Change this inline definition to a $ref definition"); LOGGER.warn("Invalid inline schema defined in oneOf/anyOf in '" + composedSchemaName + "'. Per the OpenApi spec, for this case when a composed schema defines a discriminator, the oneOf/anyOf schemas must use $ref. Change this inline definition to a $ref definition");
} }
CodegenProperty df = discriminatorFound(composedSchemaName, sc, discPropName, openAPI); CodegenProperty df = discriminatorFound(composedSchemaName, sc, discPropName, openAPI);
String modelName = ModelUtils.getSimpleRef(ref); String modelName = ModelUtils.getSimpleRef(ref);
@ -2886,7 +2886,7 @@ public class DefaultCodegen implements CodegenConfig {
msgSuffix += spacer + "invalid optional definition of " + discPropName + ", include it in required"; msgSuffix += spacer + "invalid optional definition of " + discPropName + ", include it in required";
} }
} }
throw new RuntimeException("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the referenced schema '" + modelName + "' is incorrect. " + msgSuffix); LOGGER.warn("'" + composedSchemaName + "' defines discriminator '" + discPropName + "', but the referenced schema '" + modelName + "' is incorrect. " + msgSuffix);
} }
MappedModel mm = new MappedModel(modelName, toModelName(modelName)); MappedModel mm = new MappedModel(modelName, toModelName(modelName));
descendentSchemas.add(mm); descendentSchemas.add(mm);

View File

@ -1188,12 +1188,15 @@ public class DefaultCodegenTest {
Schema sc = openAPI.getComponents().getSchemas().get(modelName); Schema sc = openAPI.getComponents().getSchemas().get(modelName);
/*
// comment out below as we're now showing warnings instead of throwing exceptions
try { try {
codegen.fromModel(modelName, sc); codegen.fromModel(modelName, sc);
Assert.assertTrue(false, "A RuntimeException should have been thrown when processing "+modelName+ " but it was not"); Assert.assertTrue(false, "A RuntimeException should have been thrown when processing "+modelName+ " but it was not");
} catch (RuntimeException re) { } catch (RuntimeException re) {
Assert.assertEquals(re.getMessage(), errorMessageExpected); Assert.assertEquals(re.getMessage(), errorMessageExpected);
} }
*/
} }
} }
@ -1220,12 +1223,15 @@ public class DefaultCodegenTest {
Schema sc = openAPI.getComponents().getSchemas().get(modelName); Schema sc = openAPI.getComponents().getSchemas().get(modelName);
/*
// comment out below as we're now showing warnings instead of throwing exceptions
try { try {
codegen.fromModel(modelName, sc); codegen.fromModel(modelName, sc);
Assert.assertTrue(false, "A RuntimeException should have been thrown when processing "+modelName+ " but it was not"); Assert.assertTrue(false, "A RuntimeException should have been thrown when processing "+modelName+ " but it was not");
} catch (RuntimeException re) { } catch (RuntimeException re) {
Assert.assertEquals(re.getMessage(), errorMessageExpected); Assert.assertEquals(re.getMessage(), errorMessageExpected);
} }
*/
} }
} }
@ -1256,7 +1262,8 @@ public class DefaultCodegenTest {
// inline anyOf with inline anyOf model doesn't work because we have null $refs and we throw an exception // inline anyOf with inline anyOf model doesn't work because we have null $refs and we throw an exception
final String fmodelName = "FruitInlineInlineDisc"; final String fmodelName = "FruitInlineInlineDisc";
final Schema fsc = openAPI.getComponents().getSchemas().get(fmodelName); final Schema fsc = openAPI.getComponents().getSchemas().get(fmodelName);
Assert.assertThrows(() -> codegen.fromModel(fmodelName, fsc)); // comment out below as we're now showing warnings instead of throwing exceptions
//Assert.assertThrows(() -> codegen.fromModel(fmodelName, fsc));
// ref anyOf models with discriminator in properties in those models // ref anyOf models with discriminator in properties in those models
modelName = "FruitReqDisc"; modelName = "FruitReqDisc";
@ -1341,7 +1348,8 @@ public class DefaultCodegenTest {
// inline oneOf with inline oneOf model doesn't work because we have null $refs and we throw an exception // inline oneOf with inline oneOf model doesn't work because we have null $refs and we throw an exception
final String fmodelName = "FruitInlineInlineDisc"; final String fmodelName = "FruitInlineInlineDisc";
final Schema fsc = openAPI.getComponents().getSchemas().get(fmodelName); final Schema fsc = openAPI.getComponents().getSchemas().get(fmodelName);
Assert.assertThrows(() -> codegen.fromModel(fmodelName, fsc)); // comment out below as we're now showing warnings instead of throwing exceptions
//Assert.assertThrows(() -> codegen.fromModel(fmodelName, fsc));
// ref oneOf models with discriminator in properties in those models // ref oneOf models with discriminator in properties in those models
modelName = "FruitReqDisc"; modelName = "FruitReqDisc";