-
Notifications
You must be signed in to change notification settings - Fork 6
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
Improvements and fixes #18
Conversation
- Trims all descriptions - Replaces `\n` by `\n///` after trimming to avoid comments with an empty line at the end
Small performance improvement. Using `Future.value` Dart will create a new microtask event into the microtask queue instead of a new event into the event queue.
a9a5c7b
to
c50d4da
Compare
c50d4da
to
615e0d7
Compare
lib/src/open_api/spec.dart
Outdated
@@ -528,6 +528,8 @@ Map<String, dynamic> _formatSpecFromJson( | |||
if (m.containsKey('type')) { | |||
if (m['type'] == 'string' && m.containsKey('enum')) { | |||
m['type'] = 'enumeration'; | |||
final d = m['description'] ?? ''; | |||
m['description'] = '$d\nPossible values: ${m['enum']}'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidmigloz yes, I would probably move this to the generator. Although, I do have a question:
Why are we re-documenting enum
values in the doc strings. This seems redundant. Simply clicking on the type should take you to the enum definition (lives in its own file) that you can quickly inspect to see the values. I think this adds a lot of extra documentation that may be unnecessary and distracting, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, if the enum is defined as an inner schema, it is generated with type String
.
Eg.
schemas:
CreateCompletionRequest:
type: object
description: Request object for the Create completion endpoint.
properties:
model:
description: &model_description |
ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-...
type: string
enum:
[
"babbage-002",
"davinci-002",
"gpt-3.5-turbo-instruct",
"text-davinci-003",
"text-davinci-002",
"text-davinci-001",
"code-davinci-002",
"text-curie-001",
"text-babbage-001",
"text-ada-001",
]
It will be converted into:
/// Factory constructor for CreateCompletionRequest
const factory CreateCompletionRequest({
/// ID of the model to use. You can use the [List models](https://platform.openai.com/doc...
required String model,
})
So, in this case, documenting the possible values is nice. I agree, that when using an actual enum it is redundant.
I've moved that logic to the generator and only for this specific case. If you define the enum as a separate schema (which by the way is broken at the moment), it won't document the enum values.
Let me know if that works for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you define the enum as a separate schema (which by the way is broken at the moment)
What do you mean by this? Maybe I am misunderstanding, but I generate enum schemas all the time from defined component schemas. Here is an example:
"components": {
"schemas": {
"Task": {
"title": "Task",
"enum": [
"pending",
"completed",
"failed"
],
"type": "string",
"description": "An enumeration."
},
}
}
enum Task {
@JsonValue('pending')
inProgress,
@JsonValue('completed')
canceled,
@JsonValue('failed')
failed,
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't dug into it yet, but if instead of this:
CreateCompletionRequest:
type: object
description: Request object for the Create completion endpoint.
properties:
model:
description: ...
type: string
enum:
[
"babbage-002",
"davinci-002",
"gpt-3.5-turbo-instruct",
"text-davinci-003",
"text-davinci-002",
"text-davinci-001",
"code-davinci-002",
"text-curie-001",
"text-babbage-001",
"text-ada-001",
]
I move it to its own schema:
CompletionModel:
type: string
enum:
[
"babbage-002",
"davinci-002",
"gpt-3.5-turbo-instruct",
"text-davinci-003",
"text-davinci-002",
"text-davinci-001",
"code-davinci-002",
"text-curie-001",
"text-babbage-001",
"text-ada-001",
]
CreateCompletionRequest:
type: object
description: Request object for the Create completion endpoint.
properties:
model:
$ref: "#/components/schemas/CompletionModel"
I get the following exception:
Unhandled exception:
'package:openapi_spec/src/open_api/index.freezed.dart': Failed assertion: line 10478 pos 13: 'values == null || ref == null': Cannot define both values and ref
I'll look into it in a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue seems to be that when dereferencing an enum the new _SchemaEnum
will have both ref
and values
, which is not allowed by the assert.
I've removed the assert and it works as expected (but I guess it's not ideal), do you have a better suggestion?
@davidmigloz I tested this out on a fairly complicated spec and everything looks good. My only comment is regarding "Add possible values in enums descriptions". Without more context regarding what benefit this adds, I think we should remove this change from this PR. Let me know when you are ready for this to be merged and I will get it in. |
3d52b6d
to
9f4cae9
Compare
9f4cae9
to
2d3fd0e
Compare
I've included a couple more fixes in the PR (let me know if it's getting too big 😅)
|
No this is great stuff! I really want to add mini tests composed of small API spec snippets that test individual cases and ensure that the generated code is proper. In time! Let's get this merged in and see if we can close out some of the other issues and cut a new release before the weekend is out. |
Awesome! Yeah, the small API spec snippets will really help. |
\n
by\n///
after trimming to avoid comments with an empty line at the endFuture.value
in middleware functionsFuture.value
Dart will create a new microtask event into the microtask queue instead of a new event into the event queue.requestBody
descriptioncc @walsha2