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

Union tmpl generates wrong code when using non string type #1668

Open
mweibel opened this issue Jun 24, 2024 · 0 comments
Open

Union tmpl generates wrong code when using non string type #1668

mweibel opened this issue Jun 24, 2024 · 0 comments
Labels
area:*of `oneOf`/`anyOf`/`allOf` bug Something isn't working

Comments

@mweibel
Copy link

mweibel commented Jun 24, 2024

openapi: "3.0.0"
info:
  version: 0.0.1
  title: Test
  description: Example
servers:
  - url: /v1

paths:
  /stuff:
    post:
      description: Create stuff
      operationId: createStuff
      security:
        - Bearer: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CreateStuff"
      responses:
        "201":
          description: created stuff
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Stuff"
components:
  schemas:
    CreateStuff:
      oneOf:
        - $ref: "#/components/schemas/CreateStuffBla"
        - $ref: "#/components/schemas/CreateStuffNope"
      discriminator:
        propertyName: which
        mapping:
          bla: "#/components/schemas/CreateStuffBla"
          nope: "#/components/schemas/CreateStuffNope"
    CreateStuffProperties:
      type: object
      required:
        - which
      properties:
        which:
          $ref: "#/components/schemas/WhichStuff"
    CreateStuffBla:
      allOf:
        - $ref: "#/components/schemas/CreateStuffProperties"
        - type: object
          properties:
            settings:
              $ref: "#/components/schemas/StuffBla"
    CreateStuffNope:
      allOf:
        - $ref: "#/components/schemas/CreateStuffProperties"
        - type: object
          properties:
            settings:
              $ref: "#/components/schemas/StuffNope"
    Stuff:
      type: object
      required:
        - which
      properties:
        which:
          $ref: "#/components/schemas/WhichStuff"
    WhichStuff:
      type: string
      nullable: true
      enum:
        [
          bla,
          nope,
        ]
      default: bla
    StuffBla:
      type: object
      properties:
        something:
          type: string
    StuffNope:
      type: object
      properties:
        nothing:
          type: string

generated code:

// AsCreateStuffBla returns the union data inside the CreateStuff as a CreateStuffBla
func (t CreateStuff) AsCreateStuffBla() (CreateStuffBla, error) {
	var body CreateStuffBla
	err := json.Unmarshal(t.union, &body)
	return body, err
}

// FromCreateStuffBla overwrites any union data inside the CreateStuff as the provided CreateStuffBla
func (t *CreateStuff) FromCreateStuffBla(v CreateStuffBla) error {
	v.Which = "bla"
	b, err := json.Marshal(v)
	t.union = b
	return err
}

// MergeCreateStuffBla performs a merge with any union data inside the CreateStuff, using the provided CreateStuffBla
func (t *CreateStuff) MergeCreateStuffBla(v CreateStuffBla) error {
	v.Which = "bla"
	b, err := json.Marshal(v)
	if err != nil {
		return err
	}

	merged, err := runtime.JSONMerge(t.union, b)
	t.union = merged
	return err
}

One of the errors:

'"bla"' (type string) cannot be represented by the type *WhichStuff

v.Which = "bla" (etc.) doesn't work because Which is of type *WhichStuff and not a string. In our case it's even a type within another package (i.e. using x-go-type).

Besides fixing this issue, I wonder if we could also make the generation of those From<Name> and As<Name> and Merge<Name> optional. We don't use them at all and don't plan to use them either.

This might be related to #1619 but I haven't fully verified it.

@jamietanna jamietanna added bug Something isn't working area:*of `oneOf`/`anyOf`/`allOf` labels Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:*of `oneOf`/`anyOf`/`allOf` bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants