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

chore: PoC - Model generation - support primitive types at root level #2673

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
93 changes: 93 additions & 0 deletions tools/codegen/codespec/api_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package codespec

import (
"context"
"fmt"
"strconv"

"github.com/pb33f/libopenapi/datamodel/high/base"
high "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
)

var (
errSchemaNotFound = fmt.Errorf("schema not found")
)

// This function only builds the schema from a proxy and returns the basic type and format without handling oneOf, anyOf, allOf, or nullable types.
func BuildSchema(proxy *base.SchemaProxy) (*APISpecSchema, error) {
resp := &APISpecSchema{}

schema, err := proxy.BuildSchema()
if err != nil {
return nil, fmt.Errorf("failed to build schema from proxy: %w", err)
}

if len(schema.Type) == 0 {
return nil, fmt.Errorf("invalid schema. no values for schema.Type found")
}

resp.Type = schema.Type[0]
resp.Schema = schema

return resp, nil
}

func getSchemaFromMediaType(mediaTypes *orderedmap.Map[string, *high.MediaType]) (*APISpecSchema, error) {
if mediaTypes == nil {
return nil, errSchemaNotFound
}

sortedMediaTypes := orderedmap.SortAlpha(mediaTypes)
for pair := range orderedmap.Iterate(context.Background(), sortedMediaTypes) {
mediaType := pair.Value()
if mediaType.Schema != nil {
s, err := BuildSchema(mediaType.Schema)
if err != nil {
return nil, err
}
return s, nil
}
}

return nil, errSchemaNotFound
}

func buildSchemaFromRequest(op *high.Operation) (*APISpecSchema, error) {
if op == nil || op.RequestBody == nil || op.RequestBody.Content == nil || op.RequestBody.Content.Len() == 0 {
return nil, errSchemaNotFound
}

return getSchemaFromMediaType(op.RequestBody.Content)
}

func buildSchemaFromResponse(op *high.Operation) (*APISpecSchema, error) {
if op == nil || op.Responses == nil || op.Responses.Codes == nil || op.Responses.Codes.Len() == 0 {
return nil, errSchemaNotFound
}

okResponse, ok := op.Responses.Codes.Get(OASResponseCodeOK)
if ok {
return getSchemaFromMediaType(okResponse.Content)
}

createdResponse, ok := op.Responses.Codes.Get(OASResponseCodeCreated)
if ok {
return getSchemaFromMediaType(createdResponse.Content)
}

sortedCodes := orderedmap.SortAlpha(op.Responses.Codes)
for pair := range orderedmap.Iterate(context.Background(), sortedCodes) {
responseCode := pair.Value()
statusCode, err := strconv.Atoi(pair.Key())
if err != nil {
continue
}

if statusCode >= 200 && statusCode <= 299 {
return getSchemaFromMediaType(responseCode.Content)
}
}

return nil, errSchemaNotFound
}
59 changes: 59 additions & 0 deletions tools/codegen/codespec/api_spec_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package codespec

import (
"slices"

"github.com/pb33f/libopenapi/datamodel/high/base"
high "github.com/pb33f/libopenapi/datamodel/high/v3"
)

type APISpecSchema struct {
Schema *base.Schema
Type string
}

type APISpecResource struct {
Description *string
DeprecationMessage *string
CreateOp *high.Operation
ReadOp *high.Operation
UpdateOp *high.Operation
DeleteOp *high.Operation
CommonParameters []*high.Parameter
}

func (s *APISpecSchema) GetComputability(name string) ComputedOptionalRequired {
if slices.Contains(s.Schema.Required, name) {
return Required
}

return Optional
}

func (s *APISpecSchema) GetDeprecationMessage() *string {
if s.Schema.Deprecated == nil || !(*s.Schema.Deprecated) {
return nil
}

deprecationMessage := "This attribute has been deprecated"

return &deprecationMessage
}

func (s *APISpecSchema) GetDescription() *string {
if s.Schema.Description == "" {
return nil
}

return &s.Schema.Description
}

func (s *APISpecSchema) IsSensitive() *bool {
isSensitive := s.Schema.Format == OASFormatPassword

if !isSensitive {
return nil
}

return &isSensitive
}
Loading