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 4 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
104 changes: 104 additions & 0 deletions tools/codegen/codespec/api_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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, errInvalidSchema
maastha marked this conversation as resolved.
Show resolved Hide resolved
}

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

return resp, nil
}

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

// hard-coding API version for now, this should be dynamically handled to perhaps use the latest API version
jsonMediaType, ok := mediaTypes.Get("application/vnd.atlas.2023-01-01+json")
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved
if ok && jsonMediaType.Schema != nil {
s, err := BuildSchema(jsonMediaType.Schema)
if err != nil {
return nil, err
}
return s, nil
}

sortedMediaTypes := orderedmap.SortAlpha(mediaTypes)
for pair := range orderedmap.Iterate(context.TODO(), sortedMediaTypes) {
mediaType := pair.Value()
if mediaType.Schema != nil {
s, err := BuildSchema(mediaType.Schema)
if err != nil {
return nil, err
}
return s, nil
}
}
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved

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.TODO(), sortedCodes) {
maastha marked this conversation as resolved.
Show resolved Hide resolved
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
}
63 changes: 63 additions & 0 deletions tools/codegen/codespec/api_spec_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package codespec

import (
"fmt"

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

var errInvalidSchema = fmt.Errorf("invalid schema")

type APISpecSchema struct {
Schema *base.Schema
Type string
Format string
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved
}

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

func (s *APISpecSchema) GetComputability(name string) ComputedOptionalRequired {
for _, prop := range s.Schema.Required {
maastha marked this conversation as resolved.
Show resolved Hide resolved
if name == prop {
return Required
}
}

return ComputedOptional
}

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.Format == OASFormatPassword

if !isSensitive {
return nil
}

return &isSensitive
}
Loading