Skip to content
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

Generate anonymous objects referenced in schemas #648

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jamietanna
Copy link
Collaborator

@jamietanna jamietanna commented Jun 30, 2022

Closes #626, #636.

pkg/codegen/schema.go Outdated Show resolved Hide resolved
@veger
Copy link

veger commented Jul 13, 2022

I am very interested to try it, when it is finished, as I think it is solving a similar issue I am experiencing... 👍

@riftsin
Copy link

riftsin commented Nov 17, 2022

Hi guys, I was wondering if there was any update on that issue ?

@jamietanna
Copy link
Collaborator Author

jamietanna commented Nov 23, 2022

@deepmap-marcinr I think I may need a hand with the PascalSchema error, currently trying to work out the intent of those tests to work out what handling needs to be added

pkg/codegen/codegen.go Outdated Show resolved Hide resolved
@jamietanna jamietanna force-pushed the defect/626 branch 2 times, most recently from 9ef3dd1 to 52a07d9 Compare November 25, 2022 17:52
@jamietanna jamietanna marked this pull request as ready for review November 25, 2022 17:54
@jamietanna
Copy link
Collaborator Author

I've got this working finally 👏

If you're happy with it, @deepmap-marcinr, I'll sort out the Git history then merge it.

I'll test it out with a few other specs that have been mentioned to be affected, too

@jamietanna
Copy link
Collaborator Author

jamietanna commented Nov 27, 2022

Got a couple of tweaks to make:

  • anonymous requestBodies
  • anonymous parameters
  • handle when there's i.e. content: {}

jamietanna and others added 6 commits November 27, 2022 16:00
rew! handle absent content/mediaType
As part of future changes we need to be able to reference all the types
in the defined `responses` of a specification.

However, as we're currently only rendering `application/json` media
types, this fails.

Instead, we can generate `json.RawMessage` (which is an alias of
`[]byte`) for these types, which allow referencing them, and
(un)marshaling data to the types.
As part of future changes we need to be able to reference all the types
in the defined `requestBodies` of a specification.

However, as we're currently only rendering `application/json` media
types, this fails.

Instead, we can generate `json.RawMessage` (which is an alias of
`[]byte`) for these types, which allow referencing them, and
(un)marshaling data to the types.
Jamie Tanna and others added 2 commits November 28, 2022 17:41
Previously, there was no way to render any anonymous types that are
referenced in the `paths` of an OpenAPI specification.

Although there is a strong preference in the community to use the
`/components/schemas/`, there are times that it's out of our control to
move objects around.

This generates both the `requestBody` and `responses`.

TODO Closes 626 TODO
} else {
typeName += ToCamelCase(strings.ToLower(operationName)) + ToCamelCase(pathName)
}
typeName += fmt.Sprintf("ResponseBody%s", ToCamelCase(statusCodeOrDefault))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this sufficient to avoid type conflict? What if a request has two response bodies with different content types for the same response code? What happens in that case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In that case, yep, it'll handle the conflict well enough

i.e.:

// NewPetResponseBody200ApplicationYaml defines model for the response body for POST /pets (200, application/yaml).
type NewPetResponseBody200ApplicationYaml = map[string]interface{}

// NewPetResponseBody200TextPlain defines model for the response body for POST /pets (200, text/plain).
type NewPetResponseBody200TextPlain = map[string]interface{}

@mmorells
Copy link

mmorells commented May 9, 2023

Bump

@massenz
Copy link

massenz commented Oct 3, 2023

Is there any updated on this?
It is impacting using go generate for k8s Object DeepCopy etc, as the inlined anonymous structs cannot be consumed to generate the functions.

Simplifying the issue, we have this generated code:

type MyBucket struct {
	Spec *struct {
		
		ArchivePeriod *BucketSpecArchivePeriod `json:"archivePeriod,omitempty"`
	 	Cors          *struct {
	 		 ...
	 	}
	 	// other fields
}       

when running go generated ./generated we get the following error:

└─( go generate ./generated
command-line-arguments:-: name requested for invalid type: struct{Cors *struct{MaxAgeSeconds *float32 "json:\"maxAgeSeconds,omitempty\""; Methods *[]string "json:\"methods,omitempty\""; Origins *[]string "json:\"origins,omitempty\""; ResponseHeaders *[]string "json:\"responseHeaders,omitempty\""} "json:\"cors,omitempty\""; Status *struct{FromProvider *map[string]interface{} "json:\"fromProvider,omitempty\""} "json:\"status,omitempty\""}
Error: not all generators ran successfully
run `controller-gen object paths=types.gen.go -w` to see all available markers, or `controller-gen object paths=types.gen.go -h` for usage
generated/types.gen.go:6: running "controller-gen": exit status 1

By trying a few different combinations, it appears that the anonymoust structs are the cause of the error.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generation of anonymous object schemas doesn't seem to work
7 participants