FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow Struct Embedding with `allOf` by clarkjohnd · Pull Request #2086 · oapi-codegen/oapi-codegen · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (7) .yaml  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
1 change: 1 addition & 0 deletions internal/test/all_of/doc.go
This file contains hidden or 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
Expand Up @@ -2,3 +2,4 @@ package allof

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config1.yaml openapi.yaml
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config2.yaml openapi.yaml
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=embed-config.yaml embed-openapi.yaml
7 changes: 7 additions & 0 deletions internal/test/all_of/embed-config.yaml
This file contains hidden or 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,7 @@
package: v2
generate:
models: true
embedded-spec: true
output-options:
skip-prune: true
output: embed/openapi.gen.go
44 changes: 44 additions & 0 deletions internal/test/all_of/embed-openapi.yaml
This file contains hidden or 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,44 @@
openapi: "3.0.1"
info:
version: 1.0.0
title: Tests AllOf composition
paths: {}
components:
schemas:
PersonProperties:
type: object
description: |
These are fields that specify a person. They are all optional, and
would be used by an `Edit` style API endpoint, where each is optional.
properties:
FirstName:
type: string
LastName:
type: string
GovernmentIDNumber:
type: integer
format: int64
FirstName:
type: object
properties:
FirstName:
type: string
Person:
type: object
x-go-allof-embed-refs: true
description: |
This is a person, with mandatory first and last name, but optional ID
number. This would be returned by a `Get` style API. We merge the person
properties with another Schema which only provides required fields.
allOf:
- $ref: "#/components/schemas/PersonProperties"
- required: [ FirstName, LastName ]
- $ref: "#/components/schemas/FirstName"
Comment on lines +33 to +36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Potential overlapping field issue: both PersonProperties (line 34) and FirstName (line 36) define a FirstName property. The validation logic in merge_schemas.go:215-219 checks for overlapping fields and should reject this when x-go-allof-embed-refs: true is set. Verify that this test case generates correctly or if it needs to be updated.

PersonAllOfSingle:
type: object
x-go-allof-embed-refs: true
description: |
This is a person record as returned from a Create endpoint. It contains
all the fields of a Person, with an additional resource UUID.
allOf:
- $ref: "#/components/schemas/Person"
124 changes: 124 additions & 0 deletions internal/test/all_of/embed/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/test/all_of/openapi.yaml
This file contains hidden or 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
Expand Up @@ -51,3 +51,10 @@ components:
type: integer
format: int64
required: [ ID ]
PersonAllOfSingle:
type: object
description: |
This is a person record as returned from a Create endpoint. It contains
all the fields of a Person, with an additional resource UUID.
allOf:
- $ref: "#/components/schemas/Person"
29 changes: 18 additions & 11 deletions internal/test/all_of/v1/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions internal/test/all_of/v2/openapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pkg/codegen/extension.go
This file contains hidden or 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
Expand Up @@ -15,7 +15,9 @@ const (
// extGoName is used to override a field name
extGoName = "x-go-name"
// extGoTypeName is used to override a generated typename for something.
extGoTypeName = "x-go-type-name"
extGoTypeName = "x-go-type-name"
// extGoAllOfEmbedRefs is used to generate allOfs with refs that embedded structs (this generation can fail if it generates objects which have overlapping fields)
extGoAllOfEmbedRefs = "x-go-allof-embed-refs"
extPropGoJsonIgnore = "x-go-json-ignore"
extPropOmitEmpty = "x-omitempty"
extPropOmitZero = "x-omitzero"
Expand All @@ -37,6 +39,18 @@ func extString(extPropValue interface{}) (string, error) {
return str, nil
}

func ReadExtGoAllOfEmbedRefs(extensions map[string]interface{}) (bool, error) {
extPropValue, ok := extensions[extGoAllOfEmbedRefs]
if !ok {
return false, nil
}
v, ok := extPropValue.(bool)
if !ok {
return false, fmt.Errorf("for %s failed to read as boolean: %T", extGoAllOfEmbedRefs, extPropValue)
}
return v, nil
}

func extTypeName(extPropValue interface{}) (string, error) {
return extString(extPropValue)
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL