-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
openapi3gen: add CreateComponentSchemas
option to export object schemas to components
#914
Conversation
Signed-off-by: Enrique Lacal <[email protected]>
@EnriqueL8 let me try to test it and thus help you. |
@EnriqueL8 Thanks! |
There is a problem in some cases where it actually makes it worse - I think introducing an annotation to ignore structs to be schemas would be helpful as well. Maybe using the struct tag? So in the case where an api returns an object such as:
The top level should be a schema on the path itself but the type for the object should be in |
Hi! Please run |
CreateComponentSchemas
option to export object schemas to components
Thanks - just making a few more change and will run it |
Signed-off-by: Enrique Lacal <[email protected]>
Signed-off-by: Enrique Lacal <[email protected]>
Signed-off-by: Enrique Lacal <[email protected]>
I'm trying to compile my project having replaced original kin-openapi with your branch. Observe the problem: ../../../go/pkg/mod/github.com/davidebianchi/[email protected]/main.go:115:19: cannot use openapi3.Paths{} (value of type openapi3.Paths) as *openapi3.Paths value in assignment I guess it because the [email protected] refers to the incompatible version of kin-openapi Exactly the same problem as mentioned here https://github.com/davidebianchi/gswagger/actions/runs/7914093080/job/21603115911 |
@EnriqueL8 there is one problem. If there are different types in different packages have the same name then the "NewSchemaRefForValue" produces one component, the same ref. Example: package root:
// Imported package with conflicting type name
import "github.com/getkin/kin-openapi/openapi3gen/subpkg"
type AnotherStruct struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
Field3 string `json:"field3"`
}
type RecursiveType struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
Field3 string `json:"field3"`
AnotherStruct AnotherStruct `json:"children,omitempty"`
// Use imported type
AnotherStruct2 subpkg.AnotherStruct `json:"children,omitempty"`
} package subpkg:
type AnotherStruct struct {
Field4 string `json:"field1"`
} |
Aah I see - is that something that we should document as a limitation as it seems quite improbable? The same thing would occur if that type is cyclic |
@EnriqueL8, I think it makes to supply an optional type name generator and use it at openapi3gen.go:315 ... diving into the code |
@EnriqueL8 please take a look at your forked repository. I have created a MR into your developed branch. I propose a new option making it possible to customize the generated type name |
thanks @sskserk I'll take a look |
type name builder
Added your commit |
@EnriqueL8 let me create a couple of additional tests. I'll bring an additional MR into the current branch. |
@sskserk awesome - I'm also testing this |
Signed-off-by: Enrique Lacal <[email protected]>
Signed-off-by: Enrique Lacal <[email protected]>
I'm wondering if we should have a customiser for ignoring components instead of only top level? will leave to the reviewer to decide @fenollp fyi |
@sskserk I've found another issue here when you have a generic field, ignoring the top level is not enough if it's nested so you want to be able to not add generics to component schemas as you will get one per type use which is awful |
Signed-off-by: Enrique Lacal <[email protected]>
Pushed up a commit for handling generics - I'm sure there is a better way so any thoughts appreciated |
Signed-off-by: Enrique Lacal <[email protected]>
@EnriqueL8 & @fenollp , let me just share the news. A related project has been fixed as well davidebianchi/gswagger#148 |
This is ready for review |
Signed-off-by: Enrique Lacal <[email protected]>
Signed-off-by: Enrique Lacal <[email protected]>
@fenollp I'm seeing a lot of issues with It might be that I am using validate wrong and should be loading the document generated into a loader and the validate it? Okay that works if I do:
Maybe there is a flag on the validate to allow references? or is that not a valid OpenAPI3 document? |
I've noticed another issue with @fenollp if you get a chance would appreciate a first review as well 😃 |
Signed-off-by: Enrique Lacal <[email protected]>
Alright pushed up fix for maps, only perform the export for structs |
@fenollp do you think this MR is ready to be merged? |
@fenollp any bandwidth to take a look at this? Note would appreciate your input on #914 (comment) |
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.
Mostly nitpicks, LGTM!
|
||
// Best way I could find to check that | ||
// this current type is a generic | ||
isGeneric, err := regexp.Match(`^.*\[.*\]$`, []byte(t.Name())) |
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.
How about just checking that the last rune is ]
?
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.
would be an array in that case?
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.
ah yes, then runes[-1] == ']' && runes[-2] != '['
@fenollp thanks for the review! Will push changes up soon |
address comment
Signed-off-by: Enrique Lacal <[email protected]>
Signed-off-by: Enrique Lacal <[email protected]>
Signed-off-by: Enrique Lacal <[email protected]>
@fenollp Ready for re-review 😃 |
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.
Looks like it's all down to that regex thing and we're good :)
|
||
// Best way I could find to check that | ||
// this current type is a generic | ||
isGeneric, err := regexp.Match(`^.*\[.*\]$`, []byte(t.Name())) |
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.
ah yes, then runes[-1] == ']' && runes[-2] != '['
Hey there! Please rebase & I'll make this move along. |
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.
Rebase please
@fenollp Thanks, sorry was away and not available to rebase! It's a shame that the commit was squashed and not awarded to the contributors of the PR |
Hi @EnriqueL8, you're right. I messed up while rebasing your PR and the shared attribution went to someone that's not even taken part in that part of the lib. I should have checked, sorry. I do not mean to steal anyone's work and would very much prefer to give attribution where it's deserved (e.g. here). So I suggest this: send a PR that both reverts then applies your commit (add some changes if you want but that's not needed). At least this way attribution is assured. If you have other ideas, I'm open :) |
Sounds good - I'll do that! |
PR to start the discussion on #909