You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在API应用与业务逻辑应用间共享Swagger生成的数据模型?

模型共享的最佳实现方式

Great question—separating your API layer from business logic while reusing models is a smart move, and there are a few solid approaches tailored to your Flask/Connexion + Swagger setup. Let’s break down the best options:

方案1:抽离模型为独立共享Python包(最推荐)

This is the cleanest, most scalable approach—it fully decouples your API and BL-App, centering all model definitions in a single source of truth. Here’s how to set it up:

  • Create a standalone model package

    1. Spin up a new Python project (e.g., pet_core_models) with a standard structure:
      pet_core_models/
      ├── pyproject.toml  # or setup.py for older projects
      └── pet_core_models/
          ├── __init__.py
          ├── dog.py
          └── [other model files]
      
    2. Copy the model classes generated by Swagger Codegen (from python-flask/swagger_server/models/) into this package. For example, take the Dog class from dog.py and paste it into pet_core_models/dog.py. Whenever you update your Swagger YAML, regenerate the models and sync them to this package.
  • Update your API project to use the shared package

    1. Add the shared package to your API project’s dependencies. For local development, use an editable install:
      pip install -e ../pet_core_models
      
      For production, publish it to a private PyPI server or include it as a git submodule.
    2. Replace the auto-generated model files in swagger_server/models/ with simple imports from the shared package. For swagger_server/models/dog.py:
      from pet_core_models.dog import Dog
      __all__ = ['Dog']
      
      This lets Connexion/Flask find the models it needs while pulling the actual definition from your shared package.
  • Update your BL-App to use the shared package
    Add pet_core_models to your BL-App’s dependencies, then import models directly:

    from pet_core_models.dog import Dog
    
    # Use the Dog class in your business logic, e.g.:
    def get_all_dogs():
        return [Dog(name="Hasso", age=21), Dog(name="Lassy", age=15)]
    

Why this works:

  • Models are maintained once, eliminating duplication.
  • API and BL-App are fully decoupled—changes to one don’t break the other (as long as model contracts stay compatible).
  • Easy to version control the model package, so you can roll back or test changes safely.

方案2:让BL-App直接依赖API生成的模型(最简单)

If your Swagger YAML is the single source of truth for your data structures, you can skip the shared package and have your BL-App use the models generated by Swagger Codegen directly.

  • Link the API models to your BL-App
    Add the API project’s swagger_server directory as a dependency in your BL-App’s requirements.txt:
    -e ../python-flask/swagger_server
    
  • Import models in your BL-App
    Use the same imports as the API layer:
    from swagger_server.models.dog import Dog
    
    # Your business logic here
    

Pros & Cons:

  • Pros: No extra setup—reuse the auto-generated models directly.
  • Cons: Creates tight coupling between your BL-App and API project. If you restructure the API project (e.g., move models), you’ll have to update imports in your BL-App.

方案3:双向生成模型(for strict sync needs)

If you want to define models in one place and auto-generate code for both the API and BL-App, use code generation tools to keep everything in sync:

  • If Swagger YAML is your source:
    Use openapi-generator to generate API models, and datamodel-code-generator to generate pydantic-style models for your BL-App from the same YAML. You can script this in a CI pipeline to auto-update models whenever your Swagger spec changes.
  • If BL-App models are your source:
    Define models in your BL-App using pydantic, then use pydantic2openapi to generate a Swagger YAML from those models. Use that YAML to generate your API code with Swagger Codegen.

Why this works:

  • Ensures 100% consistency between API and BL-App models—no manual syncing needed.
  • Best for teams where model definitions change frequently.

Quick Tips

  • Make sure serialization/deserialization logic aligns between both layers. Swagger-generated models often include to_dict() and from_dict() methods—use those in your BL-App to keep data handling consistent.
  • If you go the shared package route, add version numbers to avoid unexpected breaking changes between API and BL-App.

内容的提问来源于stack exchange,提问作者Maus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:00:13