-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from grafana/bugfix/124-fix-joinschema-go-cod…
…egen [bugfix] Fork deepmap/oapi-codegen to fix joinSchema empty interface codegen bug
- Loading branch information
Showing
64 changed files
with
10,843 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
-- in.cue -- | ||
package generate | ||
|
||
import "github.com/grafana/thema" | ||
|
||
thema.#Lineage | ||
name: "joined" | ||
joinSchema: { | ||
joinedField1: string | ||
joinedField2: int32 | ||
} | ||
seqs: [ | ||
{ | ||
schemas: [ | ||
{ | ||
normalField1: bool | ||
normalField2: [...string] | ||
} | ||
] | ||
} | ||
] | ||
|
||
-- out/generate/0.0/depointerized.go -- | ||
package defaultchange | ||
|
||
// Joined defines model for joined. | ||
type Joined struct { | ||
JoinedField1 string `json:"joinedField1"` | ||
JoinedField2 int32 `json:"joinedField2"` | ||
NormalField1 bool `json:"normalField1"` | ||
NormalField2 []string `json:"normalField2"` | ||
} | ||
-- out/generate/0.0/expandref.go -- | ||
package defaultchange | ||
|
||
// Joined defines model for joined. | ||
type Joined struct { | ||
JoinedField1 string `json:"joinedField1"` | ||
JoinedField2 int32 `json:"joinedField2"` | ||
NormalField1 bool `json:"normalField1"` | ||
NormalField2 []string `json:"normalField2"` | ||
} | ||
-- out/generate/0.0/godeclincomments.go -- | ||
package defaultchange | ||
|
||
// Joined defines model for joined. | ||
type Joined struct { | ||
JoinedField1 string `json:"joinedField1"` | ||
JoinedField2 int32 `json:"joinedField2"` | ||
NormalField1 bool `json:"normalField1"` | ||
NormalField2 []string `json:"normalField2"` | ||
} | ||
-- out/generate/0.0/group.go -- | ||
package defaultchange | ||
|
||
// JoinedField1 defines model for joinedField1. | ||
type JoinedField1 = string | ||
|
||
// JoinedField2 defines model for joinedField2. | ||
type JoinedField2 = int32 | ||
|
||
// NormalField1 defines model for normalField1. | ||
type NormalField1 = bool | ||
|
||
// NormalField2 defines model for normalField2. | ||
type NormalField2 = []string | ||
-- out/generate/0.0/nil.go -- | ||
package joined | ||
|
||
// Joined defines model for joined. | ||
type Joined struct { | ||
JoinedField1 string `json:"joinedField1"` | ||
JoinedField2 int32 `json:"joinedField2"` | ||
NormalField1 bool `json:"normalField1"` | ||
NormalField2 []string `json:"normalField2"` | ||
} |
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,17 @@ | ||
# Updating this fork of deepmap/oapi-codegen | ||
|
||
The contents of the `oapi-codegen` are copied from the latest `deepmap/oapi-codegen`, with the following changes: | ||
* All packages except `pkg/codegen` and `pkg/util` have been removed, as they are not needed for thema. | ||
* Package refs have been changed from `github.com/deepmap/oapi-codegen` to `github.com/grafana/thema/internal/deepmap/oapi-codegen` | ||
* `TestExamplePetStoreCodeGeneration` and `TestExamplePetStoreCodeGenerationWithUserTemplates` have been removed from `pkg/codegen/codegen_test.go` | ||
* (this is to remove any need to reference the swagger packages, and said tests do not impact the code being used by thema) | ||
* The contents of [this PR](https://github.com/deepmap/oapi-codegen/pull/717) in deepmap/oapi-codegen have been played onto this fork | ||
|
||
A full diff of changes can be found at [diff.txt]. | ||
|
||
When updating this fork, please add whatever changes you make (if different from the main oapi-codegen branch) to the above list and update [diff.txt] accordingly. | ||
This can be done with: | ||
```shell | ||
$ diff <path_to_deepmap>/oapi-codegen/pkg/codegen <path_to_grafana>/thema/internal/deepmap/oapi-codegen/pkg/codegen > <path_to_grafana>/thema/internal/deepmap/diff.txt | ||
``` | ||
If you make changes to other packages, please also include them in the diff. |
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,165 @@ | ||
diff oapi-codegen/pkg/codegen/codegen_test.go thema/internal/deepmap/oapi-codegen/pkg/codegen/codegen_test.go | ||
4d3 | ||
< "bytes" | ||
7,8d5 | ||
< "io" | ||
< "net/http" | ||
11,13d7 | ||
< examplePetstoreClient "github.com/deepmap/oapi-codegen/examples/petstore-expanded" | ||
< examplePetstore "github.com/deepmap/oapi-codegen/examples/petstore-expanded/echo/api" | ||
< "github.com/deepmap/oapi-codegen/pkg/util" | ||
15a10 | ||
> "github.com/grafana/thema/internal/deepmap/oapi-codegen/pkg/util" | ||
31,131d25 | ||
< } | ||
< | ||
< func TestExamplePetStoreCodeGeneration(t *testing.T) { | ||
< | ||
< // Input vars for code generation: | ||
< packageName := "api" | ||
< opts := Configuration{ | ||
< PackageName: packageName, | ||
< Generate: GenerateOptions{ | ||
< EchoServer: true, | ||
< Client: true, | ||
< Models: true, | ||
< EmbeddedSpec: true, | ||
< }, | ||
< } | ||
< | ||
< // Get a spec from the example PetStore definition: | ||
< swagger, err := examplePetstore.GetSwagger() | ||
< assert.NoError(t, err) | ||
< | ||
< // Run our code generation: | ||
< code, err := Generate(swagger, opts) | ||
< assert.NoError(t, err) | ||
< assert.NotEmpty(t, code) | ||
< | ||
< // Check that we have valid (formattable) code: | ||
< _, err = format.Source([]byte(code)) | ||
< assert.NoError(t, err) | ||
< | ||
< // Check that we have a package: | ||
< assert.Contains(t, code, "package api") | ||
< | ||
< // Check that the client method signatures return response structs: | ||
< assert.Contains(t, code, "func (c *Client) FindPetByID(ctx context.Context, id int64, reqEditors ...RequestEditorFn) (*http.Response, error) {") | ||
< | ||
< // Check that the property comments were generated | ||
< assert.Contains(t, code, "// Id Unique id of the pet") | ||
< | ||
< // Check that the summary comment contains newlines | ||
< assert.Contains(t, code, `// Deletes a pet by ID | ||
< // (DELETE /pets/{id}) | ||
< `) | ||
< | ||
< // Make sure the generated code is valid: | ||
< checkLint(t, "test.gen.go", []byte(code)) | ||
< } | ||
< | ||
< func TestExamplePetStoreCodeGenerationWithUserTemplates(t *testing.T) { | ||
< | ||
< userTemplates := map[string]string{"typedef.tmpl": "//blah"} | ||
< | ||
< // Input vars for code generation: | ||
< packageName := "api" | ||
< opts := Configuration{ | ||
< PackageName: packageName, | ||
< Generate: GenerateOptions{ | ||
< Models: true, | ||
< }, | ||
< OutputOptions: OutputOptions{ | ||
< UserTemplates: userTemplates, | ||
< }, | ||
< } | ||
< | ||
< // Get a spec from the example PetStore definition: | ||
< swagger, err := examplePetstore.GetSwagger() | ||
< assert.NoError(t, err) | ||
< | ||
< // Run our code generation: | ||
< code, err := Generate(swagger, opts) | ||
< assert.NoError(t, err) | ||
< assert.NotEmpty(t, code) | ||
< | ||
< // Check that we have valid (formattable) code: | ||
< _, err = format.Source([]byte(code)) | ||
< assert.NoError(t, err) | ||
< | ||
< // Check that we have a package: | ||
< assert.Contains(t, code, "package api") | ||
< | ||
< // Check that the built-in template has been overriden | ||
< assert.Contains(t, code, "//blah") | ||
< } | ||
< | ||
< func TestExamplePetStoreParseFunction(t *testing.T) { | ||
< | ||
< bodyBytes := []byte(`{"id": 5, "name": "testpet", "tag": "cat"}`) | ||
< | ||
< cannedResponse := &http.Response{ | ||
< StatusCode: 200, | ||
< Body: io.NopCloser(bytes.NewReader(bodyBytes)), | ||
< Header: http.Header{}, | ||
< } | ||
< cannedResponse.Header.Add("Content-type", "application/json") | ||
< | ||
< findPetByIDResponse, err := examplePetstoreClient.ParseFindPetByIDResponse(cannedResponse) | ||
< assert.NoError(t, err) | ||
< assert.NotNil(t, findPetByIDResponse.JSON200) | ||
< assert.Equal(t, int64(5), findPetByIDResponse.JSON200.Id) | ||
< assert.Equal(t, "testpet", findPetByIDResponse.JSON200.Name) | ||
< assert.NotNil(t, findPetByIDResponse.JSON200.Tag) | ||
< assert.Equal(t, "cat", *findPetByIDResponse.JSON200.Tag) | ||
diff oapi-codegen/pkg/codegen/merge_schemas.go thema/internal/deepmap/oapi-codegen/pkg/codegen/merge_schemas.go | ||
13c13 | ||
< func MergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) { | ||
--- | ||
> func MergeSchemas(schema *openapi3.Schema, path []string) (Schema, error) { | ||
17c17 | ||
< return mergeSchemasV1(allOf, path) | ||
--- | ||
> return mergeSchemasV1(schema.AllOf, path) | ||
19c19 | ||
< return mergeSchemas(allOf, path) | ||
--- | ||
> return mergeSchemas(schema, path) | ||
22c22,23 | ||
< func mergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) { | ||
--- | ||
> func mergeSchemas(baseSchema *openapi3.Schema, path []string) (Schema, error) { | ||
> allOf := baseSchema.AllOf | ||
25,27c26,27 | ||
< if n == 1 { | ||
< return GenerateGoSchema(allOf[0], path) | ||
< } | ||
--- | ||
> schema := *baseSchema | ||
> schema.AllOf = nil | ||
29,34c29 | ||
< schema, err := valueWithPropagatedRef(allOf[0]) | ||
< if err != nil { | ||
< return Schema{}, err | ||
< } | ||
< | ||
< for i := 1; i < n; i++ { | ||
--- | ||
> for i := 0; i < n; i++ { | ||
diff oapi-codegen/pkg/codegen/operations.go thema/internal/deepmap/oapi-codegen/pkg/codegen/operations.go | ||
26d25 | ||
< "github.com/deepmap/oapi-codegen/pkg/util" | ||
27a27 | ||
> "github.com/grafana/thema/internal/deepmap/oapi-codegen/pkg/util" | ||
diff oapi-codegen/pkg/codegen/schema.go thema/internal/deepmap/oapi-codegen/pkg/codegen/schema.go | ||
257c257 | ||
< mergedSchema, err := MergeSchemas(schema.AllOf, path) | ||
--- | ||
> mergedSchema, err := MergeSchemas(schema, path) | ||
diff oapi-codegen/pkg/codegen/template_helpers.go thema/internal/deepmap/oapi-codegen/pkg/codegen/template_helpers.go | ||
23c23 | ||
< "github.com/deepmap/oapi-codegen/pkg/util" | ||
--- | ||
> "github.com/grafana/thema/internal/deepmap/oapi-codegen/pkg/util" | ||
Common subdirectories: oapi-codegen/pkg/codegen/templates and thema/internal/deepmap/oapi-codegen/pkg/codegen/templates | ||
Common subdirectories: oapi-codegen/pkg/codegen/test_specs and thema/internal/deepmap/oapi-codegen/pkg/codegen/test_specs |
Oops, something went wrong.