Skip to content

Commit 93aa3d7

Browse files
PalleasMikailBag
authored andcommitted
Expose deprecated flag in model template (OpenAPITools#5964)
1 parent 7b6e2bf commit 93aa3d7

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
7474
public Set<String> allMandatory = new TreeSet<String>(); // with parent's required properties
7575

7676
public Set<String> imports = new TreeSet<String>();
77-
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel;
77+
public boolean hasVars, emptyVars, hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel, hasChildren, isMapModel, isDeprecated;
7878
public boolean hasOnlyReadOnly = true; // true if all properties are read-only
7979
public ExternalDocumentation externalDocumentation;
8080

@@ -543,6 +543,7 @@ public boolean equals(Object o) {
543543
isArrayModel == that.isArrayModel &&
544544
hasChildren == that.hasChildren &&
545545
isMapModel == that.isMapModel &&
546+
isDeprecated == that.isDeprecated &&
546547
hasOnlyReadOnly == that.hasOnlyReadOnly &&
547548
getUniqueItems() == that.getUniqueItems() &&
548549
getExclusiveMinimum() == that.getExclusiveMinimum() &&
@@ -609,7 +610,7 @@ public int hashCode() {
609610
getVars(), getAllVars(), getRequiredVars(), getOptionalVars(), getReadOnlyVars(), getReadWriteVars(),
610611
getParentVars(), getAllowableValues(), getMandatory(), getAllMandatory(), getImports(), hasVars,
611612
isEmptyVars(), hasMoreModels, hasEnums, isEnum, isNullable, hasRequired, hasOptional, isArrayModel,
612-
hasChildren, isMapModel, hasOnlyReadOnly, getExternalDocumentation(), getVendorExtensions(),
613+
hasChildren, isMapModel, isDeprecated, hasOnlyReadOnly, getExternalDocumentation(), getVendorExtensions(),
613614
getAdditionalPropertiesType(), getMaxProperties(), getMinProperties(), getUniqueItems(), getMaxItems(),
614615
getMinItems(), getMaxLength(), getMinLength(), getExclusiveMinimum(), getExclusiveMaximum(), getMinimum(),
615616
getMaximum(), getPattern(), getMultipleOf());
@@ -673,6 +674,7 @@ public String toString() {
673674
sb.append(", isArrayModel=").append(isArrayModel);
674675
sb.append(", hasChildren=").append(hasChildren);
675676
sb.append(", isMapModel=").append(isMapModel);
677+
sb.append(", isDeprecated=").append(isDeprecated);
676678
sb.append(", hasOnlyReadOnly=").append(hasOnlyReadOnly);
677679
sb.append(", externalDocumentation=").append(externalDocumentation);
678680
sb.append(", vendorExtensions=").append(vendorExtensions);

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,10 @@ public CodegenModel fromModel(String name, Schema schema) {
21172117
|| isAliasOfSimpleTypes(schema)); // check if the unaliased schema is an alias of simple OAS types
21182118
m.discriminator = createDiscriminator(name, schema);
21192119

2120+
if (schema.getDeprecated() != null) {
2121+
m.isDeprecated = schema.getDeprecated();
2122+
}
2123+
21202124
if (schema.getXml() != null) {
21212125
m.xmlPrefix = schema.getXml().getPrefix();
21222126
m.xmlNamespace = schema.getXml().getNamespace();

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,19 @@ public void testNullableProperty() {
805805
Assert.assertTrue(property.isNullable);
806806
}
807807

808+
@Test
809+
public void testDeprecatedModel() {
810+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/component-deprecated.yml");
811+
new InlineModelResolver().flatten(openAPI);
812+
final DefaultCodegen codegen = new DefaultCodegen();
813+
814+
CodegenModel codedenPetModel = codegen.fromModel("Pet", openAPI.getComponents().getSchemas().get("Pet"));
815+
Assert.assertTrue(codedenPetModel.isDeprecated);
816+
817+
CodegenModel codegenFoodModel = codegen.fromModel("Food", openAPI.getComponents().getSchemas().get("Food"));
818+
Assert.assertTrue(codegenFoodModel.isDeprecated);
819+
}
820+
808821
@Test
809822
public void testDeprecatedProperty() {
810823
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/property-deplicated.yaml");
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
openapi: 3.0.1
2+
info:
3+
version: 1.0.0
4+
title: Example
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://api.example.xyz/v1
9+
10+
components:
11+
schemas:
12+
Food:
13+
deprecated: true
14+
type: string
15+
enum:
16+
- dry
17+
- wet
18+
19+
Pet:
20+
title: a Pet
21+
deprecated: true
22+
description: A pet up for adoption
23+
type: object
24+
required:
25+
- name
26+
- status
27+
properties:
28+
name:
29+
type: string
30+
example: doggie
31+
status:
32+
type: string
33+
description: pet status
34+
enum:
35+
- available
36+
- pending
37+
- adopted

0 commit comments

Comments
 (0)