Skip to content

Commit 5b17715

Browse files
committed
Add PathItems to Components
- update model interfaces - update ModelConstructionTest - update OASModelReader and static file tests
1 parent fbd05fd commit 5b17715

File tree

6 files changed

+153
-16
lines changed

6 files changed

+153
-16
lines changed

api/src/main/java/org/eclipse/microprofile/openapi/models/Components.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,53 @@ default Components callbacks(Map<String, Callback> callbacks) {
483483
*/
484484
void removeCallback(String key);
485485

486+
/**
487+
* Returns the pathItems property of this Components instance. Path items listed here can be referenced from
488+
* elsewhere in the OpenAPI document.
489+
*
490+
* @return a copy Map (potentially immutable) of path items
491+
*/
492+
Map<String, PathItem> getPathItems();
493+
494+
/**
495+
* Sets the pathItems property of this Components instance. Path items listed here can be referenced from elsewhere
496+
* in the OpenAPI document.
497+
*
498+
* @param pathItems
499+
* a map of path items
500+
*/
501+
void setPathItems(Map<String, PathItem> pathItems);
502+
503+
/**
504+
* Sets the pathItems property of this Components instance. Path items listed here can be referenced from elsewhere
505+
* in the OpenAPI document.
506+
*
507+
* @param pathItems
508+
* a map of path items
509+
* @return the current Schema instance
510+
*/
511+
default Components pathItems(Map<String, PathItem> pathItems) {
512+
setPathItems(pathItems);
513+
return this;
514+
}
515+
516+
/**
517+
* Adds a path item.
518+
*
519+
* @param name
520+
* name of the path item to add
521+
* @param pathItem
522+
* the path item to add
523+
* @return the current Schema instance
524+
*/
525+
Components addPathItem(String name, PathItem pathItem);
526+
527+
/**
528+
* Removes a path item.
529+
*
530+
* @param name
531+
* name of the path item to remove
532+
*/
533+
void removePathItem(String name);
534+
486535
}

tck/src/main/java/org/eclipse/microprofile/openapi/reader/MyOASModelReaderImpl.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,18 @@ public OpenAPI buildModel() {
223223
.description("The username corresponding to provided user id")
224224
.operationId("getUserByName")
225225
.parameters(new HashMap<String, Object>())
226-
.addParameter("userId", "$request.link-path.userId")))
226+
.addParameter("userId", "$request.link-path.userId"))
227+
.addPathItem("idCrud", OASFactory.createPathItem()
228+
.DELETE(OASFactory.createOperation()
229+
.responses(OASFactory.createAPIResponses()
230+
.addAPIResponse("202", OASFactory.createAPIResponse()
231+
.content(OASFactory.createContent())
232+
.description("Delete item"))))
233+
.addParameter(OASFactory.createParameter()
234+
.name("id")
235+
.in(In.PATH)
236+
.description("The item parameter")
237+
.required(true))))
227238
.tags(new ArrayList<Tag>())
228239
.addTag(OASFactory.createObject(Tag.class)
229240
.name("Get Airlines")
@@ -389,6 +400,16 @@ public OpenAPI buildModel() {
389400
.title("id")
390401
.description("id of the new booking")
391402
.addType(
392-
Schema.SchemaType.STRING)))))))));
403+
Schema.SchemaType.STRING))))))))
404+
.addPathItem("/refpath/{id}", OASFactory.createPathItem()
405+
.ref("idCrud") // Note PathItem allows ref with other properties
406+
.GET(OASFactory.createOperation()
407+
.responses(OASFactory.createAPIResponses()
408+
.addAPIResponse("200", OASFactory.createAPIResponse()
409+
.content(OASFactory.createContent()
410+
.addMediaType("application/json",
411+
OASFactory.createMediaType()
412+
.schema(OASFactory.createSchema()
413+
.ref("Airlines")))))))));
393414
}
394415
}

tck/src/main/java/org/eclipse/microprofile/openapi/tck/ModelConstructionTest.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,27 @@ public void componentsTest() {
393393
SecurityScheme otherSecuritySchemeValue = createConstructibleInstance(SecurityScheme.class);
394394
checkMapImmutable(c, Components::getSecuritySchemes, "otherSecurityScheme", otherSecuritySchemeValue);
395395
checkNullValueInAdd(c::getSecuritySchemes, c::addSecurityScheme, "someSecurityScheme", securitySchemeValue);
396+
397+
final String pathItemKey = "myPathItem";
398+
final PathItem pathItemValue = createConstructibleInstance(PathItem.class);
399+
checkSameObject(c, c.addPathItem(pathItemKey, pathItemValue));
400+
checkMapEntry(c.getPathItems(), pathItemKey, pathItemValue);
401+
assertEquals(c.getPathItems().size(), 1, "The map is expected to contain one entry.");
402+
c.removePathItem(pathItemKey);
403+
assertEquals(c.getPathItems().size(), 0, "The map is expected to be empty.");
404+
405+
final String pathItemKey2 = "myPathItem2";
406+
final PathItem pathItemValue2 = createConstructibleInstance(PathItem.class);
407+
c.setPathItems(Collections.singletonMap(pathItemKey2, pathItemValue2));
408+
checkMapEntry(c.getPathItems(), pathItemKey2, pathItemValue2);
409+
assertEquals(c.getPathItems().size(), 1, "The map is expected to contain one entry.");
410+
checkSameObject(c, c.addPathItem(pathItemKey, pathItemValue));
411+
checkMapEntry(c.getPathItems(), pathItemKey, pathItemValue);
412+
assertEquals(c.getPathItems().size(), 2, "The map is expected to contain two entries.");
413+
414+
PathItem otherPathItemValue = createConstructibleInstance(PathItem.class);
415+
checkMapImmutable(c, Components::getPathItems, "otherPathItem", otherPathItemValue);
416+
checkNullValueInAdd(c::getPathItems, c::addPathItem, "somePathItem", pathItemValue);
396417
}
397418

398419
@Test
@@ -1771,30 +1792,29 @@ private void processReference(Reference<?> r) {
17711792
r.setRef(myRef1);
17721793
assertEquals(r.getRef(), myRef1,
17731794
"The return value of getRef() is expected to be equal to the value that was set.");
1795+
17741796
// Check that the short name ref value can be set using the setter method and that the getter method returns the
17751797
// expanded value.
1776-
if (!(r instanceof PathItem)) {
1777-
final String shortName = "myRef2";
1778-
final String myRef2 = createReference(r, shortName);
1779-
r.setRef(shortName);
1780-
assertEquals(r.getRef(), myRef2, "The return value of getRef() is expected to be a fully expanded name.");
1781-
}
1798+
final String shortName2 = "myRef2";
1799+
final String myRef2 = createReference(r, shortName2);
1800+
r.setRef(shortName2);
1801+
assertEquals(r.getRef(), myRef2, "The return value of getRef() is expected to be a fully expanded name.");
1802+
17821803
// Check that the ref value can be set using the builder method and that the getter method returns the same
17831804
// value.
17841805
final String myRef3 = createReference(r, "myRef3");
17851806
final Reference<?> self = r.ref(myRef3);
17861807
assertSame(self, r, "The return value of ref() is expected to return the current instance.");
17871808
assertEquals(r.getRef(), myRef3,
17881809
"The return value of getRef() is expected to be equal to the value that was set.");
1810+
17891811
// Check that the short name ref value can be set using the builder method and that the getter method returns
17901812
// the expanded value.
1791-
if (!(r instanceof PathItem)) {
1792-
final String shortName = "myRef4";
1793-
final String myRef4 = createReference(r, shortName);
1794-
final Reference<?> self2 = r.ref(shortName);
1795-
assertSame(self2, r, "The return value of ref() is expected to return the current instance.");
1796-
assertEquals(r.getRef(), myRef4, "The return value of getRef() is expected to be a fully expanded name.");
1797-
}
1813+
final String shortName4 = "myRef4";
1814+
final String myRef4 = createReference(r, shortName4);
1815+
final Reference<?> self2 = r.ref(shortName4);
1816+
assertSame(self2, r, "The return value of ref() is expected to return the current instance.");
1817+
assertEquals(r.getRef(), myRef4, "The return value of getRef() is expected to be a fully expanded name.");
17981818
}
17991819

18001820
private void processConstructibleProperty(Constructible o, Property p, Class<?> enclosingInterface) {
@@ -1888,7 +1908,7 @@ private String createReference(Reference<?> r, String v) {
18881908
} else if (r instanceof Parameter) {
18891909
sb.append("#/components/parameters/");
18901910
} else if (r instanceof PathItem) {
1891-
sb.append("http://www.abc.def.ghi/");
1911+
sb.append("#/components/pathItems/");
18921912
} else if (r instanceof RequestBody) {
18931913
sb.append("#/components/requestBodies/");
18941914
} else if (r instanceof Schema) {

tck/src/main/java/org/eclipse/microprofile/openapi/tck/ModelReaderAppTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ public void testComponents(String type) {
325325
vr.body("components.headers.Request-Limit", notNullValue());
326326
vr.body("components.securitySchemes.httpTestScheme", notNullValue());
327327
vr.body("components.links.UserName", notNullValue());
328+
vr.body("components.pathItems.idCrud", notNullValue());
328329
}
329330

330331
@Test(dataProvider = "formatProvider")
@@ -337,6 +338,22 @@ public void testHeaderInComponents(String type) {
337338
vr.body(maxRate + ".allowEmptyValue", equalTo(true));
338339
}
339340

341+
@Test(dataProvider = "formatProvider")
342+
public void testPathItemWithRef(String type) {
343+
ValidatableResponse vr = callEndpoint(type);
344+
345+
// Referencing path item
346+
String refpath = "paths.'/refpath/{id}'";
347+
vr.body(refpath + ".$ref", equalTo("#/components/pathItems/idCrud"));
348+
vr.body(refpath + ".get.responses.'200'", notNullValue());
349+
350+
// Referenced path item
351+
String idCrud = "components.pathItems.idCrud";
352+
vr.body(idCrud, notNullValue());
353+
vr.body(idCrud + ".parameters[0].description", equalTo("The item parameter"));
354+
vr.body(idCrud + ".delete.responses.'202'.description", equalTo("Delete item"));
355+
}
356+
340357
@Test(dataProvider = "formatProvider")
341358
public void testContentInAPIResponse(String type) {
342359
ValidatableResponse vr = callEndpoint(type);

tck/src/main/java/org/eclipse/microprofile/openapi/tck/StaticDocumentTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,14 @@ public void testStaticDocument(String type) {
140140
equalTo("#/components/schemas/Booking"));
141141
vr.body(webhookDelete + ".responses.'204'.description",
142142
equalTo("Indicates that the deletion event was processed successfully"));
143+
144+
String idCrud = "components.pathItems.idCrud";
145+
vr.body(idCrud, notNullValue());
146+
vr.body(idCrud + ".parameters[0].description", equalTo("The item parameter"));
147+
vr.body(idCrud + ".delete.responses.'202'.description", equalTo("Delete item"));
148+
149+
String refpath = "paths.'/refpath/{id}'";
150+
vr.body(refpath + ".$ref", equalTo("#/components/pathItems/idCrud"));
151+
vr.body(refpath + ".get.responses.'200'", notNullValue());
143152
}
144153
}

tck/src/main/resources/simpleapi.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ paths:
120120
responses:
121121
'200':
122122
description: trace operation tested
123+
/refpath/{id}:
124+
$ref: '#/components/pathItems/idCrud'
125+
get:
126+
responses:
127+
'200':
128+
content:
129+
'application/json':
130+
schema:
131+
$ref: '#/components/schemas/InventoryItem'
123132
webhooks:
124133
bookingEvent:
125134
put:
@@ -186,3 +195,15 @@ components:
186195
examples:
187196
- 408-867-5309
188197
type: object
198+
pathItems:
199+
idCrud:
200+
delete:
201+
responses:
202+
'202':
203+
content: []
204+
description: Delete item
205+
parameters:
206+
- name: id
207+
in: path
208+
description: The item parameter
209+
required: true

0 commit comments

Comments
 (0)