OpenAPITools / OpenAPITools/openapi-generator
[BUG][DefaultCodegen] allOf not respecting readOnly and all
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
I try to use a enum property, but override readOnly attribute (same for description, and all others), but it is not working.
I only noticed, because I'm having my own template script (not related to this problem!) to not generate @NotNull anotation in case of required but readOnly attribut, like here: #9655
This is the proposed way of dealing with readOnly consider by OpenAPI spec / JSON schema, see:
- https://github.com/OAI/OpenAPI-Specification/issues/872#issuecomment-339847815
- https://github.com/OAI/OpenAPI-Specification/issues/1671#issuecomment-414690621
openapi-generator version
latest version, master.
OpenAPI declaration file content or url
File src/test/resources/3_0/allOf-readonly.yaml
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/person/display/{personId}:
get:
parameters:
- name: personId
in: path
required: true
description: The id of the person to retrieve
schema:
type: string
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/clubForCreation"
components:
schemas:
ClubType:
type: string
enum:
- serious
- silly
clubForCreation:
properties:
type:
$ref: '#/components/schemas/ClubType'
clubForUpdate:
properties:
type:
allOf:
- $ref: '#/components/schemas/ClubType'
- readOnly: true
Generation Details
Steps to reproduce
Test for DefaultCodegenTest
public void testAllOfReadonly() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/allOf-readonly.yaml");
DefaultCodegen codegen = new DefaultCodegen();
// OK
Schema clubForCreation = openAPI.getComponents().getSchemas().get("clubForCreation");//clubForCreation.getProperties().get("type")).getReadOnly() is null
CodegenProperty clubForCreationType = codegen.fromProperty("type", clubForCreation);
Assert.assertEquals(clubForCreationType.isReadOnly, false);
// NOK
Schema clubForUpdate = openAPI.getComponents().getSchemas().get("clubForUpdate");// clubForUpdate.getProperties().get("type")).getReadOnly() is null
CodegenProperty clubForUpdateType = codegen.fromProperty("type", clubForUpdate);
Assert.assertEquals(clubForUpdateType.isReadOnly, true);
}
Related issues/PRs
Suggest a fix
When a property is a ComposedSchema (espacially an "allOf") it should sync/merge all readOnly / description / ... values...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with src/test/resources/3_0/allOf-readonly.yaml and testAllOfReadonly in DefaultCodegenTest. Trace DefaultCodegen.fromProperty for the ComposedSchema using allOf, then verify that the test distinguishes the ordinary property from the readOnly allOf property. Done means the provided assertions pass, including isReadOnly=false for clubForCreation and true for clubForUpdate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100