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
This commit is contained in:
Yutaka.Miyamae 2020-01-29 17:30:33 +09:00 committed by GitHub
parent 1ac0f141a6
commit 8214460ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 16 deletions

View File

@ -54,6 +54,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
public boolean exclusiveMaximum;
public boolean hasMore;
public boolean required;
public boolean deprecated;
public boolean secondaryParam;
public boolean hasMoreNonReadOnly; // for model constructor, true if next property is not readonly
public boolean isPrimitiveType;
@ -554,6 +555,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
sb.append(", exclusiveMaximum=").append(exclusiveMaximum);
sb.append(", hasMore=").append(hasMore);
sb.append(", required=").append(required);
sb.append(", deprecated=").append(deprecated);
sb.append(", secondaryParam=").append(secondaryParam);
sb.append(", hasMoreNonReadOnly=").append(hasMoreNonReadOnly);
sb.append(", isPrimitiveType=").append(isPrimitiveType);
@ -619,6 +621,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
exclusiveMaximum == that.exclusiveMaximum &&
hasMore == that.hasMore &&
required == that.required &&
deprecated == this.deprecated &&
secondaryParam == that.secondaryParam &&
hasMoreNonReadOnly == that.hasMoreNonReadOnly &&
isPrimitiveType == that.isPrimitiveType &&
@ -698,16 +701,18 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
@Override
public int hashCode() {
return Objects.hash(openApiType, baseName, complexType, getter, setter, description, dataType,
datatypeWithEnum, dataFormat, name, min, max, defaultValue, defaultValueWithParam, baseType,
containerType, title, unescapedDescription, maxLength, minLength, pattern, example, jsonSchema,
minimum, maximum, exclusiveMinimum, exclusiveMaximum, hasMore, required, secondaryParam,
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric, isInteger,
isLong, isNumber, isFloat, isDouble, isByteArray, isBinary, isFile, isBoolean, isDate, isDateTime,
isUuid, isUri, isEmail, isFreeFormObject, isListContainer, isMapContainer, isEnum, isReadOnly,
isWriteOnly, isNullable, isSelfReference, isCircularReference, _enum, allowableValues, items,
mostInnerItems, vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName, xmlNamespace,
isXmlWrapped, multipleOf);
return Objects.hash(openApiType, baseName, complexType, getter, setter, description,
dataType, datatypeWithEnum, dataFormat, name, min, max, defaultValue,
defaultValueWithParam, baseType, containerType, title, unescapedDescription,
maxLength, minLength, pattern, example, jsonSchema, minimum, maximum,
exclusiveMinimum, exclusiveMaximum, hasMore, required, deprecated, secondaryParam,
hasMoreNonReadOnly, isPrimitiveType, isModel, isContainer, isString, isNumeric,
isInteger, isLong, isNumber, isFloat, isDouble, isByteArray, isBinary, isFile,
isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject,
isListContainer, isMapContainer, isEnum, isReadOnly, isWriteOnly, isNullable,
isSelfReference, isCircularReference, _enum, allowableValues, items, mostInnerItems,
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
xmlNamespace, isXmlWrapped);
}
}

View File

@ -2255,6 +2255,10 @@ public class DefaultCodegen implements CodegenConfig {
property.defaultValue = toDefaultValue(p);
property.defaultValueWithParam = toDefaultValueWithParam(name, p);
property.jsonSchema = Json.pretty(p);
if (p.getDeprecated() != null) {
property.deprecated = p.getDeprecated();
}
if (p.getReadOnly() != null) {
property.isReadOnly = p.getReadOnly();
}
@ -5477,4 +5481,4 @@ public class DefaultCodegen implements CodegenConfig {
public void setFeatureSet(final FeatureSet featureSet) {
this.featureSet = featureSet == null ? DefaultFeatureSet : featureSet;
}
}
}

View File

@ -1,4 +1,5 @@
{{#useBeanValidation}}{{#required}}
{{^isReadOnly}}@get:NotNull{{/isReadOnly}} {{/required}}{{>beanValidationModel}}{{/useBeanValidation}}{{#swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}
@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/swaggerAnnotations}}{{#deprecated}}
@Deprecated(message=""){{/deprecated}}
@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}}

View File

@ -678,6 +678,22 @@ public class DefaultCodegenTest {
Assert.assertTrue(property.isNullable);
}
@Test
public void testDeprecatedProperty() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/property-deplicated.yaml");
new InlineModelResolver().flatten(openAPI);
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
final Map responseProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("Response").getProperties());
final Map requestProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("Response").getProperties());
Assert.assertTrue(codegen.fromProperty("firstName",(Schema) responseProperties.get("firstName")).deprecated);
Assert.assertFalse(codegen.fromProperty("customerCode",(Schema) responseProperties.get("customerCode")).deprecated);
Assert.assertTrue(codegen.fromProperty("firstName",(Schema) requestProperties.get("firstName")).deprecated);
Assert.assertFalse(codegen.fromProperty("customerCode",(Schema) requestProperties.get("customerCode")).deprecated);
}
@Test
public void integerSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();

View File

@ -0,0 +1,46 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/deprecated-test:
x-swagger-router-controller: /deprecated-test
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Request'
responses:
'200':
description: responses
content:
application/json:
schema:
$ref: '#/components/schemas/Response'
components:
schemas:
Request:
type: object
properties:
customerCode:
type: string
example: '0001'
firstName:
type: string
deprecated: true
example: 'first'
Response:
type: object
properties:
customerCode:
type: string
example: '0001'
firstName:
type: string
deprecated: true
example: 'first'

View File

@ -1 +1 @@
4.2.1-SNAPSHOT
4.2.3-SNAPSHOT

View File

@ -16,10 +16,10 @@ import io.swagger.annotations.ApiModelProperty
/**
* A pet for sale in the pet store
* @param id
* @param category
* @param name
* @param photoUrls
* @param id
* @param category
* @param tags
* @param status pet status in the store
*/