Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openapi): fix doc import and publish update(1.14) #1053

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/dashboard/apigateway/apigateway/apps/support/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def save_released_resource_doc(self, resource_doc_version, force: bool = False)
)
for doc in resource_doc_version.data
]
self.bulk_create(resource_doc_to_add, batch_size=RELEASED_RESOURCE_DOC_CREATE_BATCH_SIZE)
# 异步同时(多个stage同时发布同一版本)更新会存在一些冲突问题
self.bulk_create(
resource_doc_to_add, batch_size=RELEASED_RESOURCE_DOC_CREATE_BATCH_SIZE, ignore_conflicts=True
)

def clear_unreleased_resource_doc(self, gateway_id: int) -> None:
"""清理未发布的资源文档,如已发布版本被新版本替代的情况"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def parse(self):

def _get_parser(self, parse_result) -> BaseParser:
if self.version == OPENAPIV2:
return BaseParser(parse_result.specification)
return BaseParser(parse_result.specification, str(self.version))

return OpenAPIV3Parser(parse_result.specification)
return OpenAPIV3Parser(parse_result.specification, str(self.version))

def get_resource_list(self, raw=False):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from urllib.parse import urlparse

from django.utils.translation import gettext as _
from openapi_spec_validator.versions import OPENAPIV30, OPENAPIV31
from pydantic import parse_obj_as

from apigateway.biz.constants import OpenAPIFormatEnum
Expand All @@ -46,6 +47,7 @@ class BaseParser:
"""

_openapi_data: Dict[str, Any]
_openapi_version: Optional[str] = str(OPENAPIV30)

def get_resources(self) -> List[Dict[str, Any]]:
resources = []
Expand Down Expand Up @@ -107,7 +109,7 @@ def _get_openapi_schema(self, operation: Dict[str, Any]):
}
}
"""
openapi_schema = {}
openapi_schema = {"version": self._openapi_version}
request_body = self._get_request_body(operation)
if len(request_body) > 0:
openapi_schema["requestBody"] = request_body
Expand Down Expand Up @@ -330,7 +332,7 @@ def _get_base_path(self):
return parsed_url.path

def _get_openapi_schema(self, operation: Dict[str, Any]):
openapi_schema = {}
openapi_schema = {"version": self._openapi_version}
if "parameters" in operation:
openapi_schema["parameters"] = operation.get("parameters", [])

Expand Down Expand Up @@ -469,8 +471,12 @@ def to_openapi(self, resources: list, file_type: str = "") -> str:
return yaml_export_dumps(content)

def _get_openapi_content(self, resources: list) -> Dict[str, Any]:
openapi_version = "3.0.1"
if resources and resources[0].get("openapi_schema", {}).get("version") == str(OPENAPIV31):
openapi_version = "3.1.0"

return {
"openapi": "3.0.1",
"openapi": openapi_version,
"servers": [{"url": "/"}],
"info": {
"version": self.api_version,
Expand Down Expand Up @@ -520,6 +526,10 @@ def _generate_paths(self, resources: List[Dict]) -> Dict[str, Any]:
# schema
schema = resource.get("openapi_schema", {})

# remove openapi version
if "version" in schema:
del schema["version"]

operation.update(schema)

if self.include_bk_apigateway_resource:
Expand Down
3 changes: 2 additions & 1 deletion src/dashboard/apigateway/apigateway/core/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ def save_released_resource(self, resource_version, force: bool = False) -> None:
)
for resource in resource_version.data
]
self.bulk_create(resource_to_add, batch_size=RELEASED_RESOURCE_CREATE_BATCH_SIZE)
# 异步同时(多个stage同时发布同一版本)更新会存在一些冲突问题
self.bulk_create(resource_to_add, batch_size=RELEASED_RESOURCE_CREATE_BATCH_SIZE, ignore_conflicts=True)

def get_released_resource(self, gateway_id: int, resource_version_id: int, resource_name: str) -> Optional[dict]:
released_resource = self.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ class TestResourceImportCheckApi:
},
"labels": ["pet"],
"openapi_schema": {
"version": "OpenAPIV2.0",
"parameters": [
{
"name": "userId",
Expand All @@ -488,7 +489,7 @@ class TestResourceImportCheckApi:
"description": "ID of User",
"schema": {"type": "integer", "format": "int64"},
}
]
],
},
"plugin_configs": None,
}
Expand Down Expand Up @@ -555,7 +556,9 @@ class TestResourceImportCheckApi:
"config": {"method": "GET", "path": "/hello/", "match_subpath": False, "timeout": 30},
},
"labels": ["pet"],
"openapi_schema": {},
"openapi_schema": {
"version": "OpenAPIV2.0",
},
"plugin_configs": None,
}
],
Expand Down
Loading