更新:我发现了这个问题。我在定义中对对象使用"File“一词似乎导致了这个问题。我将对象重命名为"FileThing“作为测试,swagger-codegen生成了预期的模型对象和模块。(也许这与https://github.com/swagger-api/swagger-codegen/issues/3223有关)。
当使用swagger-codegen从OpenAPI v2规范生成Python (Connexion)服务器存根时,我在生成的源代码中没有看到任何模型对象。但是,其他语言/API目标(如go-server )确实生成模型对象。我用的是swagger-codegen-cli-2.3.1.jar。在这一点上,我的规范非常简单,并且是:
swagger: "2.0"
info:
version: 0.0.1
title: Blah API
description: An API for interacting with the blah system
paths:
/files:
get:
description: Retrieves information on files that match the specified criteria
produces:
- "application/json"
parameters:
- name: tags
type: array
description: The tags for which matching files are to be returned
in: query
items:
type: string
responses:
'200':
description: Successful response
schema:
type: array
items:
$ref: "#/definitions/File"
definitions:
File:
type: object
properties:
file_id:
type: "string"
format: "uuid"
path:
type: "string"
tags:
type: array
items:
type: "string"
如您所见,我希望看到为定义部分中定义的File对象创建的模型对象。但是,Source/swagger_server下的模型包仅包含一个base_model.py模块,除了基模型类本身之外没有其他定义。我运行swagger-codegen-cli,如下所示:
sknick@sknick:~/Repo/Blah/Server$ java -jar /home/sknick/Misc/swagger-codegen-cli-2.3.1.jar generate -l python-flask -o ./Source -i ./api.yaml
[main] INFO io.swagger.parser.Swagger20Parser - reading from ./api.yaml
[main] WARN io.swagger.codegen.DefaultCodegen - Empty operationId found for path: GET /files. Renamed to auto-generated operationId: filesGET
[main] INFO io.swagger.codegen.DefaultGenerator - Model File not imported due to import mapping
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/controllers/default_controller.py
[main] INFO io.swagger.codegen.DefaultGenerator - File exists. Skipped overwriting /home/sknick/Repo/Blah/Server/./Source/swagger_server/test/test_default_controller.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/README.md
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/setup.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/tox.ini
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/test-requirements.txt
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/requirements.txt
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/git_push.sh
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.gitignore
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.travis.yml
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/Dockerfile
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.dockerignore
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/__main__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/encoder.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/util.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/controllers/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/models/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/models/base_model_.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/test/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/swagger/swagger.yaml
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.swagger-codegen/VERSION
发布于 2018-05-03 15:31:27
它看起来就像python-烧瓶生成器中的一个bug,它不生成与特定File
类名(如Date
、Set
等)匹配的模型。这里通过importMapping
在DefaultCodegen
类中配置了“忽略模型”行为:
一些生成器(如C# )清除importMapping
,因为它们不使用Java类映射:
importMapping.clear();
但是python-烧瓶生成器并不能做到这一点。它可能也会清除importMapping
,但我不是Python/Flask,所以我不确定。
我建议您在Swagger存储库中使用公开问题,并/或提交一个PR。
与此同时,一些可能的解决办法是:
FileDTO
。importMapping.clear()
,并使用自定义生成器。https://stackoverflow.com/questions/50123074
复制相似问题