Propagate deprecated property through $ref's (#6093)

* Propagate deprecated property through $ref's

As $ref is supposed to completely replace the definition of a property,
make sure we also include the 'deprecated' property when generating the
type of a $ref property.

This makes a property $ref'ing a deprecated schema also become deprecated.

* Clarify why we're messing around with $ref
This commit is contained in:
Åsmund Grammeltvedt 2020-05-01 16:01:01 +02:00 committed by GitHub
parent 6484c03518
commit c08e80f49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 0 deletions

View File

@ -2850,6 +2850,17 @@ public class DefaultCodegen implements CodegenConfig {
if (p.getDeprecated() != null) {
property.deprecated = p.getDeprecated();
} else if (p.get$ref() != null) {
// Since $ref should be replaced with the model it refers
// to, $ref'ing a model with 'deprecated' set should cause
// the property to reflect the model's 'deprecated' value.
String ref = ModelUtils.getSimpleRef(p.get$ref());
if (ref != null) {
Schema referencedSchema = ModelUtils.getSchemas(this.openAPI).get(ref);
if (referencedSchema != null && referencedSchema.getDeprecated() != null) {
property.deprecated = referencedSchema.getDeprecated();
}
}
}
if (p.getReadOnly() != null) {
property.isReadOnly = p.getReadOnly();

View File

@ -1409,6 +1409,19 @@ public class DefaultCodegenTest {
Assert.assertFalse(codegen.fromProperty("customerCode",(Schema) requestProperties.get("customerCode")).deprecated);
}
@Test
public void testDeprecatedRef() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/model-deprecated.yaml");
new InlineModelResolver().flatten(openAPI);
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
final Map requestProperties = Collections.unmodifiableMap(openAPI.getComponents().getSchemas().get("complex").getProperties());
Assert.assertTrue(codegen.fromProperty("deprecated", (Schema)requestProperties.get("deprecated")).deprecated);
Assert.assertFalse(codegen.fromProperty("current", (Schema)requestProperties.get("current")).deprecated);
}
@Test
public void integerSchemaPropertyAndModelTest() {
OpenAPI openAPI = TestUtils.createOpenAPI();

View File

@ -0,0 +1,36 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
components:
schemas:
deprecated:
type: object
deprecated: true
properties:
customerCode:
type: string
example: '0001'
firstName:
type: string
example: 'first'
current:
type: object
properties:
customerCode:
type: string
example: '0001'
firstName:
type: string
example: 'first'
complex:
properties:
deprecated:
$ref: "#/components/schemas/deprecated"
current:
type: boolean