Skip to content

Commit

Permalink
make openapi property order idempotent.
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Sep 9, 2024
1 parent 375e0df commit 85972a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
22 changes: 11 additions & 11 deletions writer/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {

func WriteServiceToOpenAPI(ps *parser.ParsedService) ([]byte, error) {
openAPI, err := convertToOpenAPI(ps)
if(err != nil) {
if err != nil {
return nil, err
}

Expand All @@ -35,10 +35,10 @@ func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) {
definitions := Definitions{}
for _, r := range service.ResourceByType {
d, err := resourceToSchema(r)
if(err != nil) {
if err != nil {
return nil, err
}
definitions[r.Kind] = d;
definitions[r.Kind] = d
schemaRef := fmt.Sprintf("#/definitions/%v", r.Kind)
if r.Methods.List != nil {
log.Printf("resource plural: %s", r.Plural)
Expand Down Expand Up @@ -166,19 +166,19 @@ func convertToOpenAPI(service *parser.ParsedService) (*OpenAPI, error) {
func resourceToSchema(r *parser.ParsedResource) (Schema, error) {
properties := Properties{}
required := []string{}
for name, p := range r.Properties {
t, err := openAPIType(p)
if(err != nil ) {
for _, f := range r.GetFieldsSortedByNumber() {
t, err := openAPIType(f)
if err != nil {
return Schema{}, err
}
properties[name] = Schema{
properties[f.Name] = Schema{
Type: t.openapi_type,
Format: t.openapi_format,
XTerraformID: name == constants.FIELD_ID_NAME,
ReadOnly: p.ReadOnly,
XTerraformID: f.Name == constants.FIELD_ID_NAME,
ReadOnly: f.ReadOnly,
}
if p.Required {
required = append(required, name)
if f.Required {
required = append(required, f.Name)
}
}
return Schema{
Expand Down
53 changes: 27 additions & 26 deletions writer/openapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,42 @@ package openapi
import (
"fmt"

"github.com/aep-dev/aepc/parser"
"github.com/aep-dev/aepc/schema"
)

type TypeInfo struct {
openapi_type string
openapi_format string
openapi_type string
openapi_format string
}

func openAPIType(p *schema.Property) (TypeInfo, error) {
t := "";
f := "";
func openAPIType(p *parser.ParsedProperty) (TypeInfo, error) {
t := ""
f := ""

switch(p.Type) {
case schema.Type_STRING:
t = "string"
case schema.Type_DOUBLE:
t = "number"
f = "double"
case schema.Type_FLOAT:
t = "number"
f = "float"
case schema.Type_INT32:
t = "integer"
f = "int32"
case schema.Type_INT64:
t = "integer"
f = "int64"
case schema.Type_BOOLEAN:
t = "boolean"
default:
return TypeInfo{}, fmt.Errorf("%s does not have openapi type support", p.Type)
switch p.Type {
case schema.Type_STRING:
t = "string"
case schema.Type_DOUBLE:
t = "number"
f = "double"
case schema.Type_FLOAT:
t = "number"
f = "float"
case schema.Type_INT32:
t = "integer"
f = "int32"
case schema.Type_INT64:
t = "integer"
f = "int64"
case schema.Type_BOOLEAN:
t = "boolean"
default:
return TypeInfo{}, fmt.Errorf("%s does not have openapi type support", p.Type)
}

return TypeInfo{
openapi_type: t,
openapi_type: t,
openapi_format: f,
}, nil
}
}

0 comments on commit 85972a6

Please sign in to comment.