Skip to content

fix(template): fix strict-interface template #1132

Merged
deepmap-marcinr merged 1 commit intooapi-codegen:masterfrom
sarathsp06:fix-tmpl-txt
Jul 28, 2023
Merged

fix(template): fix strict-interface template #1132
deepmap-marcinr merged 1 commit intooapi-codegen:masterfrom
sarathsp06:fix-tmpl-txt

Conversation

@sarathsp06
Copy link
Copy Markdown
Contributor

fix strict-interface template to make sure text response content type uses string type.

This is in reference to #1130 . The code change is probably not semantically correct, happy to make appropriate changes

…se content type uses string type

Signed-off-by: Sarath Sadasivan Pillai <sarathsp06@gmail.com>
@deepmap-marcinr deepmap-marcinr merged commit df2cf43 into oapi-codegen:master Jul 28, 2023
debuggerpk pushed a commit to breuHQ/oapi-codegen that referenced this pull request Sep 18, 2023
…se content type uses string type (oapi-codegen#1132)

Signed-off-by: Sarath Sadasivan Pillai <sarathsp06@gmail.com>
@tarampampam
Copy link
Copy Markdown

@sarathsp06 @deepmap-marcinr Looks like this PR broke the echo strict server:

  /healthz:
    get:
      tags: [Service]
      summary: Liveness probe
      description: Is the app alive or dead?
      operationId: livenessProbe
      responses:
        '200':
          description: Ok
          content: {text/plain: {example: OK}}
          headers: {X-Request-Id: {schema: {$ref: '#/components/schemas/RequestID'}, description: Request ID}}

image

Text
type LivenessProbe200TextResponse string

func (response LivenessProbe200TextResponse) VisitLivenessProbeResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "text/plain")
	w.Header().Set("X-Request-Id", fmt.Sprint(response.Headers.XRequestId))
	w.WriteHeader(200)

	_, err := w.Write([]byte(response.Body))
	return err
}
Config
package: openapi

generate:
  echo-server: true
  strict-server: true
  models: true
  embedded-spec: true

danicc097 pushed a commit to danicc097/oapi-codegen that referenced this pull request Aug 31, 2024
…se content type uses string type (oapi-codegen#1132)

Signed-off-by: Sarath Sadasivan Pillai <sarathsp06@gmail.com>
jamietanna pushed a commit that referenced this pull request Sep 20, 2024
…t response content type uses string type (#1132)"

This reverts commit df2cf43.

As raised in #1676, this was a breaking change, and means that
`text/plain` responses cannot interact with their headers.

Closes #1676.
@jamietanna
Copy link
Copy Markdown
Member

jamietanna commented Sep 20, 2024

This is being reverted as part of #1773, apologies for the impact!

jamietanna pushed a commit that referenced this pull request Sep 20, 2024
…t response content type uses string type (#1132)"

This reverts commit df2cf43.

As raised in #1676, this was a breaking change, and means that
`text/plain` responses cannot interact with their headers.

Closes #1676.
mromaszewicz added a commit to mromaszewicz/oapi-codegen that referenced this pull request Feb 16, 2026
…api-codegen#2190)

When a response is defined as a $ref to a component response with
text/plain content type (and no headers), the strict-server template
generated a struct-embedding type whose Visit method tried to call
[]byte(response) on a struct, which failed to compile.

The root cause was the revert of PR oapi-codegen#1132 (commit 891a067), which had
originally fixed this by making all text responses string types. That
PR was reverted because it broke text responses with headers (oapi-codegen#1676),
which require the struct form with a Body field.

The fix extends the existing multipart special case in Branch 1A of the
strict-interface template to also cover text responses without headers.
This generates a named type alias (e.g. `type GetTest401TextResponse
UnauthorizedTextResponse`) instead of a struct embedding, so
[]byte(response) compiles and works correctly.

Text responses with headers continue to go through Branch 1C (struct
with Body + Headers fields), so oapi-codegen#1676 is unaffected — confirmed by
verifying no diff in internal/test/issues/issue-1676/ping.gen.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mromaszewicz added a commit that referenced this pull request Feb 16, 2026
…2225)

* test: add regression test for issue #2190

Add a minimal reproduction for invalid generated code when reusing
response components. The generated VisitGetTestResponse method attempts
[]byte(response) on a struct type, which does not compile.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: generate correct type for $ref text responses in strict server (#2190)

When a response is defined as a $ref to a component response with
text/plain content type (and no headers), the strict-server template
generated a struct-embedding type whose Visit method tried to call
[]byte(response) on a struct, which failed to compile.

The root cause was the revert of PR #1132 (commit 891a067), which had
originally fixed this by making all text responses string types. That
PR was reverted because it broke text responses with headers (#1676),
which require the struct form with a Body field.

The fix extends the existing multipart special case in Branch 1A of the
strict-interface template to also cover text responses without headers.
This generates a named type alias (e.g. `type GetTest401TextResponse
UnauthorizedTextResponse`) instead of a struct embedding, so
[]byte(response) compiles and works correctly.

Text responses with headers continue to go through Branch 1C (struct
with Body + Headers fields), so #1676 is unaffected — confirmed by
verifying no diff in internal/test/issues/issue-1676/ping.gen.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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.

4 participants