Skip to content

Commit

Permalink
Merge pull request koxudaxi#375 from yyamano/issue-222
Browse files Browse the repository at this point in the history
Generate an empty model file if an api spec file have no "schemas".
  • Loading branch information
koxudaxi authored Aug 19, 2023
2 parents 28f8af2 + 30346b4 commit eba51d5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
5 changes: 3 additions & 2 deletions fastapi_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ def generate_code(
parser = OpenAPIParser(input_text)
with chdir(output_dir):
models = parser.parse()
output = output_dir / model_path
if not models:
return
# if no models (schemas), just generate an empty model file.
modules = {output: ("", input_name)}
elif isinstance(models, str):
output = output_dir / model_path
modules = {output: (models, input_name)}
else:
raise Exception('Modular references are not supported in this version')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from typing import List, Optional, Union

from fastapi import FastAPI, Path, Query, UploadFile
from fastapi import FastAPI, Path, Query, Request
from starlette.requests import Request

from .models import (
Expand Down Expand Up @@ -37,7 +37,7 @@ def post_bar(request: Request) -> None:


@app.post('/convert', response_model=bytes)
def convert(format: Optional[str] = 'pdf', file: UploadFile = ...) -> bytes:
def convert(format: Optional[str] = 'pdf', request: Request = ...) -> bytes:
pass


Expand Down
23 changes: 23 additions & 0 deletions tests/data/expected/openapi/default_template/no_models/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# generated by fastapi-codegen:
# filename: no_models.yaml
# timestamp: 2020-06-19T00:00:00+00:00

from __future__ import annotations

from fastapi import FastAPI

app = FastAPI(
version='1.0.0',
title='Swagger Petstore',
license={'name': 'MIT'},
description=None,
servers=[{'url': 'http://petstore.swagger.io/v1'}],
)


@app.get('/hello', response_model=str)
def hello() -> str:
"""
get hello message
"""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated by fastapi-codegen:
# filename: no_models.yaml
# timestamp: 2020-06-19T00:00:00+00:00
22 changes: 22 additions & 0 deletions tests/data/openapi/default_template/no_models.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
description:

servers:
- url: http://petstore.swagger.io/v1
paths:
/hello:
get:
summary: get hello message
operationId: hello
responses:
'200':
description: message string
content:
text/plain:
schema:
type: string

0 comments on commit eba51d5

Please sign in to comment.