Duplicate type name from automatically named property of schema
Hello everyone,
I've just spent some time debugging this, I solved it for myself given that it actually was a typo, but I feel like this is a real issue.
go version: 1.22.0 linux/amd64
oapi-codegen version: 2.3.0
YAML config:
package: build
generate:
echo-server: true
models: true
output: build/openapi.go
openapi.yaml:
openapi: 3.0.0
components:
schemas:
Request:
type: object
properties:
status:
type: string
enum:
- success
required:
- status
RequestStatus:
type: string
enum:
- created
- waiting_for_review
- waiting_for_payment
- paid
- processing
- completed
- expired
paths:
/requests:
get:
operationId: GetRequest
parameters:
- name: status
in: query
description: Request status (filter)
required: false
allowEmptyValue: false
schema:
$ref: '#/components/schemas/RequestStatus'
responses:
'200':
description: User's request
content:
application/json:
schema:
$ref: '#/components/schemas/Request'
headers:
Conio-RequestID:
description: Request ID
schema:
type: string
Conio-SessionID:
description: Session ID
schema:
type: string
Basically what will happen here is that oapi-codegen will create a struct for the Request model that will have a Status property of type RequestStatus.
This RequestStatus is a string type that defines what the property is, the catch here is that the name RequestStatus has been chosen by oapi-codegen.
This means that if I actually define a RequestStatus, like the schema below Request, it will create a conflict between the two of them and exit the code generation with this error:
error generating code: error generating type definitions: error generating code for type definitions: duplicate typename 'RequestStatus' detected, can't auto-rename, please use x-go-name to specify your own name for one of them
Now, as suggested by the error's text, we can add x-go-name, give it a name and everything will be fine.
I'm my case this is not working.
Just to be clear, this is what the Request schema is after the add:
Request:
type: object
properties:
status:
x-go-name: RequestStatusProp
type: string
enum:
- success
required:
- status
Now, I'd like to raise two points here:
- is there a way to make the error explain a little bit better what actually is happening? I spent some hours behind that before understanding what actually was happening;
- is the
x-go-nameactually not working in this case? Or am I using it wrong?
BTW, I'd like to precise that I see there is an architectural issue here, the property status of the object Request can NOT be different from an object called RequestStatus (it can but is highly misleading). Anyway, take the openapi just as an example for the sake of this issue.
bro you are working on linux how did you downloaded oapi because i downloaded oapi and when i whrite some command i am getting error oapi-codegen : command not found i am working on ubuntu 22.04