如何在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
- 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] - Copy the model classes generated by Swagger Codegen (from
python-flask/swagger_server/models/) into this package. For example, take theDogclass fromdog.pyand paste it intopet_core_models/dog.py. Whenever you update your Swagger YAML, regenerate the models and sync them to this package.
- Spin up a new Python project (e.g.,
Update your API project to use the shared package
- Add the shared package to your API project’s dependencies. For local development, use an editable install:
For production, publish it to a private PyPI server or include it as a git submodule.pip install -e ../pet_core_models - Replace the auto-generated model files in
swagger_server/models/with simple imports from the shared package. Forswagger_server/models/dog.py:
This lets Connexion/Flask find the models it needs while pulling the actual definition from your shared package.from pet_core_models.dog import Dog __all__ = ['Dog']
- Add the shared package to your API project’s dependencies. For local development, use an editable install:
Update your BL-App to use the shared package
Addpet_core_modelsto 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’sswagger_serverdirectory as a dependency in your BL-App’srequirements.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:
Useopenapi-generatorto generate API models, anddatamodel-code-generatorto 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 usingpydantic, then usepydantic2openapito 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()andfrom_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

