Skip to content

Generate anonymous objects referenced in schemas#648

Draft
jamietanna wants to merge 8 commits intooapi-codegen:mainfrom
jamietanna:defect/626
Draft

Generate anonymous objects referenced in schemas#648
jamietanna wants to merge 8 commits intooapi-codegen:mainfrom
jamietanna:defect/626

Conversation

@jamietanna
Copy link
Copy Markdown
Member

@jamietanna jamietanna commented Jun 30, 2022

Closes #626, #636.

@veger
Copy link
Copy Markdown

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
Copy Markdown

riftsin commented Nov 17, 2022

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

@jamietanna
Copy link
Copy Markdown
Member 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

@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
Copy Markdown
Member 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
Copy Markdown
Member 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
Copy Markdown
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
Copy Markdown
Member 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
Copy Markdown

mmorells commented May 9, 2023

Bump

@massenz
Copy link
Copy Markdown

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!

@gainsley
Copy link
Copy Markdown

Another bump for this.

vbargl added a commit to vbargl/oapi-codegen that referenced this pull request Feb 7, 2025
vbargl added a commit to vbargl/oapi-codegen that referenced this pull request Feb 7, 2025
@Tommy-42
Copy link
Copy Markdown

Hey @jamietanna do you have any news for this feature ? We would benefit a lot from this and would like to see it merge.

Do you have any blockers ?

Cheers

@jamietanna jamietanna marked this pull request as draft March 27, 2025 21:03
@jamietanna
Copy link
Copy Markdown
Member Author

We would benefit a lot from this and would like to see it merge.

Do you have any blockers ?

Main blocker is time to be honest 😅

As noted in #1606 the project would appreciate sponsors to help with dedicating time to the project's maintenance, and if the company you're working at would benefit from this, I'd appreciate sponsorship to help the work along

I've got quite a few things to do before v2.5.0 and an ever growing backlog, on top of long standing issues like this that would benefit many users

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

9 participants