-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Output JSON when applying an overlay to a JSON doc (#950)
Improved JSON schema support in the studio: - `run` will now respect input filetype when applying overlays. Specifically, input `json` specs when overlaid will write `json` out. Note this is only relevant / visible to the user now because of the studio - `run` will now reformat single-line input specs. This helps with both the studio and linting output - Adds an integration test confirming that we can pull json documents from the registry Related speakeasy-api/sdk-gen-config#58
- Loading branch information
1 parent
8a61986
commit e2b2fa9
Showing
9 changed files
with
192 additions
and
63 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package schemas | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"github.com/pb33f/libopenapi/json" | ||
"github.com/speakeasy-api/speakeasy-core/openapi" | ||
"github.com/speakeasy-api/speakeasy/internal/utils" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Format reformats a document to the desired output format while preserving key ordering | ||
// Can be used to convert output types, or improve readability (e.g. prettifying single-line documents) | ||
func Format(ctx context.Context, schemaPath string, yamlOut bool) ([]byte, error) { | ||
_, _, model, err := LoadDocument(ctx, schemaPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse document: %w", err) | ||
} | ||
|
||
return Render(model.Index.GetRootNode(), schemaPath, yamlOut) | ||
} | ||
|
||
func Render(y *yaml.Node, schemaPath string, yamlOut bool) ([]byte, error) { | ||
yamlIn := utils.HasYAMLExt(schemaPath) | ||
|
||
if yamlIn && yamlOut { | ||
var res bytes.Buffer | ||
encoder := yaml.NewEncoder(&res) | ||
// Note: would love to make this generic but the indentation information isn't in go-yaml nodes | ||
// https://github.com/go-yaml/yaml/issues/899 | ||
encoder.SetIndent(2) | ||
if err := encoder.Encode(y); err != nil { | ||
return nil, fmt.Errorf("failed to encode YAML: %w", err) | ||
} | ||
return res.Bytes(), nil | ||
} | ||
|
||
// Preserves key ordering | ||
specBytes, err := json.YAMLNodeToJSON(y, " ") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert YAML to JSON: %w", err) | ||
} | ||
|
||
if yamlOut { | ||
// Use libopenapi to convert JSON to YAML to preserve key ordering | ||
_, model, err := openapi.Load(specBytes, schemaPath) | ||
|
||
yamlBytes, err := model.Model.Render() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to Render YAML: %w", err) | ||
} | ||
|
||
return yamlBytes, nil | ||
} else { | ||
return specBytes, nil | ||
} | ||
} |
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.