I have an API that returns a JSON array of objects modeled in the attached spec. The generated code has both a ListOfMessage object and a Message object but the ListOfMessage object has no fields.
3.0.0
openapi: 3.0.0
info:
description: Chat API
version: "1.0.0-oas3"
title: Chat API
contact:
email: [email protected]
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: admins
description: Secured Admin-only calls
- name: developers
description: Operations available to regular developers
paths:
'/api/v1/chat/{acctNum}/room/{id}/messages':
get:
tags:
- developers
summary: searches for messages by room
operationId: messagesByRoomV1
parameters:
- in: path
name: acctNum
description: Account number of organization
required: true
schema:
type: string
- in: path
name: id
description: Chat room id
required: true
schema:
type: integer
responses:
'200':
description: search results matching criteria
content:
application/json:
schema:
$ref: '#/components/schemas/ListOfMessage'
'400':
description: bad input parameter
'403':
description: >-
forbidden if account number does not match user organization account
number and user is not a super-admin
'500':
description: internal server error
servers:
- url: 'https://test.fake.io'
components:
schemas:
ListOfMessage:
type: array
items:
$ref: '#/components/schemas/Message'
Message:
required:
- id
- createdOn
- updatedOn
- owner
- ownerName
- content
- chatRoomId
properties:
id:
type: integer
createdOn:
type: integer
updatedOn:
type: integer
owner:
type: integer
ownerName:
type: string
content:
type: string
chatRoomId:
type: integer
The generated code could simply return a Dart list of Message or a a list of Message could be added to the ListOfMessage class. It seems the generator does not support components of type array in this language.
A workaround is to change the ListOfMessage definition to the following:
ListOfMessage:
required:
- messages
properties:
messages:
type: array
items:
$ref: '#/components/schemas/Message'
however, I was only able to do this because I control the API.
Instead of using/defining ListOfMessage, what about defining the content as the following instead?
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Message'
Thanks @wing328 that worked!
No problem. Glad to help out.
@wing328 I don't believe this bug should have been closed. Following the thread above, it seems that you closed it because the workaround worked, but it requires control over the api spec. If you don't have control over the spec, you cannot apply the workaround.
Most helpful comment
@wing328 I don't believe this bug should have been closed. Following the thread above, it seems that you closed it because the workaround worked, but it requires control over the api spec. If you don't have control over the spec, you cannot apply the workaround.