Comments (19)
I just figured out that this issue shows only when you use allOf and you define required
in both reference and the second object.
from modelina.
Thanks for the issueΒ @czlowiek488, let me check π
from modelina.
With this AsyncAPI document:
{
"asyncapi": "2.0.0",
"info": {
"title": "example",
"version": "0.1.0"
},
"channels": {
"example": {
"publish": {
"message": {
"payload": {
"$id": "someEventName",
"allOf": [
{
"$ref": "#/components/schemas/EventEnvelope"
},
{
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"project": {
"$ref": "#/components/schemas/Project"
}
}
}
}
}
]
}
}
}
}
},
"components": {
"schemas": {
"EventEnvelope": {
"$id": "EventEnvelope",
"description": "Envelope shared between messages",
"type": "object",
"required": [
"eventName"
],
"properties": {
"eventName": {
"type": "string"
}
}
},
"Project": {
"$id": "Project",
"type": "object",
"required": [
"id"
],
"properties": {
"id": {
"type": "string"
}
}
}
}
}
}
Which returns 3 interfaces
SomeEventName - Here <anonymous-schema-3>
is a bug, gonna add another issue for this, the current workaround is to specify $id
for last schema in allOf
.
export interface SomeEventName {
eventName: string;
data?: <anonymous-schema-3>;
}
Project
export interface Project {
id: string;
}
<anonymous-schema-3>
export interface AnonymousSchema_3 {
project?: Project;
}
Is this not the expected behavior @czlowiek488? besides <anonymous-schema-3>
of course.
from modelina.
Oh sorry. I found an issue but I written wrong case to reproduce.
Try this.
components:
schemas:
Project:
$id: Project
type: object
properties:
id:
type: string
name:
type: string
messages:
someEventName:
required: [id]
$ref: "#/components/schemas/Project"
someEventName2:
required: [id, name]
$ref: "#/components/schemas/Project"
from modelina.
@czlowiek488 that is not a valid AsyncAPI document, messages cannot define required
, are you able to provide the full example? I don't care how long it is, potentially you could use https://gist.github.com/
Cause the examples I recreate, all works as expected
from modelina.
Alright I think I recreated it π€
{
"asyncapi": "2.0.0",
"info": {
"title": "example",
"version": "0.1.0"
},
"channels": {},
"components": {
"messages": {
"someEventName": {
"payload": {
"required": ["id"],
"$ref": "#/components/schemas/Project"
}
},
"someEventName2": {
"payload": {
"required": ["id", "name"],
"$ref": "#/components/schemas/Project"
}
}
},
"schemas": {
"Project": {
"$id": "Project",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
}
}
}
Only output
export interface Project {
id: string;
name: string;
}
Where both fields are required.
from modelina.
This is what I am dealing with https://github.com/xtrf/XTRFCloudInternalEventBus/blob/main/spec/TMSBusinessEvents.yaml
from modelina.
Have you added $id
to the project
schema? In order to generate a model named Project
? π€
from modelina.
Here is how I see the problem (and I am assuming that you have added the $id
property to the project
schema).
When a schema is referenced multiple places and the referenced schema has $id
sat, such as:
...
"someEventName": {
"payload": {
"required": ["id"],
"description": "test"
"$ref": "#/components/schemas/Project"
}
},
"someEventName2": {
"payload": {
"required": ["id", "name"],
"$ref": "#/components/schemas/Project"
}
}
...
The Project
schema is rendered by accumulating properties i.e. it will inherit description
and required
from the someEventName
payload. It will then merge the schema payload from someEventName2
which means the extra required property name
is added to the list.
Temporary workaround: Add the $id
from the referenced schemas and do not add an $id
to Project
.
...
"someEventName": {
"payload": {
"$id": "someEventNameProject",
"required": ["id"],
"description": "test"
"$ref": "#/components/schemas/Project"
}
},
"someEventName2": {
"payload": {
"$id": "someEventName2Project",
"required": ["id", "name"],
"$ref": "#/components/schemas/Project"
}
}
...
"Project": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
...
This will force the generator library to generate 2 models for project.
from modelina.
I will have to ponder some more about what we can do about the underlying solution to make this as clean as possible... Since in principle, you do want two models generated π€ Right @czlowiek488 ?
from modelina.
Yes, but this way may be actually the right way. If it could work in someway without $id each $ref, it has to had automatically generated names. And that`s something what should be avoided imho.
I think some kind of error should be throwed in case when you overwrite fields from schema without a $id. Or at least user should be warned.
from modelina.
If it could work in someway without $id each $ref, it has to had automatically generated names. And that`s something what should be avoided imho.
Yea... I was thinking about whether we could rename the schema using the $id in some context. If there is a clash when referenced from properties we could give it the model the name <property name><schema $id>
... Have to think some more about this.
I think some kind of error should be throwed in case when you overwrite fields from schema without a $id. Or at least user should be warned.
Agreed, #152 or a follow-up issue should add this.
from modelina.
Yea... I was thinking about whether we could rename the schema using the $id in some context. If there is a clash when referenced from properties we could give it the model the name <property name><schema $id>... Have to think some more about this.
Ignore it for now, it will complicate code a lot. It should not be included in MVP.
from modelina.
Not to mention that this whole codebase is soooo complex i would rather rewrite it from scratch than maintain....
from modelina.
Not to mention that this whole codebase is soooo complex i would rather rewrite it from scratch than maintain....
Are you referring to this library? π€ If so, then yea... It is unfortunately just the nature of converting JSON Schema validation rules to a model definition (the process we call simplification), even with a rewrite the complexity behind this is unavoidable (in my opion at least).. Especially since those validation rules can be defined in multiple ways...
from modelina.
Yes I do refer to generator-model-sdk, those classes, callbacks, circular function executions makes it hard to maintain.
Main problem is that you have to know how everything works to understand how small piece like function works...
Also simplifier is so hard to understand that you can`t even write right documentation for it.
from modelina.
Yeaa... @czlowiek488 would you mind creating an issue for this? @magicmatatjahu and I have discussed it before, none of us could see how to change the implementation to make it more maintainable. However, it would be great to have an issue stating it from someone who hasn't build the library π
from modelina.
I don`t have time until middle of may. For now I can show you few themes you can read about and what imho will help you to create better code structures.
- Composition pattern - long story short you describe "how" instead of "what", this is huge change in thinking how to develop stuff
- Currying - allow you to instance object without using classes ( no this === no problem )
- async/await - allow you to almost completely remove callbacks, but if you must you should create your own Promise instance where you encapsulate callback.
PS. Classes are the biggest disaster in javascript, only reasons to use it are ( very rarely, should be avoided ) extending and decorators.
from modelina.
@czlowiek488 no worries, just wanted you to create a general issue about maintainability, you dont have to make suggestions on how to improve it, just raise the issue.
Since this issue have been "solved" or through other issues I am closing it. Feel free to reopen if that is not the case.
from modelina.
Related Issues (20)
- Lonely `additionalProperties` does not generate dictionary type HOT 1
- Tuple types in marshalling are generated with syntax errors
- Java marshalling preset generate code with syntax errors HOT 1
- Java jackson preset not serializing dictionary models with unwrap option correctly HOT 1
- Enable java jackson preset feature to serialize/deserialize unwrappable dictionaries HOT 1
- [FEATURE] Change the name of the generated model class withe name of message name. HOT 7
- [BUG] Pydantic generator does not correctly handle nullable union HOT 3
- [FEATURE] export AbstractDependencyManager to allow overriding HOT 2
- Cannot process AsyncAPI YAML document HOT 1
- [BUG] Modelina is creating nested models for pattern properties HOT 1
- Dead links HOT 2
- [FEATURE] Support marshal / unmarshal enum values to target types in Go generator HOT 6
- Properties with type integer and format int64 generate properties of type int instead of type long using the C# generator HOT 3
- [BUG] Generated JsonConverter tries to Deserialize enums as dynamic in the C# generator HOT 2
- Move isEnum from C# generator to generic helper function
- [BUG] from . import AnonymousSchemaX breaks types (?) HOT 2
- [FEATURE] Adoption of React Query HOT 1
- [generalized validation] Generate code validation rules based on spec for every supported language HOT 1
- [BUG] Data class members name is changed to camel case even if an $Id is provided [Kotlin Generator] HOT 3
- [FEATURE] [Java] Add "additionalModelTypeAnnotations" to enable using Lombok HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modelina.