Skip to content

Commit

Permalink
feat: support readOnly and writeOnly (deepmap#547)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Leonhard <leo1fr@bosch.com>
  • Loading branch information
NaridaL and Adrian Leonhard committed Apr 19, 2022
1 parent ccfddd8 commit b5ef7c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
43 changes: 25 additions & 18 deletions internal/test/components/components.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/test/components/components.yaml
Expand Up @@ -92,9 +92,20 @@ components:
type: string
firstName:
type: string
readOnlyRequiredProp:
description: |
This property is required and readOnly, so the go model should have it as a pointer,
as it will not be included when it is sent from client to server.
type: string
readOnly: true
writeOnlyRequiredProp:
type: integer
writeOnly: true
required:
- role
- firstName
- readOnlyRequiredProp
- writeOnlyRequiredProp
AdditionalPropertiesObject1:
description: Has additional properties of type int
type: object
Expand Down
10 changes: 8 additions & 2 deletions pkg/codegen/schema.go
Expand Up @@ -67,6 +67,8 @@ type Property struct {
Schema Schema
Required bool
Nullable bool
ReadOnly bool
WriteOnly bool
ExtensionProps *openapi3.ExtensionProps
}

Expand All @@ -76,7 +78,9 @@ func (p Property) GoFieldName() string {

func (p Property) GoTypeDef() string {
typeDef := p.Schema.TypeDecl()
if !p.Schema.SkipOptionalPointer && (!p.Required || p.Nullable) {
if !p.Schema.SkipOptionalPointer &&
(!p.Required || p.Nullable || p.ReadOnly || p.WriteOnly) {

typeDef = "*" + typeDef
}
return typeDef
Expand Down Expand Up @@ -259,6 +263,8 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
Required: required,
Description: description,
Nullable: p.Value.Nullable,
ReadOnly: p.Value.ReadOnly,
WriteOnly: p.Value.WriteOnly,
ExtensionProps: &p.Value.ExtensionProps,
}
outSchema.Properties = append(outSchema.Properties, prop)
Expand Down Expand Up @@ -454,7 +460,7 @@ func GenFieldsFromProperties(props []Property) []string {

fieldTags := make(map[string]string)

if p.Required || p.Nullable || !omitEmpty {
if (p.Required && !p.ReadOnly && !p.WriteOnly) || p.Nullable || !omitEmpty {
fieldTags["json"] = p.JsonFieldName
} else {
fieldTags["json"] = p.JsonFieldName + ",omitempty"
Expand Down

0 comments on commit b5ef7c9

Please sign in to comment.