From aeb29a6a6cfb2e2aeaf279ffa0d4231ffb9feb60 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 6 Apr 2021 18:06:21 +0800 Subject: [PATCH 1/2] use warning instead of throwing exceptions --- .../org/openapitools/codegen/DefaultCodegen.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 590e925a1422..4993420c5e3b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -2702,14 +2702,14 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, String modelName = ModelUtils.getSimpleRef(oneOf.get$ref()); CodegenProperty thisCp = discriminatorFound(composedSchemaName, oneOf, discPropName, openAPI); 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) { cp = thisCp; continue; } 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; @@ -2721,14 +2721,14 @@ private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, String modelName = ModelUtils.getSimpleRef(anyOf.get$ref()); CodegenProperty thisCp = discriminatorFound(composedSchemaName, anyOf, discPropName, openAPI); 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) { cp = thisCp; continue; } 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; @@ -2787,7 +2787,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } } 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)); } if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) { @@ -2816,7 +2816,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } } 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)); } if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) { @@ -2866,7 +2866,7 @@ protected List getOneOfAnyOfDescendants(String composedSchemaName, // 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 // 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); String modelName = ModelUtils.getSimpleRef(ref); @@ -2886,7 +2886,7 @@ protected List getOneOfAnyOfDescendants(String composedSchemaName, 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)); descendentSchemas.add(mm); From c7bed846d5570ab9b8ccff4916d867af4528e8a3 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 12 Apr 2021 16:09:40 +0800 Subject: [PATCH 2/2] comment out tests --- .../org/openapitools/codegen/DefaultCodegenTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index c2c5b2a04038..18c834f5fb2c 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -1188,12 +1188,15 @@ public void testComposedSchemaOneOfDiscriminatorsInvalid() { Schema sc = openAPI.getComponents().getSchemas().get(modelName); + /* + // comment out below as we're now showing warnings instead of throwing exceptions try { codegen.fromModel(modelName, sc); Assert.assertTrue(false, "A RuntimeException should have been thrown when processing "+modelName+ " but it was not"); } catch (RuntimeException re) { Assert.assertEquals(re.getMessage(), errorMessageExpected); } + */ } } @@ -1220,12 +1223,15 @@ public void testComposedSchemaAnyOfDiscriminatorsInvalid() { Schema sc = openAPI.getComponents().getSchemas().get(modelName); + /* + // comment out below as we're now showing warnings instead of throwing exceptions try { codegen.fromModel(modelName, sc); Assert.assertTrue(false, "A RuntimeException should have been thrown when processing "+modelName+ " but it was not"); } catch (RuntimeException re) { Assert.assertEquals(re.getMessage(), errorMessageExpected); } + */ } } @@ -1256,7 +1262,8 @@ public void testComposedSchemaAnyOfDiscriminatorMap() { // inline anyOf with inline anyOf model doesn't work because we have null $refs and we throw an exception final String fmodelName = "FruitInlineInlineDisc"; 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 modelName = "FruitReqDisc"; @@ -1341,7 +1348,8 @@ public void testComposedSchemaOneOfDiscriminatorMap() { // inline oneOf with inline oneOf model doesn't work because we have null $refs and we throw an exception final String fmodelName = "FruitInlineInlineDisc"; 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 modelName = "FruitReqDisc";