Skip to content

Commit

Permalink
Fix exception in ConsoleRender when property has been removed (#7)
Browse files Browse the repository at this point in the history
`ConsoleRender` failed to resolve component schemas which had been removed in the "new" OpenAPI specification and threw an `IllegalArgumentException`:

```
Unexpected exception. Reason: ref '#/components/schemas/RemovedSchema' doesn't exist.

java.lang.IllegalArgumentException: ref '#/components/schemas/PermissionSet' doesn't exist.
	at com.qdesrame.openapi.diff.core.utils.RefPointer.resolveRef(RefPointer.java:20)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.resolve(ConsoleRender.java:231)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.lambda$properties$1(ConsoleRender.java:217)
	at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.properties(ConsoleRender.java:216)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.incompatibilities(ConsoleRender.java:199)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.lambda$incompatibilities$0(ConsoleRender.java:202)
	at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.incompatibilities(ConsoleRender.java:202)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.incompatibilities(ConsoleRender.java:185)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.itemContent(ConsoleRender.java:179)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.ul_content(ConsoleRender.java:155)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.ol_changed(ConsoleRender.java:87)
	at com.qdesrame.openapi.diff.core.output.ConsoleRender.render(ConsoleRender.java:47)
	at com.qdesrame.openapi.diff.cli.Main.main(Main.java:159)
```
  • Loading branch information
joschi authored Jun 24, 2020
1 parent 3c6f9af commit d348630
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,19 @@ private String properties(
String propPrefix, String title, Map<String, Schema> properties, DiffContext context) {
StringBuilder sb = new StringBuilder();
if (properties != null) {
properties.forEach(
(key, value) -> sb.append(property(propPrefix + key, title, resolve(value))));
properties.forEach((key, value) -> sb.append(resolveProperty(propPrefix, value, key, title)));
}
return sb.toString();
}

private String resolveProperty(String propPrefix, Schema value, String key, String title) {
try {
return property(propPrefix + key, title, resolve(value));
} catch (Exception e) {
return property(propPrefix + key, title, type(value));
}
}

protected String property(String name, String title, Schema schema) {
return property(name, title, type(schema));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.qdesrame.openapi.test;

import static org.assertj.core.api.Assertions.assertThat;

import com.qdesrame.openapi.diff.core.OpenApiCompare;
import com.qdesrame.openapi.diff.core.model.ChangedOpenApi;
import com.qdesrame.openapi.diff.core.output.ConsoleRender;
import org.junit.jupiter.api.Test;

public class ConsoleRenderTest {
@Test
public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
ConsoleRender render = new ConsoleRender();
ChangedOpenApi diff =
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
assertThat(render.render(diff)).isNotBlank();
}
}
31 changes: 31 additions & 0 deletions core/src/test/resources/missing_property_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.0.1
info:
title: Title
version: 1.0.0
description: Description
paths:
/:
get:
summary: Simple GET
operationId: simpleGet
responses:
default:
content:
application/json:
schema:
$ref: '#/components/schemas/Wrapper'
description: Simple GET
components:
schemas:
Wrapper:
type: object
properties:
id:
type: string
childProperty:
$ref: '#/components/schemas/ChildProperty'
ChildProperty:
type: object
properties:
id:
type: string
24 changes: 24 additions & 0 deletions core/src/test/resources/missing_property_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: 3.0.1
info:
title: Title
version: 1.0.0
description: Description
paths:
/:
get:
summary: Simple GET
operationId: simpleGet
responses:
default:
content:
application/json:
schema:
$ref: '#/components/schemas/Wrapper'
description: Simple GET
components:
schemas:
Wrapper:
type: object
properties:
id:
type: string

0 comments on commit d348630

Please sign in to comment.