Skip to content

Commit 8214460

Browse files
authored
Add deprecated annotation in kotlin-spring (#5090)
* add Deprecated in kotlin dataClass * add deprecated in CodegenProperty * format (Column limit: 100) * set property.deprecated * add test * run ./bin/kotlin-springboot-petstore-all.sh * trim space
1 parent 1ac0f14 commit 8214460

7 files changed

Lines changed: 88 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
5454
public boolean exclusiveMaximum;
5555
public boolean hasMore;
5656
public boolean required;
57+
public boolean deprecated;
5758
public boolean secondaryParam;
5859
public boolean hasMoreNonReadOnly; // for model constructor, true if next property is not readonly
5960
public boolean isPrimitiveType;
@@ -554,6 +555,7 @@ public String toString() {
554555
sb.append(", exclusiveMaximum=").append(exclusiveMaximum);
555556
sb.append(", hasMore=").append(hasMore);
556557
sb.append(", required=").append(required);
558+
sb.append(", deprecated=").append(deprecated);
557559
sb.append(", secondaryParam=").append(secondaryParam);
558560
sb.append(", hasMoreNonReadOnly=").append(hasMoreNonReadOnly);
559561
sb.append(", isPrimitiveType=").append(isPrimitiveType);
@@ -619,6 +621,7 @@ public boolean equals(Object o) {
619621
exclusiveMaximum == that.exclusiveMaximum &&
620622
hasMore == that.hasMore &&
621623
required == that.required &&
624+
deprecated == this.deprecated &&
622625
secondaryParam == that.secondaryParam &&
623626
hasMoreNonReadOnly == that.hasMoreNonReadOnly &&
624627
isPrimitiveType == that.isPrimitiveType &&
@@ -698,16 +701,18 @@ public boolean equals(Object o) {
698701
@Override
699702
public int hashCode() {
700703

701-
return Objects.hash(openApiType, baseName, complexType, getter, setter, description, dataType,
702-
datatypeWithEnum, dataFormat, name, min, max, defaultValue, defaultValueWithParam, baseType,
703-
containerType, title, unescapedDescription, maxLength, minLength, pattern, example, jsonSchema,
704-
minimum, maximum, exclusiveMinimum, exclusiveMaximum, hasMore, required, secondaryParam,
705-
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric, isInteger,
706-
isLong, isNumber, isFloat, isDouble, isByteArray, isBinary, isFile, isBoolean, isDate, isDateTime,
707-
isUuid, isUri, isEmail, isFreeFormObject, isListContainer, isMapContainer, isEnum, isReadOnly,
708-
isWriteOnly, isNullable, isSelfReference, isCircularReference, _enum, allowableValues, items,
709-
mostInnerItems, vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
710-
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName, xmlNamespace,
711-
isXmlWrapped, multipleOf);
704+
return Objects.hash(openApiType, baseName, complexType, getter, setter, description,
705+
dataType, datatypeWithEnum, dataFormat, name, min, max, defaultValue,
706+
defaultValueWithParam, baseType, containerType, title, unescapedDescription,
707+
maxLength, minLength, pattern, example, jsonSchema, minimum, maximum,
708+
exclusiveMinimum, exclusiveMaximum, hasMore, required, deprecated, secondaryParam,
709+
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric,
710+
isInteger, isLong, isNumber, isFloat, isDouble, isByteArray, isBinary, isFile,
711+
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject,
712+
isListContainer, isMapContainer, isEnum, isReadOnly, isWriteOnly, isNullable,
713+
isSelfReference, isCircularReference, _enum, allowableValues, items, mostInnerItems,
714+
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
715+
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
716+
xmlNamespace, isXmlWrapped);
712717
}
713718
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,10 @@ public CodegenProperty fromProperty(String name, Schema p) {
22552255
property.defaultValue = toDefaultValue(p);
22562256
property.defaultValueWithParam = toDefaultValueWithParam(name, p);
22572257
property.jsonSchema = Json.pretty(p);
2258+
2259+
if (p.getDeprecated() != null) {
2260+
property.deprecated = p.getDeprecated();
2261+
}
22582262
if (p.getReadOnly() != null) {
22592263
property.isReadOnly = p.getReadOnly();
22602264
}
@@ -5477,4 +5481,4 @@ public FeatureSet getFeatureSet() {
54775481
public void setFeatureSet(final FeatureSet featureSet) {
54785482
this.featureSet = featureSet == null ? DefaultFeatureSet : featureSet;
54795483
}
5480-
}
5484+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{#useBeanValidation}}{{#required}}
22
{{^isReadOnly}}@get:NotNull{{/isReadOnly}} {{/required}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
3-
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
3+
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}{{#deprecated}}
4+
@Deprecated(message=""){{/deprecated}}
45
@JsonProperty("{{{baseName}}}"){{#isInherited}} override{{/isInherited}} {{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isListContainer}}{{baseType}}<{{/isListContainer}}{{classname}}.{{nameInCamelCase}}{{#isListContainer}}>{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}? = {{#defaultvalue}}{{defaultvalue}}{{/defaultvalue}}{{^defaultvalue}}null{{/defaultvalue}}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,22 @@ public void testNullableProperty() {
678678
Assert.assertTrue(property.isNullable);
679679
}
680680

681+
@Test
682+
public void testDeprecatedProperty() {
683+
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/property-deplicated.yaml");
684+
new InlineModelResolver().flatten(openAPI);
685+
final DefaultCodegen codegen = new DefaultCodegen();
686+
codegen.setOpenAPI(openAPI);
687+
688+
final Map responseProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("Response").getProperties());
689+
final Map requestProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("Response").getProperties());
690+
691+
Assert.assertTrue(codegen.fromProperty("firstName",(Schema) responseProperties.get("firstName")).deprecated);
692+
Assert.assertFalse(codegen.fromProperty("customerCode",(Schema) responseProperties.get("customerCode")).deprecated);
693+
Assert.assertTrue(codegen.fromProperty("firstName",(Schema) requestProperties.get("firstName")).deprecated);
694+
Assert.assertFalse(codegen.fromProperty("customerCode",(Schema) requestProperties.get("customerCode")).deprecated);
695+
}
696+
681697
@Test
682698
public void integerSchemaPropertyAndModelTest() {
683699
OpenAPI openAPI = TestUtils.createOpenAPI();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
paths:
10+
/deprecated-test:
11+
x-swagger-router-controller: /deprecated-test
12+
post:
13+
requestBody:
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/Request'
18+
responses:
19+
'200':
20+
description: responses
21+
content:
22+
application/json:
23+
schema:
24+
$ref: '#/components/schemas/Response'
25+
components:
26+
schemas:
27+
Request:
28+
type: object
29+
properties:
30+
customerCode:
31+
type: string
32+
example: '0001'
33+
firstName:
34+
type: string
35+
deprecated: true
36+
example: 'first'
37+
Response:
38+
type: object
39+
properties:
40+
customerCode:
41+
type: string
42+
example: '0001'
43+
firstName:
44+
type: string
45+
deprecated: true
46+
example: 'first'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.2.1-SNAPSHOT
1+
4.2.3-SNAPSHOT

samples/server/petstore/kotlin-springboot-modelMutable/src/main/kotlin/org/openapitools/model/Pet.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import io.swagger.annotations.ApiModelProperty
1616

1717
/**
1818
* A pet for sale in the pet store
19-
* @param id
20-
* @param category
2119
* @param name
2220
* @param photoUrls
21+
* @param id
22+
* @param category
2323
* @param tags
2424
* @param status pet status in the store
2525
*/

0 commit comments

Comments
 (0)