Skip to content

Commit f229f63

Browse files
authored
Merge pull request #32 from openapi-processor/ref-loop-array
fix null pointer with array items ref loop
2 parents 0f47b58 + dbb5467 commit f229f63

File tree

10 files changed

+157
-5
lines changed

10 files changed

+157
-5
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/DataTypeConverter.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ class DataTypeConverter(
360360
*/
361361
private fun isLoop(info: SchemaInfo): Boolean {
362362
val found = current.find {
363-
it.getName() == info.getName()
363+
// $ref and non-ref SchemaInfo have the same name.
364+
// We are only interested if we have seen a non-ref!
365+
it.getName() == info.getName() && !it.isRefObject()
364366
}
365367
return found != null
366368
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/SchemaInfo.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,18 @@ class SchemaInfo(
255255
* @return a new {@link SchemaInfo}
256256
*/
257257
fun buildForItem(): SchemaInfo {
258+
val name = if (schema?.getItem()?.getRef() != null) {
259+
getRefName(schema.getItem())
260+
} else {
261+
schema?.getItem()?.getType()
262+
}
263+
258264
return SchemaInfo(
259265
path = path,
260-
name = schema?.getItem()?.getRef() ?: schema?.getItem()?.getType()!!,
266+
name = name!!,
261267
schema = schema?.getItem(),
262268
resolver = resolver
263269
)
264-
265270
}
266271

267272
override fun isPrimitive(): Boolean {

openapi-processor-core/src/testInt/groovy/com/github/hauner/openapi/processor/core/ProcessorEndToEndTest.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ProcessorEndToEndTest extends ProcessorTestBase {
3434
'deprecated',
3535
'ref-into-another-file-path',
3636
'ref-is-relative-to-current-file',
37+
'ref-loop-array',
3738
'ref-parameter',
3839
'ref-parameter-with-primitive-mapping',
3940
'response-content-multiple-no-content'

openapi-processor-core/src/testInt/groovy/com/github/hauner/openapi/processor/core/ProcessorPendingTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ProcessorPendingTest extends ProcessorTestBase {
3232
@Parameterized.Parameters(name = "{0}")
3333
static Collection<TestSet> sources () {
3434
return [
35-
new TestSet(name: 'response-content-multiple-no-content', processor: new TestProcessor(), parser: ParserType.SWAGGER),
36-
new TestSet(name: 'response-content-multiple-no-content', processor: new TestProcessor(), parser: ParserType.OPENAPI4J)
35+
new TestSet(name: 'ref-loop-array', processor: new TestProcessor(), parser: ParserType.SWAGGER),
36+
new TestSet(name: 'ref-loop-array', processor: new TestProcessor(), parser: ParserType.OPENAPI4J)
3737
]
3838
}
3939

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
items:
2+
- generated/api/Api.java
3+
- generated/model/Foo.java
4+
- generated/model/Foos.java
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
3+
* TEST ONLY.
4+
*/
5+
6+
package generated.api;
7+
8+
import annotation.Mapping;
9+
import generated.model.Foo;
10+
11+
public interface Api {
12+
13+
@Mapping("/response-ref")
14+
Foo[] getResponseRef();
15+
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
3+
* TEST ONLY.
4+
*/
5+
6+
package generated.model;
7+
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
10+
public class Foo {
11+
12+
@JsonProperty("foos")
13+
private Foos foos;
14+
15+
public Foos getFoos() {
16+
return foos;
17+
}
18+
19+
public void setFoos(Foos foos) {
20+
this.foos = foos;
21+
}
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* This class is auto generated by https://github.com/hauner/openapi-processor-core.
3+
* TEST ONLY.
4+
*/
5+
6+
package generated.model;
7+
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
10+
public class Foos {
11+
12+
@JsonProperty("items")
13+
private Foo[] items;
14+
15+
public Foo[] getItems() {
16+
return items;
17+
}
18+
19+
public void setItems(Foo[] items) {
20+
this.items = items;
21+
}
22+
23+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
items:
2+
- inputs/openapi.yaml
3+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
openapi: 3.0.2
2+
info:
3+
title: test array item $ref loop
4+
version: 1.0.0
5+
6+
paths:
7+
/response-ref:
8+
get:
9+
responses:
10+
200:
11+
description: array item $ref
12+
content:
13+
application/json:
14+
schema:
15+
type: array
16+
items:
17+
$ref: '#/components/schemas/Foo'
18+
19+
components:
20+
schemas:
21+
Foo:
22+
type: object
23+
properties:
24+
foos:
25+
$ref: '#/components/schemas/Foos'
26+
27+
Foos:
28+
type: object
29+
properties:
30+
items:
31+
type: array
32+
items:
33+
$ref: '#/components/schemas/Foo'
34+
35+
# Response
36+
# todoItems: TodoItem[]
37+
#
38+
# TodoItem {
39+
# todoList: TodoList
40+
# }
41+
#
42+
# TodoList {
43+
# todoItems: TodoItem[]
44+
# }
45+
46+
# ItemsResponse200 array
47+
# #/components/schemas/TodoItemResource $ref
48+
# TodoItemResource object
49+
# TodoItemResourceTodoList $ref
50+
# TodoListResource object
51+
# TodoListResourceTodoItem array
52+
# #/components/schemas/TodoItemResource lazy
53+
#
54+
# TodoListResource => dataTypes
55+
#
56+
# TodoItemResource => dataTypes
57+
#
58+
#
59+
# ItemsResponse200 array
60+
# TodoItemResource $ref
61+
# TodoItemResource lazy
62+
63+
64+
#
65+
#
66+
#
67+
#
68+
#
69+
#
70+
#
71+
#
72+
#
73+
#
74+
#
75+
#

0 commit comments

Comments
 (0)