Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger pyang plugin fails if a container doesn't "use" groupings #303

Open
SapnaDerajeRadhakrishna opened this issue Aug 16, 2018 · 2 comments

Comments

@SapnaDerajeRadhakrishna

Similar to ict-strauss/COP#2
This issue still exists with pyang verion 1.7

@SapnaDerajeRadhakrishna
Copy link
Author

For more details on the issue
python version - Python 2.7.6
pyang version - pyang 1.7.5

Operating System: Ubuntu 14.04.5 LTS
Kernel: Linux 4.4.0-116-generic
Architecture: x86_64

The yang files I am using is similar to the ones mentioned in the issue - ict-strauss/COP#2

Command used: pyang -f swagger -p e9-mb-409-Schema/ e9-mb-409-Schema/[email protected] -o 409.json

Error:

Traceback (most recent call last):
File "/usr/local/bin/pyang", line 450, in
run()
File "/usr/local/bin/pyang", line 419, in run
emit_obj.emit(ctx, modules, fd)
File "/usr/local/lib/python2.7/dist-packages/pyang/plugins/swagger.py", line 98, in emit
emit_swagger_spec(ctx, modules, fd, ctx.opts.path)
File "/usr/local/lib/python2.7/dist-packages/pyang/plugins/swagger.py", line 178, in emit_swagger_spec
gen_apis(chs, path, model['paths'], definitions)
File "/usr/local/lib/python2.7/dist-packages/pyang/plugins/swagger.py", line 434, in gen_apis
gen_api_node(child, path, apis, definitions, config)
File "/usr/local/lib/python2.7/dist-packages/pyang/plugins/swagger.py", line 539, in gen_api_node
schema = schema[to_lower_camelcase(node.arg)]
KeyError: u'clear-interface-ethernet-counters'

@arthurMll
Copy link
Collaborator

arthurMll commented Aug 20, 2018

Hi,

I've been running the current swagger pyang plugin uploaded into the EAGLE repository and I cannot reproduce the same problems mentioned here and in ict-strauss/COP#2.

My setup is the following:

pyang version - pyang 1.7.5
python version - Python 2.7.12
Operating System: Ubuntu 16.04.2 LTS
Linux kernel version: 4.10.0-28-generic
Swagger pyang file: swagger.py

Using the yang examples included into the issue - ict-strauss/COP#2 I obtained the following results which seems to be correct:

Example 1:

Input:

module basic {
    namespace "urn:acme:yang:basic";
    prefix "basic";

    container node {
        leaf my-id {
            type uint8;
        }
    }
}

