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

Default value not applied for application/x-www-form-urlencoded #1425

Open
gudimz opened this issue Mar 13, 2025 · 0 comments
Open

Default value not applied for application/x-www-form-urlencoded #1425

gudimz opened this issue Mar 13, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@gudimz
Copy link

gudimz commented Mar 13, 2025

What version of ogen are you using?

$ go list -m github.com/ogen-go/ogen
github.com/ogen-go/ogen v1.10.1

Swagger example

openapi: 3.0.0
info:
  title: Simple Pet API
  version: "1.0.0"
paths:
  /pet:
    post:
      summary: Create a pet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: integer
                  name:
                    type: string
                  isActive:
                    type: boolean
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
          default: "DefaultName" # attention to this field
        isActive:
          type: boolean
          default: true # attention to this field
      required:
        - id

Implementation of the handler interface

// PetPost creates a pet.
// POST /pet
func (h *Handler) PetPost(_ context.Context, req rest.PetPostReq) (*rest.PetPostOK, error) {
	var data rest.Pet

	switch v := req.(type) {
	case *rest.PetPostApplicationJSON:
		data = rest.Pet(*v)
	case *rest.PetPostApplicationXWwwFormUrlencoded:
		data = rest.Pet(*v)
	}
	return &rest.PetPostOK{
		ID:       rest.NewOptInt(data.ID),
		Name:     data.Name,
		IsActive: data.IsActive,
	}, nil
}

Content type application/json

 curl -X POST "127.0.0.1:8080/pet" \
     -H "Content-Type: application/json" \
     -d '{"id":1}' 
{"id":1,"name":"DefaultName","isActive":true}

Content type application/x-www-form-urlencoded

 curl -X POST "127.0.0.1:8080/pet" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "id=1"
{"id":1}

Expected behavior

When sending a request with either application/json or application/x-www-form-urlencoded content type, the default values specified in the schema should be automatically applied if the corresponding fields are missing in the request.

Observed behavior

The default values are correctly applied for application/json, but they are not applied when using application/x-www-form-urlencoded.

@gudimz gudimz added the bug Something isn't working label Mar 13, 2025
@gudimz gudimz changed the title [SUBJECT]: Default value not applied for application/x-www-form-urlencoded Default value not applied for application/x-www-form-urlencoded Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant