-
Notifications
You must be signed in to change notification settings - Fork 12
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: export input and output schemas to components in OpenAPI document #131
Draft
EnriqueL8
wants to merge
1
commit into
hyperledger:main
Choose a base branch
from
kaleido-io:openapi_schemas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,8 @@ | |
"strings" | ||
"time" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
Check failure on line 31 in pkg/ffapi/openapi3.go GitHub Actions / build
|
||
"github.com/getkin/kin-openapi/openapi3gen" | ||
Check failure on line 32 in pkg/ffapi/openapi3.go GitHub Actions / build
|
||
"github.com/hyperledger/firefly-common/pkg/config" | ||
"github.com/hyperledger/firefly-common/pkg/fftypes" | ||
"github.com/hyperledger/firefly-common/pkg/i18n" | ||
|
@@ -188,21 +188,34 @@ | |
} | ||
|
||
func (sg *SwaggerGen) addCustomType(t reflect.Type, schema *openapi3.Schema) { | ||
typeString := "string" | ||
typeString := openapi3.TypeString | ||
switch t.Name() { | ||
case "UUID": | ||
schema.Type = typeString | ||
schema.Type = &openapi3.Types{typeString} | ||
schema.Format = "uuid" | ||
case "FFTime": | ||
schema.Type = typeString | ||
schema.Type = &openapi3.Types{typeString} | ||
schema.Format = "date-time" | ||
case "Bytes32": | ||
schema.Type = typeString | ||
schema.Type = &openapi3.Types{typeString} | ||
schema.Format = "byte" | ||
case "FFBigInt": | ||
schema.Type = typeString | ||
schema.Type = &openapi3.Types{typeString} | ||
case "JSONAny": | ||
schema.Type = "" | ||
schema.Type = &openapi3.Types{} | ||
} | ||
} | ||
|
||
func constructExportComponentSchemasOptions(route *Route) openapi3gen.ExportComponentSchemasOptions { | ||
exportTopLevelSchema := false | ||
if route.ExportTopLevelComponentSchema != nil { | ||
exportTopLevelSchema = *route.ExportTopLevelComponentSchema | ||
} | ||
|
||
return openapi3gen.ExportComponentSchemasOptions{ | ||
ExportComponentSchemas: true, | ||
ExportTopLevelSchema: exportTopLevelSchema, | ||
ExportGenerics: true, | ||
} | ||
} | ||
|
||
|
@@ -216,13 +229,13 @@ | |
switch { | ||
case route.JSONInputSchema != nil: | ||
schemaRef, err = route.JSONInputSchema(ctx, func(obj interface{}) (*openapi3.SchemaRef, error) { | ||
return openapi3gen.NewSchemaRefForValue(obj, doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer)) | ||
return openapi3gen.NewSchemaRefForValue(obj, doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer), openapi3gen.CreateComponentSchemas(constructExportComponentSchemasOptions(route))) | ||
}) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid schema: %s", err)) | ||
} | ||
case route.JSONInputValue != nil: | ||
schemaRef, err = openapi3gen.NewSchemaRefForValue(route.JSONInputValue(), doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer)) | ||
schemaRef, err = openapi3gen.NewSchemaRefForValue(route.JSONInputValue(), doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer), openapi3gen.CreateComponentSchemas(constructExportComponentSchemasOptions(route))) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid schema: %s", err)) | ||
} | ||
|
@@ -236,7 +249,7 @@ | |
props := openapi3.Schemas{ | ||
"filename.ext": &openapi3.SchemaRef{ | ||
Value: &openapi3.Schema{ | ||
Type: "string", | ||
Type: &openapi3.Types{"string"}, | ||
Format: "binary", | ||
}, | ||
}, | ||
|
@@ -245,15 +258,15 @@ | |
props[fp.Name] = &openapi3.SchemaRef{ | ||
Value: &openapi3.Schema{ | ||
Description: i18n.Expand(ctx, i18n.APISuccessResponse), | ||
Type: "string", | ||
Type: &openapi3.Types{"string"}, | ||
}, | ||
} | ||
} | ||
|
||
op.RequestBody.Value.Content["multipart/form-data"] = &openapi3.MediaType{ | ||
Schema: &openapi3.SchemaRef{ | ||
Value: &openapi3.Schema{ | ||
Type: "object", | ||
Type: &openapi3.Types{"object"}, | ||
Properties: props, | ||
}, | ||
}, | ||
|
@@ -287,15 +300,15 @@ | |
switch { | ||
case route.JSONOutputSchema != nil: | ||
schemaRef, err = route.JSONOutputSchema(ctx, func(obj interface{}) (*openapi3.SchemaRef, error) { | ||
return openapi3gen.NewSchemaRefForValue(obj, doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer)) | ||
return openapi3gen.NewSchemaRefForValue(obj, doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer), openapi3gen.CreateComponentSchemas(constructExportComponentSchemasOptions(route))) | ||
}) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid schema: %s", err)) | ||
} | ||
case route.JSONOutputValue != nil: | ||
outputValue := route.JSONOutputValue() | ||
if outputValue != nil { | ||
schemaRef, err = openapi3gen.NewSchemaRefForValue(outputValue, doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer)) | ||
schemaRef, err = openapi3gen.NewSchemaRefForValue(outputValue, doc.Components.Schemas, openapi3gen.SchemaCustomizer(schemaCustomizer), openapi3gen.CreateComponentSchemas(constructExportComponentSchemasOptions(route))) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid schema: %s", err)) | ||
} | ||
|
@@ -339,15 +352,15 @@ | |
exampleValue = example | ||
} | ||
value := &openapi3.Schema{ | ||
Type: "string", | ||
Type: &openapi3.Types{"string"}, | ||
Default: defValue, | ||
Example: exampleValue, | ||
} | ||
if isArray { | ||
value.Type = "array" | ||
value.Type = &openapi3.Types{"array"} | ||
value.Items = &openapi3.SchemaRef{ | ||
Value: &openapi3.Schema{ | ||
Type: "string", | ||
Type: &openapi3.Types{"string"}, | ||
Default: defValue, | ||
Example: exampleValue, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getkin/kin-openapi#914 (comment) FYI