Output:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "description": "basic API generated from basic.yang",
        "title": "basic API"
    },
    "host": "localhost:8080",
    "basePath": "/restconf",
    "schemes": [
        "http"
    ],
    "paths": {
        "/config/node/": {
            "put": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Update operation of resource: node",
                "parameters": [
                    {
                        "required": true,
                        "description": "nodebody object",
                        "schema": {
                            "$ref": "#/definitions/node_schema"
                        },
                        "name": "node",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "summary": "Update node by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "update_node_by_id"
            },
            "post": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Create operation of resource: node",
                "parameters": [
                    {
                        "required": true,
                        "description": "nodebody object",
                        "schema": {
                            "$ref": "#/definitions/node_schema"
                        },
                        "name": "node",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "summary": "Create node by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "create_node_by_id"
            },
            "delete": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "produces": [
                    "application/json"
                ],
                "description": "Delete operation of resource: node",
                "summary": "Delete node by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "delete_node_by_id"
            },
            "get": {
                "responses": {
                    "200": {
                        "description": "Successful operation",
                        "schema": {
                            "$ref": "#/definitions/node_schema"
                        }
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Retrieve operation of resource: node",
                "parameters": [],
                "produces": [
                    "application/json"
                ],
                "summary": "Retrieve node",
                "consumes": [
                    "application/json"
                ],
                "operationId": "retrieve_node"
            }
        }
    },
    "definitions": {
        "node_schema": {
            "properties": {
                "my-id": {
                    "type": "string"
                }
            }
        }
    }
}

Example 2:

Input:

module basic2 {
    namespace "urn:acme:yang:basic";
    prefix "basic";

    grouping node {     
        leaf my-id {
            type uint8;
        }   
    }

    container anotherNode {
        leaf another-id {
            type uint8; // the container doesn't use the grouping "node"
        }
    }
}

Output:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "description": "basic2 API generated from basic2.yang",
        "title": "basic2 API"
    },
    "host": "localhost:8080",
    "basePath": "/restconf",
    "schemes": [
        "http"
    ],
    "paths": {
        "/config/anotherNode/": {
            "put": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Update operation of resource: anotherNode",
                "parameters": [
                    {
                        "required": true,
                        "description": "anotherNodebody object",
                        "schema": {
                            "$ref": "#/definitions/anotherNode_schema"
                        },
                        "name": "anotherNode",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "summary": "Update anotherNode by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "update_anotherNode_by_id"
            },
            "post": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Create operation of resource: anotherNode",
                "parameters": [
                    {
                        "required": true,
                        "description": "anotherNodebody object",
                        "schema": {
                            "$ref": "#/definitions/anotherNode_schema"
                        },
                        "name": "anotherNode",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "summary": "Create anotherNode by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "create_anotherNode_by_id"
            },
            "delete": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "produces": [
                    "application/json"
                ],
                "description": "Delete operation of resource: anotherNode",
                "summary": "Delete anotherNode by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "delete_anotherNode_by_id"
            },
            "get": {
                "responses": {
                    "200": {
                        "description": "Successful operation",
                        "schema": {
                            "$ref": "#/definitions/anotherNode_schema"
                        }
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Retrieve operation of resource: anotherNode",
                "parameters": [],
                "produces": [
                    "application/json"
                ],
                "summary": "Retrieve anotherNode",
                "consumes": [
                    "application/json"
                ],
                "operationId": "retrieve_anotherNode"
            }
        }
    },
    "definitions": {
        "node": {
            "properties": {
                "my-id": {
                    "type": "string"
                }
            }
        },
        "anotherNode_schema": {
            "properties": {
                "another-id": {
                    "type": "string"
                }
            }
        }
    }
}

Example 3:

Input:

module basic3 {
    namespace "urn:acme:yang:basic";
    prefix "basic";

    grouping node {     
        leaf my-id {
            type uint8;
        }   
    }

    container myNode{
        uses node;                      // the parent container uses the grouping
        container another-node {
            leaf another-id {
                type uint8; // the nested container doesn't use any grouping
            }
        }
    }
}

Output:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "description": "basic3 API generated from basic3.yang",
        "title": "basic3 API"
    },
    "host": "localhost:8080",
    "basePath": "/restconf",
    "schemes": [
        "http"
    ],
    "paths": {
        "/config/myNode/": {
            "put": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Update operation of resource: myNode",
                "parameters": [
                    {
                        "required": true,
                        "description": "myNodebody object",
                        "schema": {
                            "$ref": "#/definitions/myNode_schema"
                        },
                        "name": "myNode",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "summary": "Update myNode by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "update_myNode_by_id"
            },
            "post": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Create operation of resource: myNode",
                "parameters": [
                    {
                        "required": true,
                        "description": "myNodebody object",
                        "schema": {
                            "$ref": "#/definitions/myNode_schema"
                        },
                        "name": "myNode",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "summary": "Create myNode by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "create_myNode_by_id"
            },
            "delete": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "produces": [
                    "application/json"
                ],
                "description": "Delete operation of resource: myNode",
                "summary": "Delete myNode by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "delete_myNode_by_id"
            },
            "get": {
                "responses": {
                    "200": {
                        "description": "Successful operation",
                        "schema": {
                            "$ref": "#/definitions/myNode_schema"
                        }
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Retrieve operation of resource: myNode",
                "parameters": [],
                "produces": [
                    "application/json"
                ],
                "summary": "Retrieve myNode",
                "consumes": [
                    "application/json"
                ],
                "operationId": "retrieve_myNode"
            }
        },
        "/config/myNode/another-node/": {
            "put": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Update operation of resource: another-node",
                "parameters": [
                    {
                        "required": true,
                        "description": "another-nodebody object",
                        "schema": {
                            "$ref": "#/definitions/another-node_schema"
                        },
                        "name": "another-node",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "another-node"
                ],
                "summary": "Update another-node by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "update_myNode_another-node_another-node_by_id"
            },
            "post": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Create operation of resource: another-node",
                "parameters": [
                    {
                        "required": true,
                        "description": "another-nodebody object",
                        "schema": {
                            "$ref": "#/definitions/another-node_schema"
                        },
                        "name": "another-node",
                        "in": "body"
                    }
                ],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "another-node"
                ],
                "summary": "Create another-node by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "create_myNode_another-node_another-node_by_id"
            },
            "delete": {
                "responses": {
                    "200": {
                        "description": "Successful operation"
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Delete operation of resource: another-node",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "another-node"
                ],
                "summary": "Delete another-node by ID",
                "consumes": [
                    "application/json"
                ],
                "operationId": "delete_myNode_another-node_another-node_by_id"
            },
            "get": {
                "responses": {
                    "200": {
                        "description": "Successful operation",
                        "schema": {
                            "$ref": "#/definitions/another-node_schema"
                        }
                    },
                    "400": {
                        "description": "Internal Error"
                    }
                },
                "description": "Retrieve operation of resource: another-node",
                "parameters": [],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "another-node"
                ],
                "summary": "Retrieve another-node",
                "consumes": [
                    "application/json"
                ],
                "operationId": "retrieve_myNode_another-node_another-node"
            }
        }
    },
    "definitions": {
        "node": {
            "properties": {
                "my-id": {
                    "type": "string"
                }
            }
        },
        "myNode_schema": {
            "allOf": [
                {
                    "$ref": "#/definitions/node"
                },
                {
                    "properties": {
                        "another-node": {
                            "properties": {
                                "another-id": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            ]
        },
        "another-node_schema": {
            "properties": {
                "another-id": {
                    "type": "string"
                }
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants