We have an endpoint with json response as in Code1. There is an user condition and when this is met second version of json with string “Employee” added as in Code2.
I’m trying to document this in swagger and having issues embedding these schema’s. Below is my code, but some how getting swagger invalid with “paths must not include query string” and “potentially unused component has been detected errors” and so on. I’m not sure if this is right way of doing it or if there is any better way to achieve this? Any help would be really helpful
Below is my existing json response in for one of the end point as in Code1:.
Code1:
[
{
"EmpId":{
"String":"ABCD",
"Valid":true
},
"Department":{
"Float64":0,
"Valid":true
}
}
]
We have an optional parameter where the condition is met, below will be the response
Code2:
{
"Employee":[
{
"EmpId":{
"String":"ABCD",
"Valid":true
},
"Department":{
"Float64":0,
"Valid":true
}
}
]
}
Below is my model to achieve this and we have an if condition within the code to determine which response to get.
type Employeewithroot struct {
Employees []Employees `json:"Employee"`
}
type Employees struct {
EmpNbr sql.NullString `json:"EmpNbr"`
DateofJoin sql.NullString `json:"DateofJoin"`
DeptId sql.NullString `json:"DeptId"`
DeptName sql.NullString `json:"DeptName"`
}
openapi: 3.0.1
info:
title: API's
description: >-
REST API's for abc
contact:
name: ABC
email: ABC
version: "1.0.0"
servers:
- url: "http://localhost:8080/"
tags:
- name: Employees
description: Get Employees info
paths:
/Employees:
get:
description: Get Employee info
summary: Get Employee.
tags:
- Employees
security:
- basicAuth:
- '[]'
operationId: GetEmployees
parameters:
- name: EmpNbr
in: query
description: Employee ID(Example:0123456789)
required: true
schema:
type: string
- name: DateofJoin
in: query
description: Input DateofJoin(Example:mmddyyyy(01012023))
required: true
schema:
type: string
responses:
'200':
description: OK.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Employees'
/Employees?EmpNbr={EmpNbr}&DateofJoin={DateofJoin}&cond={cond}:
get:
description: Get Employees info as per additional condition
summary: Get EmployeeRoot Hdr.
tags:
- Employees
operationId: GetEmployeeRootHdr
parameters:
- name: EmpNbr
in: path
description: Emp ID(Example:0123456789)
required: true
schema:
type: string
- name: DateofJoin
in: path
description: Input DateofJoin(Example:mmddyyyy(01012023))
required: true
schema:
type: string
- name: cond
in: path
description: additional check
required: true
schema:
type: string
allowEmptyValue: true
responses:
'200':
description: OK.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Employeewithroot'
components:
schemas:
Employeewithroot:
properties:
Employee:
items:
$ref: '#/components/schemas/Employees'
type: array
x-go-name: Employees
Employees:
properties:
EmpNbr:
$ref: '#/components/schemas/NullString'
DateofJoin:
$ref: '#/components/schemas/NullString'
DeptId:
$ref: '#/components/schemas/NullString'
DeptName:
$ref: '#/components/schemas/NullString'
x-go-package: pkg/model
securitySchemes:
basicAuth:
type: apiKey
name: Authorization
in: header
Added the steps to the question