nested schema definitions on openapi spec #446
-
Currently, I'm using maven plugin to generate json schema from my response classes. After having generated json schema, I use them into my openapi spec 3.x in order to create response schemas. I'm getting an issue with this generated schema: {
"definitions" : {
"NestedGroupResponse" : {
"type" : "object",
"properties" : {
"groups" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/NestedGroupResponse"
}
},
"label" : {
"type" : "string"
},
"references" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
},
"type" : "object",
"properties" : {
"documentId" : {
"type" : "string"
},
"group" : {
"type" : "object",
"properties" : {
"groups" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/NestedGroupResponse"
}
},
"references" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
},
"id" : {
"type" : "string"
},
"transactionId" : {
"type" : "string"
}
}
} The above schema class is: @Data
@Builder
@Jacksonized
public class GetReferenceByUniqueIdentifierResponse {
private static final String ID = "id";
private static final String TRANSACTION_ID = "transactionId";
private static final String DOCUMENT_ID = "documentId";
private static final String GROUP = "group";
@JsonProperty(ID)
private String id;
@JsonProperty(TRANSACTION_ID)
private String transactionId;
@JsonProperty(DOCUMENT_ID)
private String documentId;
@JsonProperty(GROUP)
@JsonInclude(JsonInclude.Include.NON_NULL)
private ReferenceGroupResponse referenceGroup;
}
@Data
@Builder
@Jacksonized
public class ReferenceGroupResponse {
private static final String GROUPS = "groups";
private static final String REFERENCES = "references";
@JsonProperty(GROUPS)
private List<NestedGroupResponse> groups;
@JsonProperty(REFERENCES)
private List<String> references;
}
@Data
@Builder
@Jacksonized
public class NestedGroupResponse {
private static final String LABEL = "label";
private static final String GROUPS = "groups";
private static final String REFERENCES = "references";
@JsonProperty(LABEL)
private String label;
@JsonProperty(GROUPS)
private List<NestedGroupResponse> groups;
@JsonProperty(REFERENCES)
private List<String> references;
} maven plugin generates this schema correctly. Nevertheless, openapi swagger editor is telling me:
I guess that nested schemas doesn't quite fit well on openapi spec. Any ideas about how to handle this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @jeusdi, It sounds like the issue is not in the schema generation – although I'm missing the schema for Please provide a full executable example including the JSON Schema Generator configuration and the logic around this being injected into the OpenAPI definition. |
Beta Was this translation helpful? Give feedback.
-
Thanks @CarstenWickner. I'm aware it's not an issue of schema generator. I mean, schema generator gets me a valid json schema of my Here my related schema generator maven plugin configuration: <plugin>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<classNames>
<className>net.gencat.transversal.espaidoc.presentation.frontoffice.apigateway.document.push.response.PushDocumentResultResponse</className>
<className>net.gencat.transversal.espaidoc.presentation.frontoffice.apigateway.reference.create.response.CreateReferenceResultResponse</className>
<className>net.gencat.transversal.espaidoc.presentation.frontoffice.apigateway.reference.getbyuniqueidentifier.response.GetReferenceByUniqueIdentifierResponse</className>
</classNames>
<schemaFilePath>./openapi/components/schemas</schemaFilePath>
<schemaFileName>{0}.json</schemaFileName>
<modules>
<module>
<name>Jackson</name>
<options>
<option>FLATTENED_ENUMS_FROM_JSONVALUE</option>
</options>
</module>
</modules>
<options>
<enabled>
<option>EXTRA_OPEN_API_FORMAT_VALUES</option>
</enabled>
<disabled>SCHEMA_VERSION_INDICATOR</disabled>
</options>
</configuration>
</plugin> As you can guess, my goal is using schema generator in order to generate json schemas and use them into my openapi spec. Generated json schemas are generated on "get": {
"summary": "Find reference by ID",
"description": "Returns a single reference",
"operationId": "getReferenceById",
"parameters": [
{
"name": "referenceId",
"in": "path",
"description": "ID of reference to fetch",
"required": true,
"schema": {
"type": "string"
}
}
],
"tags": ["references"],
"responses": {
"200": {
"description": "Reference found",
"content": {
"application/json": {
"schema": {
"$ref": "../components/schemas/GetReferenceByUniqueIdentifierResponse.json"
}
}
}
},
"400": {
"$ref": "../components/responses/BadRequest.json"
},
"401": {
"$ref": "../components/responses/Unauthorized.json"
},
"403": {
"$ref": "../components/responses/Forbidden.json"
},
"404": {
"$ref": "../components/responses/NotFound.json"
},
"500": {
"$ref": "../components/responses/InternalError.json"
}
}
} Probably I don't quite figure out how schemas should be generated according to its destination. Should I generate schemas taking in mind something according to those schemas have to be embedded on an openapi spec? |
Beta Was this translation helpful? Give feedback.
@jeusdi I checked this here: https://swagger.io/specification/ under the "Data types" section it mentions: