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
[BUG] [Dart] additional properties of type: object doesn't generate with mapCastOfType #12165
Comments
Slightly modifying the schema to look like this instead: ItemBad:
type: object
properties:
mappedProps:
type: string
additionalProperties: true and then inserting a check for {{^items.isMap}}
{{#additionalPropertiesIsAnyType}}
{{{name}}}: mapCastOfType<String, Object>(json, r'{{{baseName}}}'){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
{{/additionalPropertiesIsAnyType}}
{{^additionalPropertiesIsAnyType}}
{{{name}}}: mapValueOfType<{{{datatypeWithEnum}}}>(json, r'{{{baseName}}}'){{#required}}{{^isNullable}}!{{/isNullable}}{{/required}}{{^required}}{{#defaultValue}} ?? {{{.}}}{{/defaultValue}}{{/required}},
{{/additionalPropertiesIsAnyType}}
{{/items.isMap}} I can get the dart generators to correctly deserialize the value using But I don't know what the correct way to do this really is. Some issues I have include:
|
I am wondering if the spec itself is valid in your case docs: https://swagger.io/docs/specification/data-models/dictionaries/ |
It's possibly not valid. I admit that I don't fully understand the OpenAPI spec format -- I've included it here in a simplified form because it's something I encountered in the wild at my current company. Even if we take the |
does this schema not work for you? type: object
additionalProperties: true |
I'll double check, but previously that was producing what looked like a valid Dart file, but would fail at runtime when actual access occurred on the generated map, since the generated object was some kind of |
if that happens please include a repro code of what causes the error |
I tried again with the latest master branch -- this time I don't actually get the error described above, but I do get a different and equally bad error. When the check in It's trying to assert that You can try it for yourself here: spec file: openapi: 3.0.3
info:
version: "1.1"
title: Dart Bad Map
servers:
- url: 'localhost'
variables:
host:
default: localhost
components:
schemas:
ItemsWithMapStringAnyWithPropsTrue:
type: object
description: "map-string-any defined with `additionalProperties: true`"
properties:
objectMap:
type: object
additionalProperties: true
paths:
/items:
get:
operationId: GetItemWithMapStringObjects
responses:
"200":
description: A list of ItemWithMapStringObject
content:
application/json:
schema:
$ref: '#/components/schemas/ItemsWithMapStringAnyWithPropsTrue' the test file at
|
that actually makes sense return ItemsWithMapStringAnyWithPropsTrue(
objectMap: mapValueOfType<Map<String, Object>>(json, r'objectMap') ?? {},
); with return ItemsWithMapStringAnyWithPropsTrue(
objectMap: mapCastOfType<String, Object>(json, r'objectMap') ?? {},
); |
Yes, that's what I was trying to solve with the PR -- the fix I included adds that mapCastOfType call to the mustache file for complex maps. edit: my bad, I didn't realize this wasn't the PR thread. I was talking about this PR #12426 |
I am wondering tho, why would |
sounds like a problem with |
I also wondered about that, I figured it was some arcane thing that existed for a good reason -- it's from the core generator, so its beyond the scope of just the dart portion of the project. I spent a long, long time debugging the issue to find out that I recall stepping through it that by that portion of the generator, it's looking at the inner map items instead of the map item itself, which is sees as just a generic |
Bug Report Checklist
Description
The
additionalProperties
field, which specified aMap<T1, T2>
in Dart, doesn't generate properly by fields defined byopenapi-generator version
6.0.0
OpenAPI declaration file content or url
Generation Details
java -jar ..\openapi-generator\modules\openapi-generator-cli\target\openapi-generator-cli.jar generate -i .\shadowspec.yaml -g dart -o mapped
Steps to reproduce
After generation, see the
./mapped/lib/model/item_good.dart
and./mapped/lib/model/item_bad.dart
files.ItemGood, defined as
generates correctly with
However, ItemBad, defined as
generates incorrectly, using
mapValueOfType
instead ofmapCastOfType
:Related issues/PRs
Suggest a fix
Maps should try to generate with mapValueOfType in as many situations as possible
The text was updated successfully, but these errors were encountered: