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 b11fa43
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 81 deletions.
54 changes: 27 additions & 27 deletions example/bookstore/v1/bookstore.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions example/bookstore/v1/bookstore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ service Bookstore {

// A Book resource.
message Book {
// Field for edition.
int32 edition = 4;

// Field for isbn.
string isbn = 1;

// Field for price.
float price = 2;

Expand All @@ -78,6 +72,12 @@ message Book {

// Field for id.
string id = 10001;

// Field for edition.
int32 edition = 4;

// Field for isbn.
string isbn = 1;
}

// A Create request for a Book resource.
Expand Down
18 changes: 9 additions & 9 deletions example/bookstore/v1/bookstore.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,6 @@
"v1Book": {
"type": "object",
"properties": {
"edition": {
"type": "integer",
"format": "int32",
"description": "Field for edition."
},
"isbn": {
"type": "string",
"description": "Field for isbn."
},
"price": {
"type": "number",
"format": "float",
Expand All @@ -323,6 +314,15 @@
"id": {
"type": "string",
"description": "Field for id."
},
"edition": {
"type": "integer",
"format": "int32",
"description": "Field for edition."
},
"isbn": {
"type": "string",
"description": "Field for isbn."
}
},
"description": "A Book resource."
Expand Down
4 changes: 2 additions & 2 deletions example/bookstore/v1/bookstore_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
"Book": {
"type": "object",
"required": [
"published",
"isbn",
"price",
"published"
"price"
],
"properties": {
"edition": {
Expand Down
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 b11fa43

Please sign in to comment.