From fe2f092e7f2e8d4f9a841cd9649016c3e8700623 Mon Sep 17 00:00:00 2001 From: Romain Pouclet Date: Mon, 20 Apr 2020 23:23:33 -0700 Subject: [PATCH] Expose deprecated flag in model template (#5964) --- .../openapitools/codegen/CodegenModel.java | 6 ++- .../openapitools/codegen/DefaultCodegen.java | 4 ++ .../codegen/DefaultCodegenTest.java | 13 +++++++ .../resources/3_0/component-deprecated.yml | 37 +++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/component-deprecated.yml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java index c0519355cd5..16a296f84e1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java @@ -74,7 +74,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties { public Set allMandatory = new TreeSet(); // with parent's required properties public Set imports = new TreeSet(); - public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel; + public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel, isDeprecated; public boolean hasOnlyReadOnly = true; // true if all properties are read-only public ExternalDocumentation externalDocumentation; @@ -543,6 +543,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties { isArrayModel == that.isArrayModel && hasChildren == that.hasChildren && isMapModel == that.isMapModel && + isDeprecated == that.isDeprecated && hasOnlyReadOnly == that.hasOnlyReadOnly && getUniqueItems() == that.getUniqueItems() && getExclusiveMinimum() == that.getExclusiveMinimum() && @@ -609,7 +610,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties { getVars(), getAllVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(), getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars, isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, - hasChildren, isMapModel, hasOnlyReadOnly, getExternalDocumentation(), getVendorExtensions(), + hasChildren, isMapModel, isDeprecated, hasOnlyReadOnly, getExternalDocumentation(), getVendorExtensions(), getAdditionalPropertiesType(), getMaxProperties(), getMinProperties(), getUniqueItems(), getMaxItems(), getMinItems(), getMaxLength(), getMinLength(), getExclusiveMinimum(), getExclusiveMaximum(), getMinimum(), getMaximum(), getPattern(), getMultipleOf()); @@ -673,6 +674,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties { sb.append(", isArrayModel=").append(isArrayModel); sb.append(", hasChildren=").append(hasChildren); sb.append(", isMapModel=").append(isMapModel); + sb.append(", isDeprecated=").append(isDeprecated); sb.append(", hasOnlyReadOnly=").append(hasOnlyReadOnly); sb.append(", externalDocumentation=").append(externalDocumentation); sb.append(", vendorExtensions=").append(vendorExtensions); 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 e6a75981b7f..f0906626eea 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 @@ -2117,6 +2117,10 @@ public class DefaultCodegen implements CodegenConfig { || isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types m.discriminator = createDiscriminator(name, schema); + if (schema.getDeprecated() != null) { + m.isDeprecated = schema.getDeprecated(); + } + if (schema.getXml() != null) { m.xmlPrefix = schema.getXml().getPrefix(); m.xmlNamespace = schema.getXml().getNamespace(); 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 06007048a76..d43f7062e23 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 @@ -805,6 +805,19 @@ public class DefaultCodegenTest { Assert.assertTrue(property.isNullable); } + @Test + public void testDeprecatedModel() { + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/component-deprecated.yml"); + new InlineModelResolver().flatten(openAPI); + final DefaultCodegen codegen = new DefaultCodegen(); + + CodegenModel codedenPetModel = codegen.fromModel("Pet", openAPI.getComponents().getSchemas().get("Pet")); + Assert.assertTrue(codedenPetModel.isDeprecated); + + CodegenModel codegenFoodModel = codegen.fromModel("Food", openAPI.getComponents().getSchemas().get("Food")); + Assert.assertTrue(codegenFoodModel.isDeprecated); + } + @Test public void testDeprecatedProperty() { final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/property-deplicated.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_0/component-deprecated.yml b/modules/openapi-generator/src/test/resources/3_0/component-deprecated.yml new file mode 100644 index 00000000000..4c1c5000be9 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/component-deprecated.yml @@ -0,0 +1,37 @@ +openapi: 3.0.1 +info: + version: 1.0.0 + title: Example + license: + name: MIT +servers: + - url: http://api.example.xyz/v1 + +components: + schemas: + Food: + deprecated: true + type: string + enum: + - dry + - wet + + Pet: + title: a Pet + deprecated: true + description: A pet up for adoption + type: object + required: + - name + - status + properties: + name: + type: string + example: doggie + status: + type: string + description: pet status + enum: + - available + - pending + - adopted \ No newline at end of file