Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

python 3.x - Swagger Flask Python3, JSON $ref not including external file

References to internal sections in swagger.json work correctly, but attempting to reference a JSON file in the same directory seems to be ignored. I have some large example data files so would like to reference them instead of including them in the swagger.json file.

How do I get swagger to dereference the $ref tags for external files in the ./static/ directory (this is the same location as the swagger.json file)? Is there a swagger log file that shows what/how it loads files?

Things that work:

  • http://localhost:5002/swagger
  • http://localhost:5002/static/swagger.json
  • http://localhost:5002/static/components.json

So this proves the web server can find the static/components.json file.

Part of swagger.json:

  "components": {
    "schemas": {

      "id": {
        "properties": {
          "id": {
            "type": "number",
            "format": "int64"
          }
        }
      },

      "componentsPost": {
        "type": "object",
        "properties": {
          "id": {
            "$ref": "#/components/schemas/id"   <-- THIS WORKS
          },
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "component_type_id": {
            "$ref": "#/components/schemas/id"   <-- THIS WORKS
          },
          "system_type_id": {
            "$ref": "#/components/schemas/id"   <-- THIS WORKS
          },
          "parent_id": {
            "$ref": "#/components/schemas/id"
          }
        },
        "example": {
            "$ref": "components.json"      <-- THIS DOES *NOT* WORK
        }
      },

Note: components.json is different than the #components/schema/id reference, which is in the same file as shown above.

Beginning of components.json data file:

{
  "components": [
    {
      "id": 99999,
      "name": "root",
      "description": "Root Component",
      "component_type_id": 1,
      "system_type_id": 1
    },
    {
      "id": 1,
      "name": "lan1",
      "description": "LAN",
      "component_type_id": 1,
      "system_type_id": 4,
      "parent_id": 99999
    },

The swagger.json file gets loaded but components.json does not:

[06/Jan/2021 12:32:53] "GET /static/swagger.json HTTP/1.1" 200 -

The swagger web site shows the $ref, not the expected components.json: swagger pic

Alternate code I have tried:

        "example": {
            "components": {
                "$ref": "components.json"
            }
        }

        "example": {
            "$ref": "/static/components.json"
        }

        "example": {
            "$ref": "./static/components.json"
        }

        "example": {
            "$ref": "static/components.json"
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...