Skip to content

Commit

Permalink
Propagate deprecated property through $ref's (OpenAPITools#6093)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
asmundg authored and michaelpro1 committed May 7, 2020
1 parent 302d028 commit dca2370
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,17 @@ public CodegenProperty fromProperty(String name, Schema p) {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,19 @@ public void testDeprecatedProperty() {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dca2370

Please sign in to comment.