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

feat(openapi): adding x-aep-resource annotation #32

Merged
merged 1 commit into from
Oct 14, 2024
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
103 changes: 56 additions & 47 deletions example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"200": {
"schema": {
"items": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
}
Expand All @@ -25,7 +25,7 @@
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
},
Expand All @@ -34,7 +34,7 @@
"in": "body",
"name": "body",
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
},
{
Expand All @@ -52,7 +52,7 @@
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
},
Expand All @@ -62,7 +62,7 @@
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
},
Expand All @@ -72,7 +72,7 @@
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
},
Expand All @@ -81,7 +81,7 @@
"in": "body",
"name": "body",
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
]
Expand All @@ -90,7 +90,7 @@
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
},
Expand All @@ -99,53 +99,62 @@
"in": "body",
"name": "body",
"schema": {
"$ref": "#/definitions/Book"
"$ref": "#/components/schemas/Book"
}
}
]
}
}
},
"definitions": {
"Book": {
"type": "object",
"required": [
"isbn",
"price",
"published"
],
"properties": {
"author": {
"type": "array",
"items": {
"type": "object"
}
},
"edition": {
"type": "integer",
"format": "int32"
},
"id": {
"type": "string",
"readOnly": true,
"x-terraform-id": true
},
"isbn": {
"type": "array",
"items": {
"type": "string"
"components": {
"schemas": {
"Book": {
"type": "object",
"required": [
"isbn",
"price",
"published"
],
"properties": {
"author": {
"type": "array",
"items": {
"type": "object"
}
},
"edition": {
"type": "integer",
"format": "int32"
},
"id": {
"type": "string",
"readOnly": true,
"x-terraform-id": true
},
"isbn": {
"type": "array",
"items": {
"type": "string"
}
},
"path": {
"type": "string",
"readOnly": true
},
"price": {
"type": "number",
"format": "float"
},
"published": {
"type": "boolean"
}
},
"path": {
"type": "string",
"readOnly": true
},
"price": {
"type": "number",
"format": "float"
},
"published": {
"type": "boolean"
"x-aep-resource": {
"singular": "Book",
"plural": "books",
"patterns": [
"/books/{book}"
]
}
}
}
Expand Down
60 changes: 40 additions & 20 deletions writer/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ func WriteServiceToOpenAPI(ps *parser.ParsedService) ([]byte, error) {

func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) {
paths := Paths{}
definitions := Definitions{}
components := Components{
Schemas: Schemas{},
}
for _, r := range service.ResourceByType {
d, err := resourceToSchema(r)
if err != nil {
return nil, err
}
definitions[r.Kind] = d
components.Schemas[r.Kind] = d
if !r.IsResource {
continue
}
schemaRef := fmt.Sprintf("#/definitions/%v", r.Kind)
schemaRef := fmt.Sprintf("#/components/schemas/%v", r.Kind)
if r.Methods.List != nil {
log.Printf("resource plural: %s", r.Plural)
listPath := fmt.Sprintf("/%s", lowerizer.String(r.Plural))
Expand Down Expand Up @@ -160,9 +162,9 @@ func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) {
Title: service.Service.Name,
Version: "version not set",
},
Schemes: []string{"http"},
Paths: paths,
Definitions: definitions,
Schemes: []string{"http"},
Paths: paths,
Components: components,
}
return openAPI, nil
}
Expand Down Expand Up @@ -231,6 +233,13 @@ func resourceToSchema(r *parser.ParsedResource) (Schema, error) {
Type: "object",
Properties: &properties,
Required: required,
XAEPResource: &XAEPResource{
Singular: r.Kind,
Plural: r.Plural,
Patterns: []string{
fmt.Sprintf("/%s/{%s}", lowerizer.String(r.Plural), lowerizer.String(r.Kind)),
},
},
}, nil
}

Expand All @@ -244,11 +253,11 @@ func addMethodToPath(paths Paths, path, method string, methodInfo MethodInfo) {
}

type OpenAPI struct {
Swagger string `json:"swagger"`
Info Info `json:"info"`
Schemes []string `json:"schemes"`
Paths Paths `json:"paths"`
Definitions Definitions `json:"definitions"`
Swagger string `json:"swagger"`
Info Info `json:"info"`
Schemes []string `json:"schemes"`
Paths Paths `json:"paths"`
Components Components `json:"components"`
}

type Info struct {
Expand All @@ -258,7 +267,11 @@ type Info struct {

type Paths map[string]Methods

type Definitions map[string]Schema
type Components struct {
Schemas Schemas `json:"schemas"`
}

type Schemas map[string]Schema

type Methods map[string]MethodInfo

Expand All @@ -284,14 +297,21 @@ type ParameterInfo struct {
}

type Schema struct {
Ref string `json:"$ref,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Items *Schema `json:"items,omitempty"`
Properties *Properties `json:"properties,omitempty"`
XTerraformID bool `json:"x-terraform-id,omitempty"`
Ref string `json:"$ref,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Required []string `json:"required,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Items *Schema `json:"items,omitempty"`
Properties *Properties `json:"properties,omitempty"`
XTerraformID bool `json:"x-terraform-id,omitempty"`
XAEPResource *XAEPResource `json:"x-aep-resource,omitempty"`
}

type Properties map[string]Schema

type XAEPResource struct {
Singular string `json:"singular,omitempty"`
Plural string `json:"plural,omitempty"`
Patterns []string `json:"patterns,omitempty"`
}
Loading