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

datasource incorrect type when name overlaps with parameters #93

Open
danquack opened this issue Nov 17, 2023 · 1 comment
Open

datasource incorrect type when name overlaps with parameters #93

danquack opened this issue Nov 17, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@danquack
Copy link

danquack commented Nov 17, 2023

Config Files

In my OpenAPI Spec, I have the following OpenAPI spec:

openapi: 3.0.1
info:
  title: Swagger Petstore
  description: ""
  version: 1.0.6
servers:
- url: https://petstore.swagger.io/v2
paths:
  /pet/{id}:
    get:
      summary: Find pet by ID
      parameters:
      - name: id
        in: path
        required: true
        schema:
          type: string
      responses:
        "200":
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'
    post:
      summary: Updates a pet in the store with form data
      parameters:
      - name: id
        in: path
        description: ID of pet that needs to be updated
        required: true
        schema:
          type: string
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                name:
                  type: string
                  description: Updated name of the pet
                status:
                  type: string
                  description: Updated status of the pet
      responses:
        "200":
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      required:
      - name
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
          example: doggie
      xml:
        name: Pet

Coupled with the generator config:

provider:
  name: pet
resources:
  pet:
    read:
      path: /pet/{id}
      method: GET
    create:
      path: /pet/{id}
      method: POST
data_sources:
  pet:
    read:
      path: /pet/{id}
      method: GET

Result Files

When the code spec is generated (tfplugingen-openapi generate --config ./generator_config.yml --output ./provider_code_spec.json ./swagger.yaml) we see the attribute id within the datasource defined as a string, but the resource defined as the desired int:

Data Source
{
	"name": "id",
	"string": {
		"computed_optional_required": "required"
	}
}
Resource
{
	"name": "id",
	"int64": {
		"computed_optional_required": "computed"
	}
},

Expected Outcome

If I change the path in the OpenAPI spec, to /pet/{petId} the datasource returns the id as the desired int64 type. The expected outcome is with /pet/{id} on the data source, the code gen should return an int64 type for data source, as it does in the resource.

@danquack danquack changed the title datasource - Incorrect type when name overlap with parameters datasource Incorrect type when name overlap with parameters Nov 17, 2023
@danquack danquack changed the title datasource Incorrect type when name overlap with parameters datasource incorrect type when name overlap with parameters Nov 17, 2023
@danquack danquack changed the title datasource incorrect type when name overlap with parameters datasource incorrect type when name overlaps with parameters Nov 17, 2023
@austinvalle
Copy link
Member

Hey there @danquack, thanks for filing an issue and sorry you're running into trouble here.

I think the problem described here is coming from the design decisions we made around priority of conflicting attributes.

  • In resources, the POST request body takes priority over query/path parameters
  • In data sources, the query/path parameters take priority over the GET response body

One of the reasons we made that decision is to determine computability (required, optional, computed), but in the case of conflicting types (string vs int64), rather than throwing an error, we just take the higher priority attribute.

Maybe a potential improvement we can make here is allowing the ability to configure this priority somehow.

An ugly workaround that could be used in the immediate is to rename/ignore the path parameter in your example, however you will lose the computability determination that comes from the parameter:

provider:
  name: pet
resources:
  pet:
    read:
      path: /pet/{id}
      method: GET
    create:
      path: /pet/{id}
      method: POST
data_sources:
  pet:
    read:
      path: /pet/{id}
      method: GET
    schema:
      attributes:
        aliases:
          id: param_id
      ignores:
        - param_id
{
	"datasources": [
		{
			"name": "pet",
			"schema": {
				"attributes": [
					{
						"name": "id",
						"int64": {
							"computed_optional_required": "computed"
						}
					},
					{
						"name": "name",
						"string": {
							"computed_optional_required": "computed"
						}
					}
				]
			}
		}
	],
	// rest of spec   
}

@austinvalle austinvalle added the enhancement New feature or request label Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants