Set correct SchemaInferenceStack for list example 07/113507/5
authorPeter Suna <peter.suna@pantheon.tech>
Thu, 12 Sep 2024 16:32:09 +0000 (18:32 +0200)
committerPeter Suna <peter.suna@pantheon.tech>
Fri, 13 Sep 2024 15:01:35 +0000 (17:01 +0200)
In the method prepareFirstListExample, SchemaInferenceStack points
to the list, which is incorrect as we are processing its child.

This method is called only when the min-elements constraint is set.

JIRA: NETCONF-1389
Change-Id: I4ffb371751a71012ead461837f775a89957fd111
Signed-off-by: Peter Suna <peter.suna@pantheon.tech>
restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/model/PropertyEntity.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/NC1389Test.java [new file with mode: 0644]
restconf/restconf-openapi/src/test/resources/nc1389/document/leafref-test.json [new file with mode: 0644]
restconf/restconf-openapi/src/test/resources/nc1389/yang/leafref-test@2024-09-11.yang [new file with mode: 0644]

index 0149542fa948e4de8a37607a2b784e9a65c5a6c6..7c2b7edb6e73bcd01aecc1b7fabb157a150e4684 100644 (file)
@@ -279,7 +279,9 @@ public class PropertyEntity {
         for (final var childNode : childNodes) {
             if (childNode instanceof TypedDataSchemaNode leafSchemaNode) {
                 final var def = new TypeDef();
+                stack.enterDataTree(childNode.getQName());
                 processTypeDef(leafSchemaNode.getType(), leafSchemaNode, stack, def);
+                stack.exit();
                 if (def.hasExample()) {
                     firstExampleMap.put(leafSchemaNode.getQName().getLocalName(), def.getExample());
                 }
diff --git a/restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/NC1389Test.java b/restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/NC1389Test.java
new file mode 100644 (file)
index 0000000..53e0064
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.restconf.openapi.impl;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.skyscreamer.jsonassert.JSONAssert;
+
+/**
+ * Test for <a href="https://lf-opendaylight.atlassian.net/browse/NETCONF-1389">NETCONF-1389</a>.
+ */
+class NC1389Test extends AbstractDocumentTest {
+
+    @BeforeAll
+    static void beforeAll() {
+        initializeClass("/nc1389/yang/");
+    }
+
+    /**
+     * Tests the swagger document generated for the leafref-test@2024-09-11.yang model.
+     */
+    @Test
+    void getDocByModuleTest() throws Exception {
+        final var expectedJson = getExpectedDoc("nc1389/document/leafref-test.json");
+        final var moduleDoc = getDocByModule("leafref-test", "2024-09-11");
+        JSONAssert.assertEquals(expectedJson, moduleDoc, IGNORE_ORDER);
+    }
+}
diff --git a/restconf/restconf-openapi/src/test/resources/nc1389/document/leafref-test.json b/restconf/restconf-openapi/src/test/resources/nc1389/document/leafref-test.json
new file mode 100644 (file)
index 0000000..5d63f7d
--- /dev/null
@@ -0,0 +1,406 @@
+{
+  "openapi" : "3.0.3",
+  "info" : {
+    "version" : "1.0.0",
+    "title" : "leafref-test",
+    "description" : "We are providing full API for configurational data which can be edited (by POST, PUT, PATCH and DELETE).\nFor operational data we only provide GET API.\n\nFor majority of request you can see only config data in examples. That's because we can show only one example\nper request. The exception when you can see operational data in example is when data are representing\noperational (config false) container with no config data in it."
+  },
+  "servers" : [ {
+    "url" : "http://localhost:8181/"
+  } ],
+  "paths" : {
+    "/rests/data" : {
+      "post" : {
+        "description" : "\n\nNote:\nIn example payload, you can see only the first data node child of the resource to be created, following the\nguidelines of RFC 8040, which allows us to create only one resource in POST request.\n",
+        "summary" : "POST - Controller - leafref-test - leafref-test",
+        "requestBody" : {
+          "description" : "cntr",
+          "content" : {
+            "application/json" : {
+              "schema" : {
+                "properties" : {
+                  "cntr" : {
+                    "$ref" : "#/components/schemas/leafref-test_cntr",
+                    "type" : "object"
+                  }
+                }
+              }
+            },
+            "application/xml" : {
+              "schema" : {
+                "$ref" : "#/components/schemas/leafref-test_cntr"
+              }
+            }
+          }
+        },
+        "responses" : {
+          "201" : {
+            "description" : "Created"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ ]
+      }
+    },
+    "/rests/data/leafref-test:cntr" : {
+      "post" : {
+        "description" : "\n\nNote:\nIn example payload, you can see only the first data node child of the resource to be created, following the\nguidelines of RFC 8040, which allows us to create only one resource in POST request.\n",
+        "summary" : "POST - Controller - leafref-test - cntr",
+        "requestBody" : {
+          "description" : "lst",
+          "content" : {
+            "application/json" : {
+              "schema" : {
+                "properties" : {
+                  "lst" : {
+                    "type" : "array",
+                    "items" : {
+                      "$ref" : "#/components/schemas/leafref-test_cntr_lst",
+                      "type" : "object"
+                    }
+                  }
+                }
+              }
+            },
+            "application/xml" : {
+              "schema" : {
+                "$ref" : "#/components/schemas/leafref-test_cntr_lst"
+              }
+            }
+          }
+        },
+        "responses" : {
+          "201" : {
+            "description" : "Created"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ ]
+      },
+      "put" : {
+        "description" : "",
+        "summary" : "PUT - leafref-test - Controller - cntr",
+        "requestBody" : {
+          "description" : "cntr",
+          "content" : {
+            "application/json" : {
+              "schema" : {
+                "properties" : {
+                  "leafref-test:cntr" : {
+                    "$ref" : "#/components/schemas/leafref-test_cntr",
+                    "type" : "object"
+                  }
+                }
+              }
+            },
+            "application/xml" : {
+              "schema" : {
+                "$ref" : "#/components/schemas/leafref-test_cntr"
+              }
+            }
+          }
+        },
+        "responses" : {
+          "201" : {
+            "description" : "Created"
+          },
+          "204" : {
+            "description" : "Updated"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ ]
+      },
+      "patch" : {
+        "description" : "",
+        "summary" : "PATCH - leafref-test - Controller - cntr",
+        "requestBody" : {
+          "description" : "cntr",
+          "content" : {
+            "application/yang-data+json" : {
+              "schema" : {
+                "properties" : {
+                  "leafref-test:cntr" : {
+                    "$ref" : "#/components/schemas/leafref-test_cntr",
+                    "type" : "object"
+                  }
+                }
+              }
+            },
+            "application/yang-data+xml" : {
+              "schema" : {
+                "$ref" : "#/components/schemas/leafref-test_cntr"
+              }
+            }
+          }
+        },
+        "responses" : {
+          "200" : {
+            "description" : "OK"
+          },
+          "204" : {
+            "description" : "Updated"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ ]
+      },
+      "delete" : {
+        "description" : "",
+        "summary" : "DELETE - Controller - leafref-test - cntr",
+        "responses" : {
+          "204" : {
+            "description" : "Deleted"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ ]
+      },
+      "get" : {
+        "description" : "",
+        "summary" : "GET - Controller - leafref-test - cntr",
+        "responses" : {
+          "200" : {
+            "description" : "200",
+            "content" : {
+              "application/xml" : {
+                "schema" : {
+                  "$ref" : "#/components/schemas/leafref-test_cntr"
+                }
+              },
+              "application/json" : {
+                "schema" : {
+                  "properties" : {
+                    "cntr" : {
+                      "$ref" : "#/components/schemas/leafref-test_cntr",
+                      "type" : "object"
+                    }
+                  }
+                }
+              }
+            }
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ {
+          "name" : "content",
+          "in" : "query",
+          "required" : false,
+          "schema" : {
+            "enum" : [ "config", "nonconfig", "all" ],
+            "type" : "string"
+          }
+        } ]
+      }
+    },
+    "/rests/data/leafref-test:cntr/lst={lf2}" : {
+      "put" : {
+        "description" : "",
+        "summary" : "PUT - leafref-test - Controller - lst",
+        "requestBody" : {
+          "description" : "lst",
+          "content" : {
+            "application/json" : {
+              "schema" : {
+                "properties" : {
+                  "leafref-test:lst" : {
+                    "type" : "array",
+                    "items" : {
+                      "$ref" : "#/components/schemas/leafref-test_cntr_lst",
+                      "type" : "object"
+                    }
+                  }
+                }
+              }
+            },
+            "application/xml" : {
+              "schema" : {
+                "$ref" : "#/components/schemas/leafref-test_cntr_lst"
+              }
+            }
+          }
+        },
+        "responses" : {
+          "201" : {
+            "description" : "Created"
+          },
+          "204" : {
+            "description" : "Updated"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ {
+          "name" : "lf2",
+          "in" : "path",
+          "required" : true,
+          "schema" : {
+            "type" : "string"
+          }
+        } ]
+      },
+      "patch" : {
+        "description" : "",
+        "summary" : "PATCH - leafref-test - Controller - lst",
+        "requestBody" : {
+          "description" : "lst",
+          "content" : {
+            "application/yang-data+json" : {
+              "schema" : {
+                "properties" : {
+                  "leafref-test:lst" : {
+                    "type" : "array",
+                    "items" : {
+                      "$ref" : "#/components/schemas/leafref-test_cntr_lst",
+                      "type" : "object"
+                    }
+                  }
+                }
+              }
+            },
+            "application/yang-data+xml" : {
+              "schema" : {
+                "$ref" : "#/components/schemas/leafref-test_cntr_lst"
+              }
+            }
+          }
+        },
+        "responses" : {
+          "200" : {
+            "description" : "OK"
+          },
+          "204" : {
+            "description" : "Updated"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ {
+          "name" : "lf2",
+          "in" : "path",
+          "required" : true,
+          "schema" : {
+            "type" : "string"
+          }
+        } ]
+      },
+      "delete" : {
+        "description" : "",
+        "summary" : "DELETE - Controller - leafref-test - lst",
+        "responses" : {
+          "204" : {
+            "description" : "Deleted"
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ {
+          "name" : "lf2",
+          "in" : "path",
+          "required" : true,
+          "schema" : {
+            "type" : "string"
+          }
+        } ]
+      },
+      "get" : {
+        "description" : "",
+        "summary" : "GET - Controller - leafref-test - lst",
+        "responses" : {
+          "200" : {
+            "description" : "200",
+            "content" : {
+              "application/xml" : {
+                "schema" : {
+                  "$ref" : "#/components/schemas/leafref-test_cntr_lst"
+                }
+              },
+              "application/json" : {
+                "schema" : {
+                  "properties" : {
+                    "lst" : {
+                      "type" : "array",
+                      "items" : {
+                        "$ref" : "#/components/schemas/leafref-test_cntr_lst",
+                        "type" : "object"
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        },
+        "tags" : [ "Controller leafref-test" ],
+        "parameters" : [ {
+          "name" : "lf2",
+          "in" : "path",
+          "required" : true,
+          "schema" : {
+            "type" : "string"
+          }
+        }, {
+          "name" : "content",
+          "in" : "query",
+          "required" : false,
+          "schema" : {
+            "enum" : [ "config", "nonconfig", "all" ],
+            "type" : "string"
+          }
+        } ]
+      }
+    }
+  },
+  "components" : {
+    "schemas" : {
+      "leafref-test_cntr" : {
+        "title" : "leafref-test_cntr",
+        "type" : "object",
+        "properties" : {
+          "lf1" : {
+            "description" : "",
+            "type" : "string",
+            "example" : "Some lf1"
+          },
+          "lst" : {
+            "type" : "array",
+            "items" : {
+              "$ref" : "#/components/schemas/leafref-test_cntr_lst"
+            },
+            "description" : "",
+            "example" : [ {
+              "lf2" : "Some lf2"
+            } ],
+            "minItems" : 1
+          }
+        },
+        "required" : [ "lst" ],
+        "xml" : {
+          "name" : "cntr",
+          "namespace" : "urn:leafref-test"
+        }
+      },
+      "leafref-test_cntr_lst" : {
+        "title" : "leafref-test_cntr_lst",
+        "type" : "object",
+        "properties" : {
+          "lf2" : {
+            "description" : "",
+            "type" : "string",
+            "example" : "Some lf2"
+          }
+        },
+        "xml" : {
+          "name" : "lst",
+          "namespace" : "urn:leafref-test"
+        }
+      }
+    },
+    "securitySchemes" : {
+      "basicAuth" : {
+        "scheme" : "basic",
+        "type" : "http"
+      }
+    }
+  },
+  "security" : [ {
+    "basicAuth" : [ ]
+  } ]
+}
diff --git a/restconf/restconf-openapi/src/test/resources/nc1389/yang/leafref-test@2024-09-11.yang b/restconf/restconf-openapi/src/test/resources/nc1389/yang/leafref-test@2024-09-11.yang
new file mode 100644 (file)
index 0000000..f9998f7
--- /dev/null
@@ -0,0 +1,22 @@
+module leafref-test {
+    namespace "urn:leafref-test";
+    prefix "lt";
+    revision 2024-09-11 {
+        description "Initial revision.";
+    }
+
+    container cntr {
+        leaf lf1 {
+            type string;
+        }
+        list lst {
+            key lf2;
+            min-elements 1;
+            leaf lf2 {
+                type leafref {
+                    path "../../lf1";
+                }
+            }
+        }
+    }
+}