Skip to content

Commit

Permalink
Add description, default, enum and example properties to task component
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Oct 31, 2024
1 parent 4a8a910 commit 70d9fbc
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 39 deletions.
105 changes: 70 additions & 35 deletions db/taskComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"gopkg.in/yaml.v3"
)

// ConfigFile represents the structure of the input JSON configuration
type ConfigFile struct {
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -29,7 +28,6 @@ type ConfigFile struct {
Endpoint string `json:"endpoint"`
}

// SwaggerSpec represents the root of Swagger 2.0 specification
type SwaggerSpec struct {
Swagger string `yaml:"swagger"`
BasePath string `yaml:"basePath"`
Expand All @@ -38,11 +36,15 @@ type SwaggerSpec struct {
}

type SchemaModel struct {
Type string `yaml:"type"`
Required []string `yaml:"required"`
Properties map[string]SchemaModel `yaml:"properties"`
Items *SchemaModel `yaml:"items,omitempty"`
Ref string `yaml:"$ref,omitempty"`
Type string `yaml:"type"`
Required []string `yaml:"required"`
Properties map[string]SchemaModel `yaml:"properties"`
Items *SchemaModel `yaml:"items,omitempty"`
Ref string `yaml:"$ref,omitempty"`
Description string `yaml:"description,omitempty"`
Default interface{} `yaml:"default,omitempty"`
Enum []string `yaml:"enum,omitempty"`
Example interface{} `yaml:"example,omitempty"`
}

type PathItem map[string]Operation
Expand All @@ -60,11 +62,15 @@ type ParameterSchema struct {
}

type Parameter struct {
Name string `yaml:"name"`
In string `yaml:"in"`
Required bool `yaml:"required"`
Type string `yaml:"type"`
Schema *ParameterSchema `yaml:"schema,omitempty"`
Name string `yaml:"name"`
In string `yaml:"in"`
Required bool `yaml:"required"`
Type string `yaml:"type"`
Schema *ParameterSchema `yaml:"schema,omitempty"`
Description string `yaml:"description,omitempty"`
Default interface{} `yaml:"default,omitempty"`
Enum []string `yaml:"enum,omitempty"`
Example interface{} `yaml:"example,omitempty"`
}

func TaskComponentGetByName(name string) *model.TaskComponent {
Expand Down Expand Up @@ -223,29 +229,44 @@ func resolveSchemaRef(ref string, definitions map[string]SchemaModel) SchemaMode
defName := parts[len(parts)-1]
schema := definitions[defName]

// Properties 내부의 ref 처리
for name, prop := range schema.Properties {
if prop.Ref != "" {
// reference가 있는 경우 재귀적으로 처리
resolved := resolveSchemaRef(prop.Ref, definitions)

if prop.Description != "" {
resolved.Description = prop.Description
}
if prop.Default != nil {
resolved.Default = prop.Default
}
if len(prop.Enum) > 0 {
resolved.Enum = prop.Enum
}
if prop.Example != nil {
resolved.Example = prop.Example
}
schema.Properties[name] = resolved
} else if prop.Items != nil && prop.Items.Ref != "" {
// 배열 아이템의 reference 처리
resolvedItem := resolveSchemaRef(prop.Items.Ref, definitions)

if prop.Items.Description != "" {
resolvedItem.Description = prop.Items.Description
}
if prop.Items.Default != nil {
resolvedItem.Default = prop.Items.Default
}
if len(prop.Items.Enum) > 0 {
resolvedItem.Enum = prop.Items.Enum
}
if prop.Items.Example != nil {
resolvedItem.Example = prop.Items.Example
}
prop.Items = &resolvedItem
prop.Type = "array"
schema.Properties[name] = prop
}
}

// Items 내부의 ref 처리
if schema.Items != nil && schema.Items.Ref != "" {
resolvedItem := resolveSchemaRef(schema.Items.Ref, definitions)
schema.Items = &resolvedItem
schema.Type = "array"
}

// Type이 비어있는 경우 처리
if schema.Type == "" {
if len(schema.Properties) > 0 {
schema.Type = "object"
Expand All @@ -261,40 +282,49 @@ func getPropertyType(schema SchemaModel) string {
if schema.Type != "" {
return schema.Type
}

if schema.Ref != "" {
return "object" // reference가 있는 경우 object로 처리
return "object"
}

if len(schema.Properties) > 0 {
return "object"
}

if schema.Items != nil {
return "array"
}

return "object"
}

func convertToPropertyDef(schema SchemaModel) model.PropertyDef {
property := model.PropertyDef{
Type: getPropertyType(schema),
Required: schema.Required,
Properties: make(map[string]model.PropertyDef),
Type: getPropertyType(schema),
Required: schema.Required,
Properties: make(map[string]model.PropertyDef),
Description: schema.Description,
Default: schema.Default,
Enum: schema.Enum,
Example: schema.Example,
}

// 객체 타입인 경우 모든 하위 속성 처리
if property.Type == "object" || len(schema.Properties) > 0 {
for name, prop := range schema.Properties {
property.Properties[name] = convertToPropertyDef(prop)
}
}

// 배열 타입인 경우
if property.Type == "array" && schema.Items != nil {
property.Items = &model.PropertyDef{
Type: getPropertyType(*schema.Items),
Properties: make(map[string]model.PropertyDef),
Type: getPropertyType(*schema.Items),
Properties: make(map[string]model.PropertyDef),
Description: schema.Items.Description,
Default: schema.Items.Default,
Enum: schema.Items.Enum,
Example: schema.Items.Example,
}

// 배열의 items가 객체인 경우 해당 객체의 모든 속성 처리
if len(schema.Items.Properties) > 0 {
for name, prop := range schema.Items.Properties {
property.Items.Properties[name] = convertToPropertyDef(prop)
Expand Down Expand Up @@ -334,7 +364,6 @@ func processEndpoint(connectionID string, spec *SwaggerSpec, targetEndpoint stri
},
}

// Parameters 처리
pathParams := model.ParameterStructure{
Properties: make(map[string]model.PropertyDef),
}
Expand All @@ -349,14 +378,20 @@ func processEndpoint(connectionID string, spec *SwaggerSpec, targetEndpoint stri
pathParams.Required = append(pathParams.Required, param.Name)
}
pathParams.Properties[param.Name] = model.PropertyDef{
Type: param.Type,
Type: param.Type,
Description: param.Description,
Default: param.Default,
Enum: param.Enum,
}
case "query":
if param.Required {
queryParams.Required = append(queryParams.Required, param.Name)
}
queryParams.Properties[param.Name] = model.PropertyDef{
Type: param.Type,
Type: param.Type,
Description: param.Description,
Default: param.Default,
Enum: param.Enum,
}
case "body":
if param.Schema != nil && param.Schema.Ref != "" {
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,17 @@ const docTemplate = `{
"github_com_cloud-barista_cm-cicada_pkg_api_rest_model.PropertyDef": {
"type": "object",
"properties": {
"default": {},
"description": {
"type": "string"
},
"enum": {
"type": "array",
"items": {
"type": "string"
}
},
"example": {},
"items": {
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.PropertyDef"
},
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,17 @@
"github_com_cloud-barista_cm-cicada_pkg_api_rest_model.PropertyDef": {
"type": "object",
"properties": {
"default": {},
"description": {
"type": "string"
},
"enum": {
"type": "array",
"items": {
"type": "string"
}
},
"example": {},
"items": {
"$ref": "#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.PropertyDef"
},
Expand Down
8 changes: 8 additions & 0 deletions pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ definitions:
type: object
github_com_cloud-barista_cm-cicada_pkg_api_rest_model.PropertyDef:
properties:
default: {}
description:
type: string
enum:
items:
type: string
type: array
example: {}
items:
$ref: '#/definitions/github_com_cloud-barista_cm-cicada_pkg_api_rest_model.PropertyDef'
properties:
Expand Down
12 changes: 8 additions & 4 deletions pkg/api/rest/model/taskComponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
)

type PropertyDef struct {
Type string `json:"type"`
Required []string `json:"required,omitempty"`
Properties map[string]PropertyDef `json:"properties,omitempty"`
Items *PropertyDef `json:"items,omitempty"`
Type string `json:"type"`
Required []string `json:"required,omitempty"`
Properties map[string]PropertyDef `json:"properties,omitempty"`
Items *PropertyDef `json:"items,omitempty"`
Description string `json:"description,omitempty"`
Default interface{} `json:"default,omitempty"`
Enum []string `json:"enum,omitempty"`
Example interface{} `json:"example,omitempty"`
}

type ParameterStructure struct {
Expand Down

0 comments on commit 70d9fbc

Please sign in to comment.