-
Notifications
You must be signed in to change notification settings - Fork 212
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
Fix JSON schema output for custom contracts #1427
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,11 +90,10 @@ func addFFIMethod(ctx context.Context, routes []*ffapi.Route, method *fftypes.FF | |
Path: fmt.Sprintf("invoke/%s", method.Pathname), // must match a route defined in apiserver routes! | ||
Method: http.MethodPost, | ||
JSONInputSchema: func(ctx context.Context, schemaGen ffapi.SchemaGenerator) (*openapi3.SchemaRef, error) { | ||
return contractJSONSchema(ctx, &method.Params, hasLocation) | ||
}, | ||
JSONOutputSchema: func(ctx context.Context, schemaGen ffapi.SchemaGenerator) (*openapi3.SchemaRef, error) { | ||
return contractJSONSchema(ctx, &method.Returns, true) | ||
return contractRequestJSONSchema(ctx, &method.Params, hasLocation) | ||
}, | ||
// // TODO change to operation schema | ||
JSONOutputValue: func() interface{} { return &core.OperationWithDetail{} }, | ||
JSONOutputCodes: []int{http.StatusOK}, | ||
PreTranslatedDescription: description, | ||
}) | ||
|
@@ -103,10 +102,10 @@ func addFFIMethod(ctx context.Context, routes []*ffapi.Route, method *fftypes.FF | |
Path: fmt.Sprintf("query/%s", method.Pathname), // must match a route defined in apiserver routes! | ||
Method: http.MethodPost, | ||
JSONInputSchema: func(ctx context.Context, schemaGen ffapi.SchemaGenerator) (*openapi3.SchemaRef, error) { | ||
return contractJSONSchema(ctx, &method.Params, hasLocation) | ||
return contractRequestJSONSchema(ctx, &method.Params, hasLocation) | ||
}, | ||
JSONOutputSchema: func(ctx context.Context, schemaGen ffapi.SchemaGenerator) (*openapi3.SchemaRef, error) { | ||
return contractJSONSchema(ctx, &method.Returns, true) | ||
return contractQueryResponseJSONSchema(ctx, &method.Returns) | ||
}, | ||
JSONOutputCodes: []int{http.StatusOK}, | ||
PreTranslatedDescription: description, | ||
|
@@ -146,10 +145,10 @@ func addFFIEvent(ctx context.Context, routes []*ffapi.Route, event *fftypes.FFIE | |
} | ||
|
||
/** | ||
* Parse the FFI and build a corresponding JSON Schema to describe the request body for "invoke". | ||
* Parse the FFI and build a corresponding JSON Schema to describe the request body for "invoke" or "query". | ||
* Returns the JSON Schema as an `fftypes.JSONObject`. | ||
*/ | ||
func contractJSONSchema(ctx context.Context, params *fftypes.FFIParams, hasLocation bool) (*openapi3.SchemaRef, error) { | ||
func contractRequestJSONSchema(ctx context.Context, params *fftypes.FFIParams, hasLocation bool) (*openapi3.SchemaRef, error) { | ||
paramSchema := make(fftypes.JSONObject, len(*params)) | ||
for _, param := range *params { | ||
paramSchema[param.Name] = param.Schema | ||
|
@@ -193,6 +192,40 @@ func contractJSONSchema(ctx context.Context, params *fftypes.FFIParams, hasLocat | |
return openapi3.NewSchemaRef("", s), nil | ||
} | ||
|
||
/** | ||
* Parse the FFI and build a corresponding JSON Schema to describe the response body for "invoke" or "query". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this response body is only for query outputs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Good catch. Misleading comments are the worst kind of comments. |
||
* Returns the JSON Schema as an `fftypes.JSONObject`. | ||
*/ | ||
func contractQueryResponseJSONSchema(ctx context.Context, params *fftypes.FFIParams) (*openapi3.SchemaRef, error) { | ||
paramSchema := make(fftypes.JSONObject, len(*params)) | ||
for i, param := range *params { | ||
paramName := param.Name | ||
if paramName == "" { | ||
if i > 0 { | ||
paramName = fmt.Sprintf("output%v", i) | ||
} else { | ||
paramName = "output" | ||
} | ||
} | ||
paramSchema[paramName] = param.Schema | ||
} | ||
outputSchema := fftypes.JSONObject{ | ||
"type": "object", | ||
"description": i18n.Expand(ctx, coremsgs.ContractCallRequestOutput), | ||
"properties": paramSchema, | ||
} | ||
b, err := json.Marshal(outputSchema) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s := openapi3.NewSchema() | ||
err = s.UnmarshalJSON(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return openapi3.NewSchemaRef("", s), nil | ||
} | ||
|
||
func buildDetailsTable(ctx context.Context, details map[string]interface{}) string { | ||
keyHeader := i18n.Expand(ctx, coremsgs.APISmartContractDetailsKey) | ||
valueHeader := i18n.Expand(ctx, coremsgs.APISmartContractDetailsKey) | ||
|
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.
Not sure I understand what's left for this TODO?
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.
I think I did it and forgot to remove the comment :) I will remove it